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

Introduce StringBuilderPool #646

Merged
merged 1 commit into from
Nov 15, 2021
Merged

Conversation

lahma
Copy link
Contributor

@lahma lahma commented Nov 13, 2021

To reduce memory allocations and improve scanning speed (less GC) I've introduced StringBuilder pooling. This is basically the same code we've used successfully in Jint and Esprima.

StringBuilderPool returns a disposable read-only struct that tracks lifetime and we access the rented builder from it.

I also sealed OrderedDictionary and made sure it's accessed via concrete type in fields to avoid interface dispatch.

Benchmark Code

Uses the large YAML from issue #519 . I noticed you don't have a benchmark project currently so I'm running my local one.

[MemoryDiagnoser]
public class YamlStreamBenchmark
{
    private string yamlString = "";

    [GlobalSetup]
    public void Setup()
    {
        yamlString = File.ReadAllText(@"c:\work\saltern.yml");
    }

    [Benchmark]
    public void Load()
    {
        var yamlStream = new YamlStream();
        yamlStream.Load(new StringReader(yamlString));
    }
}

Results

BenchmarkDotNet=v0.13.1, OS=Windows 10.0.22000
AMD Ryzen 9 5950X, 1 CPU, 32 logical and 16 physical cores
.NET SDK=6.0.100
  [Host]     : .NET 6.0.0 (6.0.21.52210), X64 RyuJIT
  DefaultJob : .NET 6.0.0 (6.0.21.52210), X64 RyuJIT

YamlStreamBenchmark

Diff Method Mean Error Gen 0 Gen 1 Gen 2 Allocated
Old Load 97.21 ms 0.592 ms 5833.3333 2833.3333 1000.0000 81 MB
New 83.64 ms (-14%) 0.319 ms 3500.0000 (-40%) 1500.0000 (-47%) 500.0000 (-50%) 53 MB (-35%)

@aaubry
Copy link
Owner

aaubry commented Nov 15, 2021

Very nice. Thanks!

@aaubry aaubry merged commit 961d6fc into aaubry:master Nov 15, 2021
@lahma lahma deleted the string-builder-pool branch November 15, 2021 10:11
@aaubry
Copy link
Owner

aaubry commented Jul 23, 2022

This feature has been released in version 12.0.0.

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