Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve performance for wav read sample #117

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

hoxily
Copy link

@hoxily hoxily commented Aug 4, 2021

When smaplesToRead reaches 35000 or above, reading a float a time will consumes about 3 seconds, which stalls the whole game.
Should use bunch read to speed up.

Copy link
Owner

@mofr mofr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your pull request! I've left some comments which I would like to be fixed before merging.

Assets/Scripts/Diablerie/Engine/IO/Wav.cs Show resolved Hide resolved
Assets/Scripts/Diablerie/Engine/IO/Wav.cs Show resolved Hide resolved
@@ -130,6 +130,19 @@ private static float ReadSample(BinaryReader reader)
return BytesToFloat(byte1, byte2);
}

private static void ReadSample(BinaryReader reader, float[] sampleBuffer, int startIndex, int sampleCount)
{
byte[] buffer = new byte[sampleCount * 2];
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we reuse the buffer array somehow to avoid frequent allocations and excessive garbage collector work?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about create a reuseable fixed length byte[] buffer when new Wav(), and transfer this buffer to ReadSample method. Works like a Stream's internal buffer.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good

{
byte byte1 = buffer[i];
byte byte2 = buffer[i + 1];
short s = (short)((byte2 << 8) | byte1);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's reuse BytesToFloat here to avoid the transformation logic duplication, please.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we shall test the performances of the following two solutions:

  1. reuse BytesToFloat method;
  2. directly insert body of BytesToFloat;

After performance test, choose the fastest one.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you expect significant function call overhead? In this case, I like your idea to test both of them. If the inlined version works faster I think it's a reasonable cost to duplicate the code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants