Closed lahma closed 4 days ago
It seems that it can reduce extra memory allocation that caused by string.Split
and string.Trim()
by using Span-based API.
Currently it seems there is no SplitAny
API that can be used for this purpose. (See: https://github.com/dotnet/runtime/issues/934
)
Instead. it can be implemented by simple loop logics.
Example of Benchmark results
Method | Mean | Error | StdDev | Ratio | RatioSD | Gen0 | Gen1 | Allocated | Alloc Ratio |
---|---|---|---|---|---|---|---|---|---|
Default | 163.41 ms | 1.960 ms | 1.833 ms | 1.00 | 0.00 | 101250.0000 | 32250.0000 | 430256464 B | 1.000 |
Optimized1(string) | 132.51 ms | 2.489 ms | 2.329 ms | 0.81 | 0.02 | 61500.0000 | 500.0000 | 257720518 B | 0.599 |
Optimized2(SearchValue) | 133.73 ms | 2.538 ms | 2.607 ms | 0.82 | 0.02 | 61500.0000 | 500.0000 | 257720518 B | 0.599 |
Optimized3(Span-based) | 80.74 ms | 1.560 ms | 1.602 ms | 0.49 | 0.01 | - | - | 313 B | 0.000 |
Benchmark code (LINQPad)
That implementation @filzrev did is probably worth a PR, great work with benchmarks and the implementation! I was afraid to change the "readable" behavior of the current implementation as I wasn't sure about test coverage.
A separate benchmark project could also be a good addition to the docfx solution to host experiments.
SearchValues
for special chars lookup on NET 8