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 of TextSerializer #410

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

Stabzs
Copy link

@Stabzs Stabzs commented Mar 26, 2023

This PR reduces the allocations of TextSerializer by converting double/long values to byte arrays via intermediary buffers instead of creating intermediary strings. It does this by taking advantage of UftFormatters from the System.Memory assembly. All unit tests continue to pass as expected.

SerializationBenchmarks showed a 50% allocation reduction for CollectAndSerialize, while it brought CollectAndSerializeOpenMetrics down 66% into alignment with CollectAndSerialize. Since the runs of both benchmarks are bimodal, runtimes are difficult to compare although in my tests, they were unchanged and/or showed no significant differences.

Existing:

BenchmarkDotNet=v0.13.2, OS=Windows 10 (10.0.19043.1826/21H1/May2021Update)
Intel Core i7-8565U CPU 1.80GHz (Whiskey Lake), 1 CPU, 8 logical and 4 physical cores
.NET SDK=7.0.201
  [Host]  : .NET 7.0.3 (7.0.323.6910), X64 RyuJIT AVX2
  LongRun : .NET 7.0.3 (7.0.323.6910), X64 RyuJIT AVX2

Job=LongRun  IterationCount=100  LaunchCount=3  
WarmupCount=15  
Method Mean Error StdDev Median Gen0 Allocated
CollectAndSerialize 104.09 ms 3.845 ms 19.77 ms 102.44 ms 428.5714 2.07 MB
CollectAndSerializeOpenMetrics 99.72 ms 4.825 ms 24.37 ms 86.99 ms 500.0000 2.94 MB

Optimized:

BenchmarkDotNet=v0.13.2, OS=Windows 10 (10.0.19043.1826/21H1/May2021Update)
Intel Core i7-8565U CPU 1.80GHz (Whiskey Lake), 1 CPU, 8 logical and 4 physical cores
.NET SDK=7.0.201
  [Host]  : .NET 7.0.3 (7.0.323.6910), X64 RyuJIT AVX2
  LongRun : .NET 7.0.3 (7.0.323.6910), X64 RyuJIT AVX2

Job=LongRun  IterationCount=100  LaunchCount=3  
WarmupCount=15  
Method Mean Error StdDev Gen0 Allocated
CollectAndSerialize 95.12 ms 3.763 ms 18.97 ms 200.0000 1.01 MB
CollectAndSerializeOpenMetrics 94.29 ms 4.322 ms 22.22 ms 250.0000 1.01 MB

NOTE: there are potential opportunities to use UTF8 literals in other places to minimize allocations on startup, but that seems like a separate effort.

Stabzs added 2 commits March 26, 2023 14:49
…s to byte arrays via intermediary buffers instead of creating intermediary strings.
@Stabzs
Copy link
Author

Stabzs commented Apr 12, 2023

@sandersaares Is there anything you need me to take action on for this PR? As far as I can tell, the build issues are not related to my changes.

@slang25
Copy link

slang25 commented Apr 18, 2023

This looks really nice 👏 Hope we can see this in an upcoming release

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.

None yet

2 participants