Lightning.NET icon indicating copy to clipboard operation
Lightning.NET copied to clipboard

MDBValue Optimizations

Open CoreyKaylor opened this issue 1 month ago • 0 comments

Optimizations suggested by @sebastienros in https://github.com/CoreyKaylor/Lightning.NET/issues/191
I've only compared to v0.20.0, not the previous comparer implementations. The current branch outperforms the baseline v0.20.0 between 5% and 20%

Separately, it feels like there might be a more optimal route for sorted Guid as well (which I assume is a common key scenario). I'll do a little bit of digging, but if anyone knows something I don't already feel free to share.

Memory

  • Allocation-free comparers confirmed - constant 56B overhead regardless of operation count (100 ops → 56B, 10000 ops → 56-59B)

Read Performance - All Comparers (1000 ops, 64B values)

Comparer Time vs Native
LengthOnly 24.9 μs -75%
ReverseSignedInt 99.6 μs -1%
Default (Native) 100.8 μs baseline
SignedInt 103.0 μs +2%
ReverseUnsignedInt 105.8 μs +5%
UnsignedInt 107.3 μs +6%
ReverseBitwise 110.8 μs +10%
Bitwise 113.2 μs +12%
Utf8String 114.1 μs +13%
ReverseLength 118.4 μs +17%
Length 119.4 μs +18%
ReverseUtf8String 163.9 μs +63%
HashCode 172.1 μs +71%

Write Performance - All Comparers (1000 ops, 64B values)

Comparer Time vs Native
LengthOnly 82.3 μs -65%
UnsignedInt 200.4 μs -15%
SignedInt 202.5 μs -14%
ReverseLength 219.0 μs -7%
ReverseSignedInt 221.7 μs -6%
ReverseUnsignedInt 224.8 μs -5%
ReverseBitwise 228.6 μs -3%
Default (Native) 236.5 μs baseline
Length 237.6 μs +0.5%
Utf8String 237.8 μs +0.5%
Bitwise 294.5 μs +25%
ReverseUtf8String 301.4 μs +27%
HashCode 314.7 μs +33%

Integer Keys (10000 ops, 4-byte keys)

Comparer Time vs Native
SignedInt 307 μs -81%
UnsignedInt 312 μs -81%
ReverseSignedInt 1,012 μs -38%
ReverseUnsignedInt 1,007 μs -38%
Default (Native) 1,633 μs baseline

Notes

  • LengthOnly is fastest but only compares by length (no content comparison)
  • SignedInt/UnsignedInt provide major gains for integer keys
  • HashCode is consistently slowest across all scenarios
  • Most custom comparers perform within ±15% of native for general byte data

CoreyKaylor avatar Dec 16 '25 17:12 CoreyKaylor