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

Fix repeatedly decoding base64 with large grpc-web-text request #1045

Merged
merged 2 commits into from Sep 10, 2020

Conversation

JamesNK
Copy link
Member

@JamesNK JamesNK commented Sep 9, 2020

Large base64 encoded messages are slow to decode. Each time ReadAsync is called all the buffered data is re-decoded.

This PR changes base64 decoding to be incremental. Only new data is decoded and it is appended to a sequence of decoded data.

3 megabyte message, IIS host:

  • Before - 6000 ms
  • After - 100 ms

@@ -158,31 +170,57 @@ public async override ValueTask<ReadResult> ReadAsync(CancellationToken cancella
}

// Limit result to complete base64 segments (multiples of 4)
var buffer = innerResult.Buffer.Slice(0, (innerResult.Buffer.Length / 4) * 4);
var newResultLength = innerResult.Buffer.Length - _currentInnerBufferRead;
var newResultValidLength = (newResultLength / 4) * 4;
Copy link
Contributor

Choose a reason for hiding this comment

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

Curious if bitmasking would be faster than a division and a multiplication here. Not sure if the compiler is smart enough to automatically do that. Also, I would assume the effect here would be pretty small anyways, but I was just curious.

Copy link
Member Author

Choose a reason for hiding this comment

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

Anything here will be dwarfed by the calls to ReadAsync and base64 decoding.

@JamesNK JamesNK force-pushed the jamesnk/grpcwebtext-readasync-fix branch from 840692c to db296d9 Compare September 9, 2020 21:43
@JamesNK JamesNK merged commit 84f6a3e into grpc:master Sep 10, 2020
@JamesNK JamesNK deleted the jamesnk/grpcwebtext-readasync-fix branch September 10, 2020 01:43
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