Skip to content

Specialize generated byte storage accessors - #20

Merged
RejectKid merged 1 commit into
masterfrom
specialize-generated-accessors
Jul 22, 2026
Merged

Specialize generated byte storage accessors#20
RejectKid merged 1 commit into
masterfrom
specialize-generated-accessors

Conversation

@RejectKid

Copy link
Copy Markdown
Owner

What changed

This combines the remaining generated-accessor optimizations into one change:

  • generate direct endian-aware access for byte-aligned Span<byte>, ReadOnlySpan<byte>, byte arrays, and byte InlineArrays
  • generate direct Span/InlineArray boolean masks and branch-based writes
  • eliminate MemoryMarshal.AsBytes for eligible byte InlineArray fields
  • specialize 11-, 12-, 24-, and 48-bit integral/enum fields with guarded 32/64-bit windows
  • retain no-inline exact-length fallbacks when a backing buffer is smaller than the preferred wide window
  • expand permanent benchmark coverage and the original-performance regression set from 74 to 88 methods
  • add randomized LSB/MSB, signed, read-only, InlineArray, and exact-length correctness coverage

Memory-backed booleans intentionally remain on BitPrimitives: focused measurements showed that repeatedly acquiring .Span made direct generated code slower. Span and byte-InlineArray booleans use the direct path.

Why

The generator knows storage kind, field width, offset, signedness, and bit order at compile time. Eligible layouts can avoid the general primitive dispatch/validation path while preserving the existing generated API and falling back safely for exact-size buffers.

Performance

Paired BenchmarkDotNet comparisons against merge commit 9179007:

  • 14-method ShortRun geometric mean: 2.36% faster, no regression beyond 5%
  • 7-method MediumRun decision set: 2.08% faster, no regression beyond 5%
  • Span boolean setter: 9.03% faster (MediumRun, confirmed)
  • 11-bit Memory getter: 5.20% faster (MediumRun)
  • 12-bit Memory getter: 1.09% faster (MediumRun)
  • 48-bit Memory getter: effectively unchanged (0.18% faster)
  • all measured operations remain allocation-free

Correctness and compatibility

  • no public API changes
  • exact-length 2-byte and 7-byte buffers use bounded cold fallbacks
  • both LSB and MSB paths are covered
  • signed extraction and enum paths are covered
  • read-only Span/Memory getters are covered

Validation

  • dotnet test BitsKit.Tests/BitsKit.Tests.csproj -c Release --no-restore
    • 4,589 passed on .NET 8
    • 4,589 passed on .NET 10
  • dotnet build BitsKit.sln -c Release --no-restore
  • dotnet pack BitsKit/BitsKit.csproj -c Release --no-build --no-restore
  • git diff --check
  • paired ShortRun and MediumRun regression harnesses against 9179007

@RejectKid
RejectKid marked this pull request as ready for review July 22, 2026 03:11
@RejectKid
RejectKid merged commit 4b1c9dc into master Jul 22, 2026
8 checks passed
@RejectKid
RejectKid deleted the specialize-generated-accessors branch July 22, 2026 03:12
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