-
Notifications
You must be signed in to change notification settings - Fork 494
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
[Internal] Binary Encoding: Fixes Performance Regression #4884
base: master
Are you sure you want to change the base?
[Internal] Binary Encoding: Fixes Performance Regression #4884
Conversation
@@ -129,11 +157,48 @@ protected override void Dispose(bool disposing) | |||
/// </returns> | |||
public byte[] ReadAll() | |||
{ | |||
ArraySegment<byte> byteSegment = this.innerStream.GetBuffer(); | |||
int count, totalBytes = 0, offset = (int)this.Position, length = (int)this.Length; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NIT - I admit personal opinion - but I find it unreadable when multiple variables are declared in one line.
int count, totalBytes = 0, offset = (int)this.Position, length = (int)this.Length; | ||
byte[] bytes = new byte[length]; | ||
|
||
while ((count = this.innerStream.Read(bytes, offset, length - offset)) > 0) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The implementation here is functionally incorrect when you first use Stream.Read and subsequently Stream.ReadAll - for example you would copy the first byte again in the buffer for ReadAll - while it has already been processed in that case.
@@ -129,11 +157,48 @@ protected override void Dispose(bool disposing) | |||
/// </returns> | |||
public byte[] ReadAll() | |||
{ | |||
ArraySegment<byte> byteSegment = this.innerStream.GetBuffer(); | |||
int count, totalBytes = 0, offset = (int)this.Position, length = (int)this.Length; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this.Length is not save when the inner stream is not buffered. And the API contract does not ensure that - it would thorw an exception if the inner stream is not buffered.
/// </returns> | ||
public async Task<byte[]> ReadAllAsync(CancellationToken cancellationToken = default) | ||
{ | ||
int count, totalBytes = 0, offset = (int)this.Position, length = (int)this.Length; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See comments in sync PAI. In general I think Stream implementations should implement the async variation and just await it in the sync overload. The perf overhead of doing it even if the asnyc overload completes synchronously is relatively small.
Pull Request Template
Description
Please include a summary of the change and which issue is fixed. Include samples if adding new API, and include relevant motivation and context. List any dependencies that are required for this change.
Type of change
Please delete options that are not relevant.
Closing issues
To automatically close an issue: closes #IssueNumber