surrealdb / surrealdb.net

SurrealDB SDK for .NET
https://surrealdb.com
Apache License 2.0
93 stars 18 forks source link

⚡️ use an InterpolatedStringHandler for Query string #118

Closed Odonno closed 2 months ago

Odonno commented 3 months ago

Thank you for submitting this pull request! We appreciate you spending the time to work on these changes.

What is the motivation?

Create and use a custom InterpolatedStringHandler for Query methods to improve performance. Also removed duplicated code from engines by reusing RawX method versions.

For a typical scenario, the benchmark confirms the performance improvements: more than 2x faster & slightly less memory allocation. See:

BenchmarkDotNet v0.13.10, Windows 11 (10.0.22631.3880/23H2/2023Update/SunValley3)
AMD Ryzen 9 5900X, 1 CPU, 24 logical and 12 physical cores
.NET SDK 8.0.400-preview.0.24324.5
  [Host]     : .NET 8.0.7 (8.0.724.31311), X64 RyuJIT AVX2
  DefaultJob : .NET 8.0.7 (8.0.724.31311), X64 RyuJIT AVX2

| Method                         | Count | Mode            | Mean         | Error      | StdDev     | Gen0   | Gen1   | Allocated |
|------------------------------- |------ |---------------- |-------------:|-----------:|-----------:|-------:|-------:|----------:|
| FormattableString              | 1     | Interpolation   |    570.19 ns |   3.096 ns |   2.586 ns | 0.0706 |      - |    1192 B |
| QueryInterpolatedStringHandler | 1     | Interpolation   |    259.67 ns |   1.456 ns |   1.216 ns | 0.0582 |      - |     976 B |
| FormattableString              | 1     | NoInterpolation |     18.70 ns |   0.132 ns |   0.117 ns | 0.0062 |      - |     104 B |
| QueryInterpolatedStringHandler | 1     | NoInterpolation |     26.12 ns |   0.309 ns |   0.289 ns | 0.0091 |      - |     152 B |
| FormattableString              | 100   | Interpolation   | 56,620.39 ns | 136.167 ns | 106.310 ns | 6.7749 | 1.2207 |  113656 B |
| QueryInterpolatedStringHandler | 100   | Interpolation   | 24,738.14 ns | 317.446 ns | 281.407 ns | 5.4932 | 1.0376 |   92056 B |
| FormattableString              | 100   | NoInterpolation |  1,337.70 ns |  19.816 ns |  16.547 ns | 0.2899 |      - |    4856 B |
| QueryInterpolatedStringHandler | 100   | NoInterpolation |  2,090.98 ns |  13.691 ns |  12.806 ns | 0.5760 | 0.0191 |    9656 B |

What does this change do?

Provide a description of what this pull request does.

What is your testing strategy?

Unit tests + benchmarks

Have you read the Contributing Guidelines?