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

Reduce Allocations #821

Merged
merged 16 commits into from
Apr 28, 2024
Merged

Reduce Allocations #821

merged 16 commits into from
Apr 28, 2024

Conversation

iamcarbon
Copy link
Collaborator

This PR:

  • Introduces a ValueStringBuilder
  • Makes NumericTokenizer & PlainTextTokenizer thread-safe & reusable)
  • Replaces the ListPool uses with ArrayPoolBufferWriter
  • Seals various classes
  • Eliminates various allocations
  • Remove the unused (and allocating) FromOctalInt helper

@iamcarbon
Copy link
Collaborator Author

@BobLd Ready for review.

Comment on lines +16 to +17
private static readonly PlainTokenizer PlainTokenizer = new PlainTokenizer();
private static readonly NumericTokenizer NumericTokenizer = new NumericTokenizer();
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

These are thread safe now and can be reused.

@BobLd BobLd self-requested a review April 21, 2024 13:37
@BobLd
Copy link
Collaborator

BobLd commented Apr 21, 2024

@iamcarbon UglyToad.PdfPig.Fonts is missing the following:

<ItemGroup Condition="'$(TargetFramework)'=='netstandard2.0' or '$(TargetFramework)'=='net462' or '$(TargetFramework)'=='net471'">
	<PackageReference Include="System.Memory" Version="4.5.5" />
</ItemGroup>

@BobLd
Copy link
Collaborator

BobLd commented Apr 21, 2024

Can you give more background about ValueStringBuilder? When you should and should not it be used over StringBuilder?

@iamcarbon
Copy link
Collaborator Author

Can you give more background about ValueStringBuilder? When you should and should not it be used over StringBuilder?

There's a few caveats to using ValueStringBuilder:

  • it's a ref struct and needs to be disposed (by using "using", calling dispose, or calling ToString()). If used across functions, it needs to be passed by ref. If you miss the ref, it will create a copy of it's internal state, and can result in a double return.

  • it can be slower for building large strings when a proper buffer size (hint) isn't provided. StringBuilder uses a linked list behind the scenes, while ValueStringBuilder double's its internal buffer and copies.

  • it doesn't support advanced StringBuilder uses like Replace or Insert.

With the caveats mentioned:

  • it's allocation free, and allows you to use the backing buffer directly to avoid materializing a string.

  • it can also be provided with stack memory for it's initial buffer, which avoids the overhead in using the array pool if the written bytes fit.

I'd make a case for using it nearly everywhere where the values are small or we are fairly confident in choosing the initial buffer size, and that doesn't make any use of advanced StringBuilder cases. It makes a nice dent in reducing allocations.

@BobLd BobLd merged commit 7f42a8d into UglyToad:master Apr 28, 2024
1 check passed
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