Skip to content

perf(interp): inline GETTABLE/SETTABLE hot paths for arr_ and quick_data_ - #74

Merged
cursor[bot] merged 1 commit into
masterfrom
cursor/interp-table-fast-path-96ea
Dec 2, 2026
Merged

cursor[bot] merged 1 commit into
masterfrom
cursor/interp-table-fast-path-96ea

Conversation

@esrrhs

@esrrhs esrrhs commented Dec 2, 2026

Copy link
Copy Markdown
Owner

Summary

Inlines the hot paths of VM_CASE(GETTABLE) and VM_CASE(SETTABLE) directly in the interpreter dispatch loop (src/interp/interpreter.cpp), with out-of-line noinline slow paths for misses, resizes, nil deletions, const/spec tables, float normalization, and errors. Bytecode encoding and instruction set are unchanged.

Changes

  • src/var/var_string.h: Added VarString::CachedHash() so the hot loop reads the precomputed ConstString djb2 hash (ConstString::Alloc always populates it) without pulling VarString::HashOf into InterpreterExecuteT.
  • src/interp/interpreter.cpp:
    • Added [[noreturn]] __attribute__((noinline, cold)) InterpThrowIndexError and zero-frame __attribute__((noinline)) InterpGetTableSlow / InterpSetTableSlow helpers that tail-call TableHelper::GetTableInt / GetTable / SetTable (avoiding stack-canary overhead and the double cross-TU hop through interp_rt::SetTable).
    • VM_CASE(GETTABLE):
      • Direct 1-based arr_ lookup ((uint64_t)k - 1 < tbl->arr_size_) for plain and const tables (spec tables never allocate arr_).
      • Direct quick_data_ short linear probe when spec_get == nullptr && spec_count == 0 && bucket_count_ == 0 for kInt keys and kStringId keys (hash-checked first; non-pointer-equal hash matches fall back to InterpGetTableSlow to handle dynamic VarString keys, cross-State StringIds, and djb2 collisions).
    • VM_CASE(SETTABLE) (non-const tables):
      • Direct in-bounds non-nil-to-non-nil arr_ overwrite (seq_len_ unchanged).
      • Direct sequential arr_ append when idx == arr_size_ && arr_size_ < arr_cap_ && count_ == 0 && v != nil, updating seq_len_ in O(1).
      • Direct quick_data_ kStringId non-nil update-in-place and append (count_ < QUICK_DATA_SIZE) when spec_set == nullptr && spec_count == 0 && bucket_count_ == 0.
    • Code-size impact stays compact: InterpreterExecuteT<false> grows from 0x4184 to 0x46f9 (+1,397 B, +8.3%) with zero regression on unrelated loops (Fibonacci, Sum).
  • test/test_table.cpp:
    • Added interp_table_fastpath_edge_cases_across_backends covering const table reads, arr_ growth & #t with/without holes, nil deletion/refill, float-exact-int (1.0/2.0/-0.0) & 1.5 keys, negative/zero/OOB/mininteger/maxinteger int keys, quick_data_ hits/misses/djb2 collisions ("ab" vs "bA")/dynamic-string aliasing/rehash, self-aliasing GETTABLE, boxed locals (InterpreterExecuteT<true>), ABI spec_count tables, and NativeObject spec tables (spec_get/spec_set).
    • Added interp_table_error_location_and_stack_across_backends verifying error messages, Lua source lines, and multi-frame call stacks for non-table, nil-key, NaN-key, and const-table errors across all 3 backends (INTERP, TCC, GCC).

Benchmarks (origin/master @ b686d2e4 vs this branch)

GCC 13.3.0 Release (-O3 -DNDEBUG), Linux x86_64, core-pinned (taskset -c 0), 3-rep median (--benchmark_min_time=0.2s --benchmark_repetitions=3), compared against PUC-Rio Lua 5.4.8:

Scenario Arg Lua 5.4.8 INTERP Base INTERP P4 Gain P4 / Lua
Vector3 1000000 55.42 ms 164.96 ms 57.61 ms 2.86x 1.04x
NestedTable 10000 1.03 ms 2.75 ms 1.41 ms 1.95x 1.37x
BubbleSort 200 390.02 us 1.00 ms 571.04 us 1.76x 1.46x
InsertionSort 200 270.47 us 672.88 us 439.94 us 1.53x 1.63x
TableCreate 5000 61.09 us 109.27 us 72.02 us 1.52x 1.18x
TableRemove 5000 376.34 us 287.05 us 230.29 us 1.25x 0.61x
BinarySearch 1000 211.21 us 452.13 us 432.19 us 1.05x 2.05x
Fibonacci 32 115.28 ms 324.74 ms 319.95 ms 1.01x 2.78x
Sum 5000000 16.99 ms 37.15 ms 36.96 ms 1.01x 2.18x
HashLookup 1000 130.47 us 84.46 us 85.36 us 0.99x 0.65x
HashInsert 1000 180.09 us 92.29 us 95.79 us 0.96x 0.53x
TableInsert 5000 178.04 us 203.66 us 210.61 us 0.97x 1.18x

(Note: BinarySearch is dominated by two GETGLOBAL hash lookups of search_init_vals per inner-loop iteration; HashInsert / TableInsert / HashLookup deltas are within the 2–3% Lua baseline drift observed during the run.)

Verification

  • Release build (-O3 -DNDEBUG): 0 warnings in src/ and test/test_table.cpp.
  • flua cmd/test.lua: PASS (exit 0) across all 3 backends (--jit_type=0 TCC, --jit_type=1 GCC, --jit_type=2 INTERP).
  • unit_tests (FAKELUA_HOME=/agent/repos/fakelua): 1860 / 1883 passed (including common.c_runtime_cov_matches_kCRuntimeHeader and all 41 test_table.* tests; only the 23 environmental integration tests requiring local MySQL/Redis daemons fail locally).
Open in Web Open in Cursor 

…data_

Co-authored-by: esrrhs <esrrhs@163.com>
@codecov-commenter

Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 94.54%. Comparing base (b686d2e) to head (e426e84).
❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@            Coverage Diff             @@
##           master      #74      +/-   ##
==========================================
+ Coverage   94.52%   94.54%   +0.01%     
==========================================
  Files         130      130              
  Lines       32588    32669      +81     
==========================================
+ Hits        30803    30886      +83     
+ Misses       1785     1783       -2     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@cursor
cursor Bot merged commit df62626 into master Dec 2, 2026
9 checks passed
@cursor
cursor Bot deleted the cursor/interp-table-fast-path-96ea branch December 2, 2026 06:09
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.

3 participants