Skip to content

fix(lua): unify numeric keys and correct integer arithmetic semantics - #77

Merged
wolfy-j merged 3 commits into
mainfrom
fix/numeric-key-identity
Oct 21, 2026
Merged

wolfy-j merged 3 commits into
mainfrom
fix/numeric-key-identity

Conversation

@wolfy-j

@wolfy-j wolfy-j commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

Equal integer and integral-float keys must address the same table entry, and compile-time arithmetic must preserve the numeric value and subtype that Lua specifies. This PR fixes numeric key identity across lookup, assignment, deletion, and next, and corrects both the compiler and VM numeric paths.

Arithmetic corrections:

  • Preserve integer subtype, precision, and wrapping for folded addition, subtraction, multiplication, modulo, and unary negation.
  • Keep mixed/float floor division floating-point: 7.0 // 2.5 evaluates to 2.0, not integer 3.
  • Use zero-filling right shifts, including negative shift counts and full-word/out-of-range counts: (0 - 1) >> 1 yields 9223372036854775807.
  • Require exact, in-range integer conversion for bitwise operands; 3.5 & 1, non-finite values, and out-of-range floats raise execution-time errors instead of truncating.
  • Preserve integer divide/modulo-by-zero errors at execution time and full-width integer boundary behavior.
  • Compare integers and mixed numeric operands exactly, including adjacent integers above 2^53. Make rawequal(1, 1.0) true while continuing to bypass __eq.

Shared numeric helpers keep folding and runtime rules aligned. The new tests assert independent expected values/types from Lua 5.3 sections 3.4.1–3.4.3; they do not merely compare two implementations that could share a defect. Representative results were also checked with the local reference Lua 5.4 interpreter, whose relevant numeric rules agree. This remains the existing hybrid VM; the PR does not claim complete Lua 5.3 compatibility for every operator/metamethod path.

Profiling found and removed a lookup allocation in the original PR: an integral-float hash read allocated 8 B / 1 allocation per operation. Numeric normalization now inlines into the read path, keeping temporary lookup keys on the stack. A regression test verifies 0 allocations for both RawGet and RawGetH. Dense integer/float reads also remain allocation-free. Benchmark and CPU/allocation-profile comparisons cover numeric lookup, constant compilation, bitwise operations, division, and representative VM workloads; five final-head benchmark repetitions are complete. Integral-float hash reads went from 8 B / 1 allocation to 0 B / 0 allocations; median timing was 40.44 ns before and 27.20 ns after. The key-normalization allocation hotspot disappeared from the final profile. Profiles measure allocation volume, not proof of global leak freedom. Shared-host timing varies, so allocation counts are the stronger evidence.

Validation on the revised head:

  • Focused numeric/table tests pass, including the independently specified arithmetic/error cases.
  • All ordinary root VM tests and fixture execution phases pass with race detection.
  • An additional 11,000 compile/execute/table-clear cycles on a reused state showed stable post-GC heap (~1.19 MB, 65,971–65,983 live objects). No accumulating retained heap was observed in this workload.
  • Pinned golangci-lint v2.8.0 reports zero issues.
  • Broader package race run passed; after the final equality change, the root VM and fixture execution race suites passed again.
  • 512 deterministic differential cases against Lua 5.4 passed (seed 77053), comparing numeric subtype/value or error class for folded and variable expressions. Signed-zero distinctions and error wording are excluded.
  • All 12 hosted checks passed on final head 71c73b01c4de248401e91c5a6aca73ba2abdad65: Linux, Windows, all seven race shards, fuzz, lint, and benchmarks.

Compatibility: integer and integral-float keys identify one entry; non-array integer accessors retain full width, including zero/negative keys. Integer numeric subtypes and precise results are now preserved. Fractional bitwise conversion, arithmetic right shifts, and truncating floating floor division were incorrect behavior and are intentionally corrected. No public method signature or persistent schema changes.

Overlap with #60: this revision now fixes unary integer negation and numeric shift behavior in both compilation and VM execution. Its future integration should retain these shared numeric rules instead of reintroducing the older conversion paths. No tick-preemption changes are included.

Additional timing check: five alternating baseline/final rounds did not reproduce the initial multi-fold slowdowns. Median integral-float hash lookup was 53.27 → 21.44 ns; dense reads and compilation also measured lower. Bitwise AND was 37.14 → 35.93 μs per benchmark iteration, OR 33.67 → 38.36 μs, and XOR 32.50 → 34.75 μs (1,000 Lua operations each). These small bitwise overheads accompany exact operand validation; all three still allocate zero bytes. Shared-host timings are variable and these are observations, not a statistical guarantee.

@wolfy-j
wolfy-j requested a review from skhaz October 21, 2026 15:25
@wolfy-j wolfy-j changed the title fix: unify numeric table keys and preserve folded integers fix(lua): unify numeric keys and correct integer arithmetic semantics Oct 21, 2026
@wolfy-j
wolfy-j merged commit c1bdf95 into main Oct 21, 2026
12 checks passed
@wolfy-j
wolfy-j deleted the fix/numeric-key-identity branch November 10, 2026 02:04
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.

2 participants