Skip to content

Improve CborWriter buffer growth #92435

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

Merged
merged 6 commits into from
Sep 25, 2023
Merged

Conversation

MichalPetryka
Copy link
Contributor

Fixes #92425.

@ghost ghost added the community-contribution Indicates that the PR has been added by a community member label Sep 21, 2023
@ghost
Copy link

ghost commented Sep 21, 2023

Tagging subscribers to this area: @dotnet/area-system-formats-cbor, @bartonjs, @vcsjones
See info in area-owners.md if you want to be subscribed.

Issue Details

Fixes #92425.

Author: MichalPetryka
Assignees: -
Labels:

area-System.Formats.Cbor

Milestone: -

if (newCapacity < requiredCapacity)
newCapacity = requiredCapacity;

byte[] newBuffer = new byte[newCapacity];
Copy link
Contributor

Choose a reason for hiding this comment

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

would it be worth using GC.AllocateUninitializedArray here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'll let the maintainers decide whether it makes sense here.

Copy link
Member

Choose a reason for hiding this comment

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

AllocateUninitializedArray would probably be OK, except it's not available for .NET Standard 2.0. I don't think it's important enough here that it's worth a #if, so just always using new byte[newCapacity] seems fine.

Copy link
Member

@eiriktsarpalis eiriktsarpalis Sep 22, 2023

Choose a reason for hiding this comment

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

Why not just use Array.Resize as before? Presumably the original buffer is close to full so we're not saving a lot by only copying the written slice.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Presumably the original buffer is close to full so we're not saving a lot by only copying the written slice.

The intent was to avoid copying empty space here, since copying is really the biggest cost on resizing here.

MichalPetryka and others added 2 commits September 21, 2023 23:06
…er/CborWriter.cs

Co-authored-by: Jeremy Barton <jbarton@microsoft.com>
@@ -205,11 +205,20 @@ private void EnsureWriteCapacity(int pendingCount)
throw new OverflowException();
Copy link
Member

Choose a reason for hiding this comment

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

Apropos, I wrote a lot of this code and it hindsight it seems like I didn't account for not inlining throws in hot-path methods. We should consider refactoring into throw helpers at some point, though clearly not in scope for this PR.

…er/CborWriter.cs

Co-authored-by: Eirik Tsarpalis <eirik.tsarpalis@gmail.com>
Copy link
Member

@eiriktsarpalis eiriktsarpalis left a comment

Choose a reason for hiding this comment

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

Thanks

@vcsjones
Copy link
Member

@MichalPetryka Can you please fix the merge conflict?

@MichalPetryka
Copy link
Contributor Author

Can you please fix the merge conflict?

Done

@eiriktsarpalis eiriktsarpalis merged commit d60f44a into dotnet:main Sep 25, 2023
@ghost ghost locked as resolved and limited conversation to collaborators Oct 25, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.Formats.Cbor community-contribution Indicates that the PR has been added by a community member
Projects
None yet
Development

Successfully merging this pull request may close these issues.

CborWriter should use doubling for buffer growth
5 participants