Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use a custom interpolation handler for paths (#266)
Motivation ---------- Improved performance and reduced heap allocations building URLs for requests. While parts of this could be backported to downlevel frameworks, it's generally not worth the effort, so it only applies to .NET 6 and newer. Modifications ------------- - Add `TrySerialize` to `LiteralSerializer` that writes to a `Span<char>` - Create a custom interpolated string handler for building URIs - Add an overload to `PathSegmentSerializer` that accepts a `PathSegmentInterpolatedStringHandler` - Use the new overload when building the `BuildUri` method when targeting .NET 6 and later - Add a benchmark project Breaking Changes -------------------- - `PathParser.ToInterpolatedStringExpression` and `PathSegment.ToInterpolatedStringContentSyntax` now take an interpreter that returns an `InterpolationSyntax` instead of an `ExpressionSyntax` Results ------- The benchmark below is for a relatively simple URI with two path segments, one a long and the other a GUID. The Build benchmark uses the overload that stackallocs an initialBuffer. BenchmarkDotNet v0.14.0, Windows 11 (10.0.26100.2033) Unknown processor .NET SDK 8.0.403 [Host] : .NET 8.0.10 (8.0.1024.46610), X64 RyuJIT AVX2 DefaultJob : .NET 8.0.10 (8.0.1024.46610), X64 RyuJIT AVX2 | Method | Mean | Error | StdDev | Ratio | Gen0 | Allocated | Alloc Ratio | |---------- |---------:|---------:|---------:|------:|-------:|----------:|------------:| | Serialize | 28.92 ns | 0.176 ns | 0.156 ns | 1.00 | 0.0216 | 272 B | 1.00 | | Build | 24.10 ns | 0.153 ns | 0.136 ns | 0.83 | 0.0108 | 136 B | 0.50 |
- Loading branch information