Skip to content

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

// 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.

2 participants