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

[Internal] Binary Encoding: Fixes Performance Regression #4884

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

kundadebdatta
Copy link
Member

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.

  • Bug fix (non-breaking change which fixes an issue)

Closing issues

To automatically close an issue: closes #IssueNumber

@@ -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;
Copy link
Member

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)
Copy link
Member

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;
Copy link
Member

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;
Copy link
Member

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.

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