Skip to content

Optimize generated MSB scalar setters - #18

Merged
RejectKid merged 1 commit into
masterfrom
agent/optimize-generated-msb-setters
Jul 22, 2026
Merged

Optimize generated MSB scalar setters#18
RejectKid merged 1 commit into
masterfrom
agent/optimize-generated-msb-setters

Conversation

@RejectKid

Copy link
Copy Markdown
Owner

What changed

  • emit direct read-modify-write expressions for valid fixed-width integral-backed MSB setters
  • support generated integral, enum, and Boolean properties across 8-, 16-, 32-, and 64-bit backing fields
  • retain the existing BitPrimitives fallback for memory-backed fields, native integers, and invalid ranges
  • add four generated MSB setter benchmarks and include them in the original/current generator regression harness
  • add randomized equivalence tests, including partial-width and full-width boundary cases
  • update the README and changelog to describe direct LSB/MSB generated setters

Why

Generated MSB setters previously called the general-purpose BitPrimitives.Write*MSB methods even though the source generator knows the backing width, bit offset, and field width at compile time. The generated expression now contains the precomputed destination mask and only the required value mask, shift, byte-order operation, and assignment.

This primarily improves cold and Tier-0 generated code. Tier-1 already optimized the old helper chain effectively, so warmed-up throughput remains tied.

Performance

Representative Dry disassembly runs:

  • UInt32 MSB setter: 144.4 ns to 61.2 ns (57.6% lower single-run estimate); generated setter body shrank from 59 bytes with a helper call to 48 call-free bytes
  • UInt64 MSB setter: 210.6 ns to 151.3 ns (28.2% lower single-run estimate); the generated setter no longer calls the general-purpose helper

The statistically sampled Medium comparison confirms Tier-1 parity across UInt32, UInt64, Boolean, and enum setters: geometric mean +0.24%, with every result within tolerance and confidence intervals overlapping.

Validation

  • dotnet build BitsKit.sln -c Release --no-restore — succeeded with 0 warnings
  • net8 tests — 4,587 passed
  • net10 tests — 4,587 passed
  • focused Medium generated MSB setter comparison — no regression beyond 5%
  • full Dry original/current harness validation — all 70 comparable operations discovered and executed

@RejectKid
RejectKid marked this pull request as ready for review July 22, 2026 01:21
@RejectKid
RejectKid merged commit 93f3690 into master Jul 22, 2026
8 checks passed
@RejectKid
RejectKid deleted the agent/optimize-generated-msb-setters branch July 22, 2026 01:21
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