Skip to content

perf(vm,interp,jit): native call-site cache and single-pass dispatch across backends - #70

Merged
esrrhs merged 2 commits into
masterfrom
cursor/native-call-fast-path-ebd9
Dec 2, 2026
Merged

esrrhs merged 2 commits into
masterfrom
cursor/native-call-fast-path-ebd9

Conversation

@esrrhs

@esrrhs esrrhs commented Dec 1, 2026

Copy link
Copy Markdown
Owner

Summary

Eliminates redundant argument packing, hash lookups, and boundary overhead on native function call hot paths across all three backends (JIT_INTERP, JIT_TCC, JIT_GCC):

  1. Single-pass native dispatch in CallByNameImpl (src/jit/vm.cpp)
    • Factored native dispatch into CallNativeEntry and routed !jit_func directly before JIT VarMulti packing, eliminating double argument flattening and wasted VarMulti::AllocTemp arena allocations on variadic native calls while preserving strict arity checks for fixed-arity natives.
    • Enabled FlCallCache population in FakeluaCallByNameCached for both JIT_GCC and JIT_TCC (resetting cache->version = 0 on miss), and routed already-cached kind == 2 fallbacks (e.g. VAR_MULTI expansion like select(i, ...)) directly to CallNativeEntry without redundant hash lookups.
  2. Inline call-site cache for variadic natives & TCC (src/compile/c_runtime_cov.h)
    • Updated FlCallNamed0..12 to serve both FAKELUA_JIT_TYPE == 1 (GCC) and FAKELUA_JIT_TYPE == 0 (TCC), and allowed cache->kind == 2 variadic native functions (cache->is_vararg || cache->expected_args == N) with no VAR_MULTI arguments to hit FakeluaCallNativeCached directly.
  3. Interpreter native call-site cache & zero-heap MAKEMULTI/COMBINE (src/interp/func_proto.h, src/interp/interpreter.cpp)
    • Added const NativeFuncEntry *native to InterpCallCache (sharing vm_version invalidation) and dispatched cached native calls directly in both CALLNAME and TAILNAME (InterpCallNativeDirect / InterpCallNativeSlow), preserving CurrentJit(), exact Lua source locations, and call stacks on exceptions.
    • Replaced std::vector<CVar> heap allocations in MAKEMULTI / COMBINE with direct stk + base pass-through (!HasBoxes) and a stack small-buffer fast path up to kMaxFunctionInputParams (HasBoxes).
  4. Propagate state->CurrentJit() in table.sort & xpcall (src/native/table/native_table.cpp, src/native/basic/native_basic.cpp, src/state/fakelua.cpp)
    • Replaced hardcoded JIT_TCC with state->CurrentJit() in table.sort's custom comparator and xpcall's error handler, while tightening DispatchCall ((!cl || IsGccClosure(cl))) so cross-backend TCC closures invoked from GCC remain guarded by RunWithJitErrorBoundary.
  5. Regression tests (test/test_native.cpp)
    • Added 4 tests covering cached vararg natives + VAR_MULTI expansion + MAKEMULTI/COMBINE with boxed locals, cached native error locations & call stacks (CALLNAME & TAILNAME), vm_version cache invalidation on native/Lua redefinition, and table.sort/xpcall across all 3 backends plus cross-backend TCC-in-GCC closures.

Verification

  • Release build (GCC 13.3.0 -O3 -DNDEBUG): 0 warnings in src/ and test/test_native.cpp.
  • flua cmd/test.lua: passed across --jit_type=0 (TCC), --jit_type=1 (GCC), and --jit_type=2 (INTERP).
  • unit_tests (with FAKELUA_HOME set): 1839 / 1862 passed (48 suites, including common.c_runtime_cov_matches_kCRuntimeHeader and all 4 new tests); the 23 failures are exclusively environmental MySQL (17) / Redis (6) integration tests with no local daemons on the VM.

Benchmarks (Master 358697f8 vs Branch)

Measured on Linux 6.12 (Intel Xeon, taskset -c 0 ./bench_mark --benchmark_min_time=0.2s --benchmark_repetitions=1, Release -O3 -DNDEBUG, PUC-Rio Lua 5.4.8):

Scenario Arg Lua 5.4.8 INTERP (base → branch) INTERP Gain TCC (base → branch) TCC Gain GCC (base → branch) GCC Gain
TableInsert 5000 173.04 us 654.89 us → 202.36 us 3.24x 131.82 us → 131.02 us 1.01x 25.24 us → 26.75 us 0.94x
MathMinMax 100000 8.80 ms 33.25 ms → 10.69 ms 3.11x 6.89 ms → 6.92 ms 0.99x 747.83 us → 746.77 us 1.00x
MathExpLog 100000 5.62 ms 21.77 ms → 7.64 ms 2.85x 7.16 ms → 7.18 ms 1.00x 659.44 us → 658.38 us 1.00x
MathTrig 100000 5.87 ms 20.81 ms → 7.79 ms 2.67x 9.11 ms → 9.06 ms 1.01x 949.64 us → 955.03 us 0.99x
TableRemove 5000 342.99 us 777.14 us → 295.21 us 2.63x 242.59 us → 241.95 us 1.00x 31.52 us → 31.53 us 1.00x
MathSqrt 100000 2.21 ms 7.55 ms → 3.11 ms 2.42x 2.43 ms → 2.49 ms 0.98x 152.31 us → 153.04 us 1.00x
StringFormat 500 48.67 us 80.59 us → 37.96 us 2.12x 21.42 us → 20.89 us 1.03x 3.33 us → 3.65 us 0.91x
StringChar 500 28.42 us 62.35 us → 30.18 us 2.07x 31.79 us → 31.60 us 1.01x 5.65 us → 5.56 us 1.02x
Variadic 1 215.3 ns 964.8 ns → 483.3 ns 2.00x 318.5 ns → 321.5 ns 0.99x 85.2 ns → 86.7 ns 0.98x
TableConcat 1000 112.44 us 107.63 us → 60.10 us 1.79x 45.24 us → 44.59 us 1.01x 14.16 us → 15.11 us 0.94x
StringFindPattern 1000 321.00 us 869.80 us → 489.35 us 1.78x 858.76 us → 516.34 us 1.66x 772.14 us → 325.66 us 2.37x
ToNumber 1 67.7 ns 179.0 ns → 102.4 ns 1.75x 124.1 ns → 122.2 ns 1.02x 73.8 ns → 79.4 ns 0.93x
ToString 500 135.4 ns 182.7 ns → 122.6 ns 1.49x 86.2 ns → 84.4 ns 1.02x 58.9 ns → 57.3 ns 1.03x
StringFindLiteral 10000 771.6 ns 525.8 ns → 415.6 ns 1.27x 513.6 ns → 410.0 ns 1.25x 487.6 ns → 374.2 ns 1.30x
StringGmatch 1000 441.49 us 710.76 us → 655.44 us 1.08x 558.08 us → 502.44 us 1.11x 387.57 us → 388.58 us 1.00x
Open in Web Open in Cursor 

cursoragent and others added 2 commits December 1, 2026 11:42
…across backends

Co-authored-by: esrrhs <esrrhs@163.com>
… PE and mark cold interp helpers noinline

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

❌ Patch coverage is 83.33333% with 25 lines in your changes missing coverage. Please review.
✅ Project coverage is 94.50%. Comparing base (358697f) to head (955004c).

Files with missing lines Patch % Lines
src/interp/interpreter.cpp 80.76% 15 Missing ⚠️
src/compile/c_runtime_cov.h 37.50% 5 Missing ⚠️
src/jit/vm.cpp 91.80% 5 Missing ⚠️
❗ Your organization needs to install the Codecov GitHub app to enable full functionality.
Additional details and impacted files
@@            Coverage Diff             @@
##           master      #70      +/-   ##
==========================================
- Coverage   94.55%   94.50%   -0.06%     
==========================================
  Files         130      130              
  Lines       32425    32482      +57     
==========================================
+ Hits        30659    30696      +37     
- Misses       1766     1786      +20     

☔ 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.

@esrrhs
esrrhs merged commit 97ddf33 into master Dec 2, 2026
9 checks passed
@esrrhs
esrrhs deleted the cursor/native-call-fast-path-ebd9 branch December 2, 2026 02:13
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