Repository navigation
fix(lua): unify numeric keys and correct integer arithmetic semantics - #77
Merged
Merged
Conversation
skhaz
approved these changes
Oct 21, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:
7.0 // 2.5evaluates to2.0, not integer3.(0 - 1) >> 1yields9223372036854775807.3.5 & 1, non-finite values, and out-of-range floats raise execution-time errors instead of truncating.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
RawGetandRawGetH. 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:
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.