Skip to content

Conversation

@antrv
Copy link

@antrv antrv commented Aug 5, 2025

Description of changes

Accessing ReadOnlySpan and Span in C# always involves boundary checks. To improve the performance, references and reference arithmetic can be used instead.

My changes improve the performance approximately by 35% on my AMD CPU.

The benchmarks are done using BenchmarkDotNet library.

BenchmarkDotNet v0.15.2, Windows 11 (10.0.26100.4652/24H2/2024Update/HudsonValley)
Unknown processor
.NET SDK 9.0.302
  [Host]     : .NET 9.0.7 (9.0.725.31616), X64 RyuJIT AVX-512F+CD+BW+DQ+VL+VBMI
  DefaultJob : .NET 9.0.7 (9.0.725.31616), X64 RyuJIT AVX-512F+CD+BW+DQ+VL+VBMI


| Method  | Mean     | Error   | StdDev   | Median   |
|-------- |---------:|--------:|---------:|---------:|
| OldCode | 156.3 ms | 6.23 ms | 17.87 ms | 149.9 ms |
| NewCode | 109.1 ms | 4.03 ms | 11.89 ms | 106.6 ms |

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.

1 participant