oxc icon indicating copy to clipboard operation
oxc copied to clipboard

refactor(syntax): replace `nonmax` crate with zero-cost non-max types

Open overlookmotel opened this issue 1 year ago • 4 comments

Replace nonmax crate with own NonMax* types.

nonmax's NonMax* types work by storing the value internally as a NonZero*, and inverting the bits on every read/write (native_u32 = nonmax_u32 as u32 ^ u32::MAX). This reserves only a single illegal value, but has (small) runtime cost on every read/write.

The NonMax* types provided here make a different trade-off.

NonMaxU8 wraps an enum with a single illegal value (255). All the other types store the value as a number of u8s + a single NonMaxU8 as highest byte. i.e. they reserve all values which have 255 as the highest byte.

  • NonMaxU8 can represent the values 0 to 254.
  • NonMaxU16 can represent the values 0 to (255 << 8) - 1.
  • NonMaxU32 can represent the values 0 to (255 << 24) - 1.
  • NonMaxU64 can represent the values 0 to (255 << 56) - 1.
  • NonMaxU128 can represent the values 0 to (255 << 120) - 1.

We trade approx 0.4% of the legal range being unavailable, in return for zero runtime cost when reading or writing the values.

Linter snapshot change is because PreferHooksInOrder rule uses a hash map keyed by ScopeId. The hash of NonMaxU32 is now same as hash of u32, which alters the hash map iteration order. This does not change what errors are output, only the order they're output in.

overlookmotel avatar Sep 13 '24 12:09 overlookmotel

Your org has enabled the Graphite merge queue for merging into main

Add the label “0-merge” to the PR and Graphite will automatically add it to the merge queue when it’s ready to merge. Or use the label “hotfix” to add to the merge queue as a hot fix.

You must have a Graphite account and log in to Graphite in order to use the merge queue. Sign up using this link.

graphite-app[bot] avatar Sep 13 '24 12:09 graphite-app[bot]

  • #5760 Graphite 👈
  • main

This stack of pull requests is managed by Graphite. Learn more about stacking.

Join @overlookmotel and the rest of your teammates on Graphite Graphite

overlookmotel avatar Sep 13 '24 12:09 overlookmotel

CodSpeed Performance Report

Merging #5760 will degrade performances by 3.53%

Comparing 09-13-refactor_syntax_replace_nonmax_crate_with_zero-cost_non-max_types (5cd101d) with main (8d026e1)

Summary

❌ 1 regressions ✅ 28 untouched benchmarks

:warning: Please fix the performance issues or acknowledge them on CodSpeed.

Benchmarks breakdown

Benchmark main 09-13-refactor_syntax_replace_nonmax_crate_with_zero-cost_non-max_types Change
codegen[checker.ts] 20.6 ms 21.4 ms -3.53%

codspeed-hq[bot] avatar Sep 13 '24 12:09 codspeed-hq[bot]

-3% on some benchmarks. Zero cost my arse! I'll try to figure out why.

overlookmotel avatar Sep 13 '24 13:09 overlookmotel

Close as stale.

Boshen avatar Nov 25 '24 08:11 Boshen

Archived on overlookmotel/non-max-id-types branch.

overlookmotel avatar Nov 25 '24 12:11 overlookmotel