Skip to content
This repository has been archived by the owner on Mar 27, 2024. It is now read-only.

Use stackalloc to reduce allocations on NewUlid #9

Merged
merged 1 commit into from
Mar 27, 2024

Conversation

andrewjsaid
Copy link
Contributor

Benchmark:

using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
using CSharp.Ulid;

BenchmarkSwitcher
    .FromAssembly(typeof(Program).Assembly)
    .Run(new[] { "-i" });

[MemoryDiagnoser]
public class Benchmarks
{
    [Benchmark]
    public Ulid NewUlid() => Ulid.NewUlid();
}

Before:


BenchmarkDotNet v0.13.12, Windows 11 (10.0.22631.3296/23H2/2023Update/SunValley3)
AMD Ryzen 9 5950X, 1 CPU, 32 logical and 16 physical cores
.NET SDK 8.0.100
  [Host] : .NET 8.0.0 (8.0.23.53103), X64 RyuJIT AVX2

Toolchain=InProcessEmitToolchain  

Method Mean Error StdDev Gen0 Allocated
NewUlid 50.55 ns 0.526 ns 0.492 ns 0.0024 40 B

After:


BenchmarkDotNet v0.13.12, Windows 11 (10.0.22631.3296/23H2/2023Update/SunValley3)
AMD Ryzen 9 5950X, 1 CPU, 32 logical and 16 physical cores
.NET SDK 8.0.100
  [Host] : .NET 8.0.0 (8.0.23.53103), X64 RyuJIT AVX2

Toolchain=InProcessEmitToolchain  

Method Mean Error StdDev Allocated
NewUlid 43.51 ns 0.259 ns 0.243 ns -

@mcb2001 mcb2001 merged commit 56032e6 into mcb2001:master Mar 27, 2024
1 check passed
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants