diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 1e16d1aa..40374c73 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -86,8 +86,10 @@ jobs: - name: Setup TCC if: matrix.build_type == 'asan' || matrix.build_type == 'cov' run: | - git clone https://github.com/TinyCC/tinycc.git + git clone --filter=blob:none --no-checkout https://github.com/TinyCC/tinycc.git cd tinycc + git fetch --depth 1 origin 0fb54300b56512754221d80adda85ddb9815bceb + git checkout --detach 0fb54300b56512754221d80adda85ddb9815bceb ./configure --extra-cflags="-fPIC" make -j$(nproc) sudo make install @@ -158,14 +160,14 @@ jobs: # Select CMake flags based on build type case "${{ matrix.build_type }}" in debug) - CMAKE_FLAGS="" + CMAKE_FLAGS="-DCMAKE_BUILD_TYPE=Debug" ;; release) CMAKE_FLAGS="-DCMAKE_BUILD_TYPE=Release" ;; asan) CMAKE_FLAGS="-DUSE_ASAN=ON" - export ASAN_OPTIONS=detect_leaks=0 + export ASAN_OPTIONS=detect_leaks=1:halt_on_error=1 ;; cov) CMAKE_FLAGS="-DUSE_COV=ON" @@ -184,7 +186,7 @@ jobs: # Step-local exports from Build do not carry over to this step. export CPM_SOURCE_CACHE=$HOME/.cache/CPM if [ "${{ matrix.build_type }}" = asan ]; then - export ASAN_OPTIONS=detect_leaks=0 + export ASAN_OPTIONS=detect_leaks=1:halt_on_error=1 fi # Run all tests (including MySQL / Redis integration tests) # Must run from test/lua so relative Lua file paths resolve correctly diff --git a/.github/workflows/build_with_windows.yml b/.github/workflows/build_with_windows.yml index 91f99a61..1c0ac9b3 100644 --- a/.github/workflows/build_with_windows.yml +++ b/.github/workflows/build_with_windows.yml @@ -22,9 +22,8 @@ jobs: uses: actions/cache@v4 with: path: .cache/ccache - key: windows-ccache-${{ github.ref }}-${{ github.sha }} + key: windows-ccache-${{ github.ref }} restore-keys: | - windows-ccache-${{ github.ref }}- windows-ccache- - name: Cache CPM dependencies diff --git a/.github/workflows/fuzz.yml b/.github/workflows/fuzz.yml index 7535789b..6a81ea97 100644 --- a/.github/workflows/fuzz.yml +++ b/.github/workflows/fuzz.yml @@ -85,7 +85,9 @@ jobs: ok=0 for i in 1 2 3 4 5; do rm -rf tinycc - if git clone --depth 1 https://github.com/TinyCC/tinycc.git; then ok=1; break; fi + if git clone --filter=blob:none --no-checkout https://github.com/TinyCC/tinycc.git && + git -C tinycc fetch --depth 1 origin 0fb54300b56512754221d80adda85ddb9815bceb && + git -C tinycc checkout --detach 0fb54300b56512754221d80adda85ddb9815bceb; then ok=1; break; fi echo "git clone tinycc failed (attempt $i), retrying..." sleep $((i * 8)) done diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 88bb8d71..78eebf69 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -209,7 +209,7 @@ jobs: uses: actions/cache@v4 with: path: .cache/ccache - key: windows-release-ccache-${{ github.sha }} + key: windows-release-ccache restore-keys: | windows-release-ccache- windows-ccache- diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 00000000..ccdb146d --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,32 @@ +# Changelog + +## 2.1.0 + +This release covers the work after v2.0.0. The public ABI stays at `SOVERSION` 2. + +### Runtime and language + +- Runtime errors on TCC, GCC, and the interpreter include the Lua source location and a Lua call stack. +- Syntax diagnostics keep their own file, line, and column, including multiline strings and unfinished short strings. +- Fixed-arity calls, assignment, comparison, `for` loops, and the numeric/string/table libraries follow the Lua 5.4 behavior FakeLua claims to support. +- File-level function bindings cannot be replaced after initialization. File-level locals stay const. +- `string.find` / `match` / `gmatch` / `gsub` use the Lua 5.4 pattern engine. +- Tables have a contiguous array part. `json.encode` rejects holes in an integer-key array. +- `flua` always enters through package `main` and calls `init`, `update`, and `fini`. + +### Performance + +- GCC JIT caches private shared-library images, publishes them atomically, and on POSIX opens a cache hit without copying it again. +- Hot generated code uses an inline call-site cache, an O(1) active source location, and safer captured-variable access. +- The interpreter keeps its runtime state on `State`, speeds up named calls and exact tail calls, and caches the resolved target of a named call site. +- Benchmark notes were remeasured on 2026-11-30. Named interpreter calls are much closer to Lua than the old 6–8x figures, and `string.gsub` is documented as a Lua 5.4 pattern. +- Creating an interpreter closure no longer allocates a heap vector for the common small upvalue count. + +### Build, coverage, and CI + +- `CMAKE_BUILD_TYPE=Debug` compiles with assertions enabled and `-O0`. Other configurations, including an unset build type, stay `-O3 -DNDEBUG`. The Linux `debug` CI job passes `Debug`. +- The AddressSanitizer CI job reports leaks at process exit. +- `src/compile/c_runtime_cov.h` is the only runtime body. CMake generates the embedded string used by the JIT and by coverage line mapping. +- TinyCC is pinned to commit `0fb54300b56512754221d80adda85ddb9815bceb`. +- Fuzz targets compile and, for the differential target, execute the interpreter as well as GCC. +- The Windows ccache key no longer includes the commit SHA, so later commits on the same branch can reuse it. diff --git a/CMakeLists.txt b/CMakeLists.txt index 8299964a..a2a72a05 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,7 +1,7 @@ cmake_minimum_required(VERSION 3.5 FATAL_ERROR) set(CMAKE_POLICY_VERSION_MINIMUM 3.5) -project(fakelua VERSION 2.0.0 LANGUAGES CXX) +project(fakelua VERSION 2.1.0 LANGUAGES CXX) set(CMAKE_CXX_STANDARD 23) @@ -21,8 +21,20 @@ set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE) set(ENV{FAKELUA_HOME} "${CMAKE_CURRENT_SOURCE_DIR}/") -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wno-unused-local-typedefs -Wno-unused-function -DNDEBUG -g3 -ggdb3 -fPIC") -set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -DNDEBUG -g3 -ggdb3 -fPIC") +# Debug keeps assertions (no NDEBUG) and -O0. Every other configuration, +# including an unset CMAKE_BUILD_TYPE, stays -O3 -DNDEBUG so local and CI +# release/asan builds do not change. Coverage also forces -O0. +set(FAKELUA_DEBUG_BUILD OFF) +if (CMAKE_BUILD_TYPE STREQUAL "Debug") + set(FAKELUA_DEBUG_BUILD ON) +endif () + +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wno-unused-local-typedefs -Wno-unused-function -g3 -ggdb3 -fPIC") +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -g3 -ggdb3 -fPIC") +if (NOT FAKELUA_DEBUG_BUILD) + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DNDEBUG") + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DNDEBUG") +endif () option(USE_ASAN "use asan" OFF) if (USE_ASAN) @@ -37,24 +49,30 @@ if (USE_COV) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fprofile-arcs -ftest-coverage -fprofile-update=atomic") endif () -IF (APPLE) - if (USE_COV) - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O0 -Wno-non-pod-varargs") - set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O0") - else () - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3 -Wno-non-pod-varargs") - set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O3") - endif () -ELSE () - # Windows (MinGW) and Linux share the same policy: -O0 only for coverage builds. - if (USE_COV) - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O0") - set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O0") - else () - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3") - set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O3") - endif () -ENDIF () +if (APPLE) + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-non-pod-varargs") +endif () + +if (FAKELUA_DEBUG_BUILD OR USE_COV) + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O0") + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O0") +else () + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3") + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O3") +endif () + +find_program(FAKELUA_PYTHON NAMES python3 python REQUIRED) +set(FAKELUA_RUNTIME_EMBED_INC "${CMAKE_BINARY_DIR}/generated/c_runtime_embed.inc") +set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS + "${CMAKE_CURRENT_SOURCE_DIR}/src/compile/c_runtime_cov.h" + "${CMAKE_CURRENT_SOURCE_DIR}/cmake/gen_c_runtime_embed.py") +execute_process( + COMMAND "${FAKELUA_PYTHON}" + "${CMAKE_CURRENT_SOURCE_DIR}/cmake/gen_c_runtime_embed.py" + "${CMAKE_CURRENT_SOURCE_DIR}/src/compile/c_runtime_cov.h" + "${FAKELUA_RUNTIME_EMBED_INC}" + COMMAND_ERROR_IS_FATAL ANY) +include_directories("${CMAKE_BINARY_DIR}/generated") find_program(CCACHE_PROGRAM ccache) if (CCACHE_PROGRAM) diff --git a/README.md b/README.md index 7dd3c4c1..14fb6d7c 100644 --- a/README.md +++ b/README.md @@ -91,7 +91,7 @@ static CVar fib_dispatcher(CVar n_var) { } ``` -With recursive Fibonacci (n=32) as an example, the GCC backend is **36.6x** faster than Lua 5.4, and the TCC backend is **11.2x** faster (see [benchmark/README.md](benchmark/README.md) / [中文](benchmark/README.zh.md)). +With recursive Fibonacci (n=32) as an example, the GCC backend is **53.5x** faster than Lua 5.4 on the 2026-11-30 run, and the TCC backend is **1.3x** faster (see [benchmark/README.md](benchmark/README.md) / [中文](benchmark/README.zh.md)). ### Table Struct Specialization @@ -357,14 +357,14 @@ Comparing Lua 5.4, FakeLua TCC, FakeLua GCC across 11 algorithms (Release `-O3` | Algorithm (typical params) | Lua 5.4 | FakeLua TCC | FakeLua GCC | |---|---|---|---| -| Fibonacci n=32 | 297.9 ms | 26.7 ms (**11.2x**↑) | 6.8 ms (**36.6x**↑) | -| Sum n=5000000 | 33.9 ms | 18.4 ms (**1.8x**↑) | 1.1 ms (**30.4x**↑) | -| Popcount n=100000 | 18.2 ms | 3.1 ms (**5.9x**↑) | 488.0 μs (**37.3x**↑) | -| BubbleSort n=200 | 1.5 ms | 3.3 ms (0.45x) | 738.8 μs (**1.9x**↑) | -| Sieve n=5000 | 353.4 μs | 1.0 ms (0.34x) | 219.3 μs (**1.8x**↑) | -| FloatPoly n=1000000 | — | — | **34.9x**↑ (浮点特化,GCC 2x 快于 C++) | - -> TCC is generally faster than Lua for pure computation; in Table-operation-heavy scenarios, Table struct specialization gives both GCC and TCC a significant boost. Full data available in [benchmark/README.md](benchmark/README.md) / [中文](benchmark/README.zh.md). +| Fibonacci n=32 | 290.1 ms | 222.4 ms (**1.3x**) | 5.4 ms (**53.5x**) | +| Sum n=5000000 | 33.8 ms | 16.2 ms (**2.1x**) | 2.0 ms (**16.7x**) | +| Popcount n=100000 | 17.5 ms | 3.2 ms (**5.6x**) | 1.0 ms (**16.9x**) | +| BubbleSort n=200 | 1.5 ms | 1.5 ms (1.0x) | 159.7 μs (**9.6x**) | +| Sieve n=5000 | 334.1 μs | 467.2 μs (0.72x) | 62.9 μs (**5.3x**) | +| FloatPoly n=1000000 | 91.2 ms | 13.8 ms (**6.6x**) | 3.7 ms (**24.4x**, matches C++) | + +> On this 2026-11-30 run TCC is only slightly ahead of Lua on Fibonacci and slower on Sieve. GCC still leads the numeric cases, and FloatPoly matches handwritten C++. Full data is in [benchmark/README.md](benchmark/README.md) / [中文](benchmark/README.zh.md). ## C++ API Reference diff --git a/README.zh.md b/README.zh.md index db0e5662..d9825027 100644 --- a/README.zh.md +++ b/README.zh.md @@ -91,7 +91,7 @@ static CVar fib_dispatcher(CVar n_var) { } ``` -以递归 Fibonacci(n=32)为例,GCC 后端比 Lua 5.4 快 **36.6x**,TCC 后端快 **11.2x**(详见 [benchmark/README.zh.md](benchmark/README.zh.md) / [English](benchmark/README.md))。 +以递归 Fibonacci(n=32)为例,2026-11-30 这次测量里 GCC 后端比 Lua 5.4 快 **53.5x**,TCC 后端快 **1.3x**(详见 [benchmark/README.zh.md](benchmark/README.zh.md) / [English](benchmark/README.md))。 ### Table 结构体特化(Table Specialization) @@ -355,14 +355,14 @@ FakeLua 还会同时安装 `fakelua.pc`,支持通过 `pkg-config` 使用。 | 算法(典型参数) | Lua 5.4 | FakeLua TCC | FakeLua GCC | |---|---|---|---| -| Fibonacci n=32 | 297.9 ms | 26.7 ms(**11.2x**↑) | 6.8 ms(**36.6x**↑) | -| Sum n=5000000 | 33.9 ms | 18.4 ms(**1.8x**↑) | 1.1 ms(**30.4x**↑) | -| Popcount n=100000 | 18.2 ms | 3.1 ms(**5.9x**↑) | 488.0 μs(**37.3x**↑) | -| BubbleSort n=200 | 1.5 ms | 3.3 ms(0.45x) | 738.8 μs(**1.9x**↑) | -| Sieve n=5000 | 353.4 μs | 1.0 ms(0.34x) | 219.3 μs(**1.8x**↑) | -| FloatPoly n=1000000 | — | — | **34.9x**↑(浮点特化,GCC 2x 快于 C++) | - -> TCC 纯计算类场景普遍快于 Lua;在包含 Table 操作的场景下,Table 结构体特化使 GCC 与 TCC 的 Table 读写性能均大幅提升。完整数据见 [benchmark/README.zh.md](benchmark/README.zh.md) / [English](benchmark/README.md)。 +| Fibonacci n=32 | 290.1 ms | 222.4 ms(**1.3x**) | 5.4 ms(**53.5x**) | +| Sum n=5000000 | 33.8 ms | 16.2 ms(**2.1x**) | 2.0 ms(**16.7x**) | +| Popcount n=100000 | 17.5 ms | 3.2 ms(**5.6x**) | 1.0 ms(**16.9x**) | +| BubbleSort n=200 | 1.5 ms | 1.5 ms(1.0x) | 159.7 μs(**9.6x**) | +| Sieve n=5000 | 334.1 μs | 467.2 μs(0.72x) | 62.9 μs(**5.3x**) | +| FloatPoly n=1000000 | 91.2 ms | 13.8 ms(**6.6x**) | 3.7 ms(**24.4x**,与 C++ 持平) | + +> 2026-11-30 这次测量里,TCC 在 Fibonacci 上只比 Lua 略快,Sieve 反而更慢。GCC 仍然领先数值场景,FloatPoly 与手写 C++ 持平。完整数据见 [benchmark/README.zh.md](benchmark/README.zh.md) / [English](benchmark/README.md)。 ## C++ API 详细文档 diff --git a/benchmark/README.md b/benchmark/README.md index 0501e8ef..0ca2309d 100644 --- a/benchmark/README.md +++ b/benchmark/README.md @@ -1,1133 +1,102 @@ -[中文](README.zh.md) | English +# Benchmark results -# Benchmark Results +These CPU times were measured on 2026-11-30 with `build/bin/bench_mark` built Release (`-O3 -DNDEBUG`). They replace the older notes that said the interpreter had no tail calls and that `string.gsub` used an ECMAScript Boost.Regex engine. -This file records results of running `bench_mark` compiled in **Release mode** (`-O3 -DNDEBUG`) locally. It covers **51 Lua performance scenarios across 6 categories**, each with C++ / Lua 5.4 / FakeLua TCC / FakeLua GCC / FakeLua INTERP (bytecode interpreter). Analysis below splits into **interpreter vs Lua 5.4** and **GCC JIT vs Lua / C++**. Table benchmarks include latest measurements reflecting the Lua-style contiguous array part (`VarTable::arr_`). +## Environment -## Running - -```bash -cmake -S . -B build -DCMAKE_BUILD_TYPE=Release -DFAKELUA_BUILD_BENCHMARKS=ON -cmake --build build --target bench_mark --parallel -# Lua 5.4 vs bytecode interpreter only: -build/bin/bench_mark --benchmark_filter='BM_Lua_|BM_FakeLua_.*_INTERP' \ - --benchmark_repetitions=1 --benchmark_report_aggregates_only=true -# Full suite (C++ / Lua / TCC / GCC / INTERP): -build/bin/bench_mark --benchmark_repetitions=1 --benchmark_report_aggregates_only=true -``` - ---- - -## Interpreter vs Lua 5.4 (2026-09-16) - -Bytecode VM (`JIT_INTERP`): 8-byte `Inst`, GNU computed-goto dispatch, Lua 5.4-style integer `FORPREP`/`FORLOOP` countdown, dest-driven `ADD`. Production path remains **JIT_GCC**, not the interpreter. - -### Environment (INTERP run) - -- Date: 2026-09-16 -- Machine: AMD EPYC 7551 32-Core Processor, 16 X 2000 MHz CPU s (KVM) -- CPU Caches: L1d 64 KiB (x16), L1i 64 KiB (x16), L2 512 KiB (x16), L3 65536 KiB (x2) -- Build mode: **Release** (`-O3 -DNDEBUG`), GCC 16.2.0 +- Date: 2026-11-30 +- Machine: AMD EPYC 7551 32-Core Processor, 16 vCPU, 2.0 GHz +- Build: GCC 16.2.0, Release `-O3 -DNDEBUG` - Lua: 5.4.7 -- FakeLua INTERP: `debug_mode=false` (scripts still compiled for TCC/GCC at init; timed path is `Call(..., JIT_INTERP, ...)`) -- Binary: `build/bin/bench_mark` - -> Absolute runtimes are highly dependent on machine and compiler version and are not comparable across environments. **INTERP vs Lua** = Lua CPU time / INTERP CPU time (>1 means the interpreter is faster). The **slower by** column is INTERP / Lua (how many times Lua's time the interpreter takes). Only ratios inside this section are meaningful; do not mix them with the 2026-08-14 GCC tables. - -Largest parameter per scenario: - -### Algorithms (algo) - -| Scenario | Param | INTERP vs Lua | INTERP / Lua time | Notes | -|----------|-------|---------------|-------------------|-------| -| Fibonacci | n=32 | 0.13x | **7.6x** | Recursive `CALL`; Lua is much cheaper per call | -| GCD | 2147483647/1073741823 | 0.29x | 3.4x | Tiny body, dispatch + boxing dominate | -| PowMod | 1234567/7654321/1e9+7 | 0.56x | 1.8x | | -| Sum | n=5M | 0.69x | **1.5x** | Closest numeric loop (in-place `ADD` + countdown `FORLOOP`) | -| BubbleSort | n=200 | 0.29x | 3.4x | Table index R/W, no array part | -| Sieve | n=5000 | 0.30x | 3.3x | | -| BinarySearch | n=1000 | 0.33x | 3.1x | | -| FastPow | 1234567/7654321/1e9+7 | 0.48x | 2.1x | | -| Popcount | n=100K | 0.33x | 3.0x | | -| InsertionSort | n=200 | 0.22x | 4.5x | | -| MatMul | 3×3 | 0.51x | 1.9x | | -| Vector3 | n=1M | 0.29x | 3.5x | Hash-only tables vs Lua array part | -| FloatPoly | n=1M | 0.77x | **1.3x** | Tight float loop, near Sum | - -### String (string) - -| Scenario | Param | INTERP vs Lua | INTERP / Lua time | Notes | -|----------|-------|---------------|-------------------|-------| -| StringLen | n=10K | **2.3x** | 0.44x | Work in C (`string.len`); INTERP faster | -| StringSub | n=10K | 1.86x | 0.54x | | -| StringRep | n=1000 | 1.08x | 0.93x | Roughly on par | -| StringReverse | n=10K | 1.13x | 0.89x | | -| StringLower | n=10K | **6.2x** | 0.16x | C native `string.lower` | -| StringUpper | n=10K | **6.1x** | 0.16x | Same as above | -| StringByte | n=1000 | 0.77x | 1.3x | | -| StringChar | n=500 | 0.44x | 2.3x | | -| StringFormat | n=500 | 0.78x | 1.3x | | -| StringFind | n=10K | 1.23x | 0.82x | Plain substring | -| StringGsub | n=1000 | 0.31x | 3.2x | ECMAScript Boost.Regex | -| ToNumber | n=1 | 0.34x | 2.9x | Single `Call()` + boxing | -| ToString | n=500 | 0.74x | 1.4x | | -| StringFindPattern | n=1000 | 0.25x | 4.0x | ECMAScript regex | -| StringGmatch | n=1000 | 0.55x | 1.8x | ECMAScript regex | - -### Table Operations (table) - -| Scenario | Param | INTERP vs Lua | INTERP / Lua time | Notes | -|----------|-------|---------------|-------------------|-------| -| TableInsert | n=5K | 0.38x | 2.6x | With array part (improved from 3.5x) | -| TableRemove | n=5K | 0.55x | 1.8x | With array part (improved from 2.4x) | -| TableConcat | n=1000 | 1.31x | 0.76x | Faster than Lua (C concat) | -| TablePack | n=1 | 1.66x | 0.60x | Faster than Lua | -| TableMove | n=5K | 0.75x | 1.3x | With array part (improved from 2.6x) | -| TableSort | n=1000 | 3.17x | 0.32x | C `table.sort` (3.2x faster than Lua) | -| TableCreate | n=5K | 0.71x | 1.4x | With array part (improved from 3.3x) | -| HashInsert | n=1000 | 1.53x | 0.65x | Arena vs Lua GC | -| HashLookup | n=1000 | 1.17x | 0.85x | | -| NestedTable | n=10K | 0.42x | 2.4x | With array part (improved from 4.0x) | - -### Function Calls (function) - -| Scenario | Param | INTERP vs Lua | INTERP / Lua time | Notes | -|----------|-------|---------------|-------------------|-------| -| EmptyCall | n=100K | 0.13x | **7.8x** | Per-call `CallByName` / interp enter | -| Recursion | n=25 | 0.17x | **6.0x** | Same as Fibonacci | -| Variadic | n=1 | 0.19x | 5.2x | `select` / vararg | -| MultiReturn | n=10K | 0.18x | 5.6x | | -| Closure | n=1000 | 0.36x | 2.7x | | -| TailRecursion | n=5K | 0.03x | **34x** | No TCO; real C recursion (`ulimit -s unlimited`) | - -### GC & Memory Pressure (gc) - -| Scenario | Param | INTERP vs Lua | INTERP / Lua time | Notes | -|----------|-------|---------------|-------------------|-------| -| TableChurn | n=1000 | **2.2x** | 0.45x | Arena, no GC | -| StringChurn | n=1000 | 1.40x | 0.71x | | -| MixedAlloc | n=1000 | 1.72x | 0.58x | | - -### Math Functions (math) - -| Scenario | Param | INTERP vs Lua | INTERP / Lua time | Notes | -|----------|-------|---------------|-------------------|-------| -| MathTrig (sin+cos) | n=100K | 0.49x | 2.0x | Native math + interp loop | -| MathSqrt | n=100K | 0.33x | 3.1x | | -| MathExpLog | n=100K | 0.42x | 2.4x | | -| MathMinMax | n=100K | 0.43x | 2.3x | | - -### Interpreter vs Lua — key findings - -1. **Tight integer/float loops are the closest**: Sum n=5M is **1.5× Lua**, FloatPoly n=1M is **1.3× Lua**. Remaining cost is tagged `CVar` copies and `boxes[]` checks on every register R/W. - -2. **Call-heavy code is 6–8× slower**: Fibonacci / Recursion / EmptyCall. Lua's call is a VM opcode; FakeLua INTERP re-enters `Call()` / `CallByName` with boxed arguments. - -3. **Tail recursion is the outlier (34×)**: Lua 5.4 turns `return f(...)` into a loop. INTERP does not; n=5000 is 5000 nested C frames (and overflows the default 8 MiB stack). - -4. **Table loops and operations with array part**: With the contiguous array-part implementation, table loops and operations are significantly faster. In the bytecode interpreter, table loop slowdown vs Lua dropped from ~3–4× to ~1.3–2.6× (`table.sort` is 3.2× faster than Lua, `TableConcat` is 1.3× faster, and `TableInsert`/`TableRemove` are substantially improved). - -5. **C stdlib work can beat Lua**: `string.lower`/`upper` **6.2×**, `string.len` 2.3×, `table.sort` 1.9×, TableChurn 2.2× (arena). When the opcode loop is not the bottleneck, native helpers plus no-GC allocation win. - -6. **Regex remains slower** (Gsub 3.2× Lua time, FindPattern 4.0×): ECMAScript Boost.Regex, not Lua pattern — same tradeoff as GCC JIT. - ---- - -## JIT GCC vs Lua / C++ (2026-08-14) - -The tables below are from a **previous** run on a different machine (AMD EPYC 7K62, GCC 15.1.0). They are **not** comparable to the INTERP numbers above. **GCC vs Lua** = speedup of FakeLua GCC relative to Lua 5.4 (>1 means GCC JIT is faster); **GCC vs C++** = FakeLua GCC / handwritten C++ (<1 means FakeLua is faster). - -### Environment (GCC run) - -- Date: 2026-08-14 -- Machine: AMD EPYC 7K62 48-Core Processor, 2 X 2595.12 MHz CPU s -- CPU Caches: L1d 32 KiB (x2), L1i 32 KiB (x2), L2 4096 KiB (x2), L3 16384 KiB (x1) -- Build mode: **Release** (`-O3 -DNDEBUG`), GCC 15.1.0 -- FakeLua GCC JIT: **Release mode** (`debug_mode=false`, GCC `-O3` optimization) -- Binary: `build/bin/bench_mark` - -> C++ reference implementations in some scenarios may be fully folded by the compiler (see annotations below), in which case the GCC vs C++ column is not informative. - -### Algorithms (algo) - -| Scenario | Param | GCC vs Lua | GCC vs C++ | Notes | -|----------|-------|-----------|-----------|-------| -| Fibonacci | n=32 | **36.6x** | 0.13 | Numeric specialization, close to C++ | -| GCD | 2147483647/1073741823 | 0.45x | 10.8 | Single call ~0.4 µs, dominated by call overhead | -| PowMod | 1234567/7654321/1e9+7 | 1.7x | 2.56 | | -| Sum | n=5M | **30.4x** | 0.06 | GCC vectorization, far faster than C++ | -| BubbleSort | n=200 | 1.9x | 2.80 | Table index read/write drag | -| Sieve | n=5000 | 1.8x | 2.68 | | -| BinarySearch | n=1000 | 3.7x | 2.40 | | -| FastPow | 1234567/7654321/1e9+7 | 1.6x | 2.45 | | -| Popcount | n=100K | **37.3x** | 0.12 | Extreme bitwise optimization | -| InsertionSort | n=200 | 2.6x | 2.92 | | -| MatMul | 3×3 | 2.9x | 5.10 | | -| Vector3 | n=1M | 4.8x | 5.85 | Table specialized to struct, pointer offset | -| FloatPoly | n=1M | **34.9x** | 0.49 | Float specialization, GCC 2x faster than C++ | - -### String (string) - -| Scenario | Param | GCC vs Lua | GCC vs C++ | Notes | -|----------|-------|-----------|-----------|-------| -| StringLen | n=10K | 1.8x | 67.3 | | -| StringSub | n=10K | 1.3x | 6.15 | | -| StringRep | n=1000 | 1.1x | 0.16 | CGen `FlStringRep` inline | -| StringReverse | n=10K | 1.7x | 0.26 | CGen `FlStringReverse` inline | -| StringLower | n=10K | **6.1x** | 0.02 | CGen inline + ASCII single pass | -| StringUpper | n=10K | **5.0x** | 0.02 | Same as above | -| StringByte | n=1000 | 1.0x | 37.7 | | -| StringChar | n=500 | 1.0x | 10.6 | | -| StringFormat | n=500 | 1.8x | 2.71 | Const `"%d"` → `FlFormatInt` | -| StringFind | n=10K | 0.85x | 19.2 | Plain substring search (`FlStringFindPlain` inline, but still has call overhead) | -| StringGsub | n=1000 | 0.06x | 38.1 | ECMAScript regex (see below) | -| ToNumber | n=1 | 0.53x | 5.65 | CGen `FlTonumber` decimal integer inline parse (same input `"1234567890"`) | -| ToString | n=500 | 0.74x | 0.01 | INT → `FlFormatInt` | -| StringFindPattern | n=1000 | 0.12x | 39.6 | ECMAScript regex; compile cache added | -| StringGmatch | n=1000 | 0.21x | 23.3 | ECMAScript regex; compile cache added | - -> **On regex being slower than Lua**: FakeLua's `string.find` / `match` / `gmatch` / `gsub` use **ECMAScript Boost.Regex** (with process-level compile caching), which is more powerful than Lua 5.4's built-in pattern (lookahead, full character classes, non-greedy, etc.). Therefore regex scenarios being slower than Lua (currently ~0.06~0.21x) is **acceptable** — it's capability for performance. To match Lua in the future, the direction is a separate Lua pattern engine, not further optimizing Boost.Regex. Scripts uniformly use `[0-9]+` (semantically identical in Lua pattern and ECMAScript regex). - -### Table Operations (table) - -| Scenario | Param | GCC vs Lua | GCC vs C++ | Notes | -|----------|-------|-----------|-----------|-------| -| TableInsert | n=5K | **9.8x** | 16.5 | With array part (up from 4.0x) | -| TableRemove | n=5K | **12.2x** | 29.5 | With array part (up from 3.2x) | -| TableConcat | n=1000 | 1.4x | 15.0 | Arena single write | -| TablePack | n=1 | 3.7x | 1229 | | -| TableMove | n=5K | 1.9x | 33.1 | Array-aware move | -| TableSort | n=1000 | 4.6x | 4.62 | C `table.sort` | -| TableCreate | n=5K | 2.7x | 14.7 | Pre-allocated array part | -| HashInsert | n=1000 | 2.4x | 1.34 | | -| HashLookup | n=1000 | 2.4x | 0.82 | Faster than handwritten C++ map | -| NestedTable | n=10K | 3.8x | 10.0 | Array part integer indexing | - -### Function Calls (function) - -| Scenario | Param | GCC vs Lua | GCC vs C++ | Notes | -|----------|-------|-----------|-----------|-------| -| EmptyCall | n=100K | **14.1x** | 61561 | C++ inlined to 0 | -| Recursion | n=25 | **42.9x** | 0.13 | Numeric specialization | -| Variadic | n=1 | 0.91x | 75.4 | High vararg construction overhead | -| MultiReturn | n=10K | **23.3x** | 0.46 | | -| Closure | n=1000 | 2.1x | 19.4 | | -| TailRecursion | n=5K | **107.5x** | 0.07 | Tail call to loop + vectorization | - -### GC & Memory Pressure (gc) - -| Scenario | Param | GCC vs Lua | GCC vs C++ | Notes | -|----------|-------|-----------|-----------|-------| -| TableChurn | n=1000 | **9.3x** | 1.24 | Arena allocator advantage | -| StringChurn | n=1000 | 2.2x | 1.47 | String allocation is a weakness | -| MixedAlloc | n=1000 | 2.3x | 1.68 | | - -### Math Functions (math) - -| Scenario | Param | GCC vs Lua | GCC vs C++ | Notes | -|----------|-------|-----------|-----------|-------| -| MathTrig (sin+cos) | n=100K | **5.8x** | 0.87 | Close to native C++ speed | -| MathSqrt | n=100K | **22.3x** | 0.48 | GCC 2x faster than C++ | -| MathExpLog | n=100K | **4.3x** | 0.87 | | -| MathMinMax | n=100K | **10.4x** | 0.90 | | - -### Key Findings - -1. **Pure numeric scenarios comprehensively beat Lua 5.4 and approach handwritten C++**: Fibonacci 36.6x, TailRecursion 107.5x, Recursion 42.9x, FloatPoly 34.9x, Popcount 37.3x, Sum 30.4x. Numeric specialization makes the generated C code nearly identical to handwritten versions; the rest is handled by GCC `-O3`. - -2. **All four math library functions are faster than Lua** (4.3x~22.3x); sin/cos/sqrt approach native C++ speed. - -3. **Most string standard library functions are faster than Lua**: lower/upper 5-6x, format 1.8x, rep 1.1x, reverse 1.7x — all via CGen inline (`FlStringLower/Upper`, `FlFormatInt`, `FlStringRep`, `FlStringReverse`) avoiding `FakeluaCallByName` overhead. Only StringFind (plain) at 0.85x is slightly slower than Lua, the remaining gap being single-call dispatch overhead. - -4. **Table operations are overall advantageous**: table.insert 4.0x, remove 3.2x, sort 1.2x faster than Lua; move / concat roughly on par. `VarTable` caches contiguous integer key prefix length making `#t` O(1); host side and JIT side share the same hash/bucket layout. - -5. **Arena allocator shows clear advantage in heavy table-creation scenarios**: TableChurn 9.3x faster than Lua — no GC, bulk free. - -6. **Regex scenarios being slower than Lua is acceptable** (Gsub 0.06x, FindPattern 0.12x, Gmatch 0.21x): uses more powerful ECMAScript Boost.Regex (not Lua pattern), with compile caching; different capability, not a target to match. - -7. **Remaining slow items are dominated by call dispatch overhead**: GCD (0.45x), ToNumber (0.53x), ToString (0.74x), Variadic (0.91x) have extremely small function bodies (< 1 µs); JIT CVar boxing/unboxing and calling convention overhead dominate — not the computation itself being slow. Cross-function inlining or calling convention optimization is needed to catch up. - - > Note: ToNumber bench parses the same input string `"1234567890"` on both sides. The remaining gap is mainly from single `Call()` dispatch and string boxing constant overhead, not parsing itself. - ---- - -## Complete Raw Output (Lua vs INTERP, 2026-09-16) - -Filter: `BM_Lua_|BM_FakeLua_.*_INTERP`. CPU times are the ones used in the INTERP vs Lua tables above. - -```text -Starting benchmarks... -2026-09-16T15:48:38+08:00 -Running ./bin/bench_mark -Run on (16 X 2000 MHz CPU s) -CPU Caches: - L1 Data 64 KiB (x16) - L1 Instruction 64 KiB (x16) - L2 Unified 512 KiB (x16) - L3 Unified 65536 KiB (x2) -***WARNING*** ASLR is enabled, the results may have unreproducible noise in them. ----------------------------------------------------------------------------- -Benchmark Time CPU Iterations ----------------------------------------------------------------------------- -BM_Lua_Fibonacci/20 1144569 ns 1084586 ns 637 -BM_Lua_Fibonacci/25 13283206 ns 12397758 ns 57 -BM_Lua_Fibonacci/30 150373384 ns 136381787 ns 5 -BM_Lua_Fibonacci/32 334257089 ns 334253452 ns 2 -BM_FakeLua_Fibonacci_INTERP/20 8578088 ns 8577897 ns 81 -BM_FakeLua_Fibonacci_INTERP/25 89749845 ns 89746764 ns 8 -BM_FakeLua_Fibonacci_INTERP/30 975132570 ns 975108155 ns 1 -BM_FakeLua_Fibonacci_INTERP/32 2552108645 ns 2552008862 ns 1 -BM_Lua_GCD/832040/514229 556 ns 556 ns 1290670 -BM_Lua_GCD/123456789/987654321 149 ns 149 ns 4642582 -BM_Lua_GCD/2147483647/1073741823 134 ns 134 ns 5272710 -BM_FakeLua_GCD_INTERP/832040/514229 1788 ns 1788 ns 407229 -BM_FakeLua_GCD_INTERP/123456789/987654321 518 ns 518 ns 1176854 -BM_FakeLua_GCD_INTERP/2147483647/1073741823 460 ns 460 ns 1597461 -BM_Lua_PowMod/2/1000/1000000007 699 ns 699 ns 990032 -BM_Lua_PowMod/7/1000000/1000000007 1210 ns 1210 ns 590702 -BM_Lua_PowMod/1234567/7654321/1000000007 1526 ns 1526 ns 450278 -BM_FakeLua_PowMod_INTERP/2/1000/1000000007 1394 ns 1394 ns 504574 -BM_FakeLua_PowMod_INTERP/7/1000000/1000000007 2395 ns 2395 ns 299151 -BM_FakeLua_PowMod_INTERP/1234567/7654321/1000000007 2714 ns 2713 ns 257497 -BM_Lua_Sum/10000 68382 ns 68378 ns 10435 -BM_Lua_Sum/100000 690733 ns 690725 ns 987 -BM_Lua_Sum/1000000 6726799 ns 6726654 ns 104 -BM_Lua_Sum/5000000 33570042 ns 33568361 ns 21 -BM_FakeLua_Sum_INTERP/10000 98246 ns 98242 ns 7088 -BM_FakeLua_Sum_INTERP/100000 975815 ns 975789 ns 719 -BM_FakeLua_Sum_INTERP/1000000 9770269 ns 9769755 ns 72 -BM_FakeLua_Sum_INTERP/5000000 48913989 ns 48914112 ns 14 -BM_Lua_BubbleSort/50 97623 ns 97619 ns 7161 -BM_Lua_BubbleSort/100 399558 ns 399543 ns 1844 -BM_Lua_BubbleSort/200 1529475 ns 1529435 ns 440 -BM_FakeLua_BubbleSort_INTERP/50 333897 ns 333884 ns 2106 -BM_FakeLua_BubbleSort_INTERP/100 1349297 ns 1349251 ns 532 -BM_FakeLua_BubbleSort_INTERP/200 5262565 ns 5262498 ns 132 -BM_Lua_Sieve/100 9456 ns 9456 ns 74524 -BM_Lua_Sieve/500 36011 ns 36011 ns 19372 -BM_Lua_Sieve/1000 72348 ns 72345 ns 9478 -BM_Lua_Sieve/5000 324762 ns 324745 ns 2091 -BM_FakeLua_Sieve_INTERP/100 17545 ns 17544 ns 40212 -BM_FakeLua_Sieve_INTERP/500 89880 ns 89879 ns 7841 -BM_FakeLua_Sieve_INTERP/1000 192069 ns 192069 ns 3874 -BM_FakeLua_Sieve_INTERP/5000 1070652 ns 1070611 ns 654 -BM_Lua_BinarySearch/100 41313 ns 41312 ns 17030 -BM_Lua_BinarySearch/500 282204 ns 282205 ns 2473 -BM_Lua_BinarySearch/1000 636610 ns 636602 ns 1108 -BM_FakeLua_BinarySearch_INTERP/100 124217 ns 124214 ns 5632 -BM_FakeLua_BinarySearch_INTERP/500 863565 ns 863516 ns 811 -BM_FakeLua_BinarySearch_INTERP/1000 1947828 ns 1947729 ns 361 -BM_Lua_FastPow/2/1000/1000000007 704 ns 704 ns 992443 -BM_Lua_FastPow/7/1000000/1000000007 1103 ns 1103 ns 638242 -BM_Lua_FastPow/1234567/7654321/1000000007 1462 ns 1462 ns 479726 -BM_FakeLua_FastPow_INTERP/2/1000/1000000007 1523 ns 1523 ns 462710 -BM_FakeLua_FastPow_INTERP/7/1000000/1000000007 2578 ns 2578 ns 283648 -BM_FakeLua_FastPow_INTERP/1234567/7654321/1000000007 3051 ns 3050 ns 231470 -BM_Lua_Popcount/1000 118658 ns 118656 ns 5883 -BM_Lua_Popcount/10000 1471087 ns 1471053 ns 475 -BM_Lua_Popcount/100000 17968504 ns 17967236 ns 39 -BM_FakeLua_Popcount_INTERP/1000 343823 ns 343819 ns 2028 -BM_FakeLua_Popcount_INTERP/10000 4310395 ns 4309845 ns 162 -BM_FakeLua_Popcount_INTERP/100000 53686039 ns 53683637 ns 13 -BM_Lua_InsertionSort/50 49325 ns 49323 ns 13744 -BM_Lua_InsertionSort/100 180948 ns 180941 ns 3869 -BM_Lua_InsertionSort/200 705938 ns 705929 ns 989 -BM_FakeLua_InsertionSort_INTERP/50 207825 ns 207820 ns 3366 -BM_FakeLua_InsertionSort_INTERP/100 804500 ns 804489 ns 877 -BM_FakeLua_InsertionSort_INTERP/200 3155980 ns 3155957 ns 221 -BM_Lua_MatMul 4416 ns 4416 ns 158495 -BM_FakeLua_MatMul_INTERP 8596 ns 8596 ns 81727 -BM_Lua_Vector3/10000 1709644 ns 1709608 ns 425 -BM_Lua_Vector3/100000 17277215 ns 17276556 ns 41 -BM_Lua_Vector3/1000000 167336192 ns 167336712 ns 4 -BM_FakeLua_Vector3_INTERP/10000 5870958 ns 5870569 ns 120 -BM_FakeLua_Vector3_INTERP/100000 59038871 ns 59037918 ns 12 -BM_FakeLua_Vector3_INTERP/1000000 583587155 ns 583577824 ns 1 -BM_Lua_FloatPoly/1000000 92155439 ns 92148782 ns 6 -BM_FakeLua_FloatPoly_INTERP/1000000 119274266 ns 119273134 ns 6 -BM_Lua_EmptyCall/10000 410740 ns 410735 ns 1715 -BM_Lua_EmptyCall/100000 4115022 ns 4114857 ns 163 -BM_FakeLua_EmptyCall_INTERP/10000 3225408 ns 3225336 ns 217 -BM_FakeLua_EmptyCall_INTERP/100000 32207058 ns 32207147 ns 22 -BM_Lua_Recursion/10 10530 ns 10530 ns 66500 -BM_Lua_Recursion/20 1285968 ns 1285954 ns 548 -BM_Lua_Recursion/25 14696263 ns 14696145 ns 49 -BM_FakeLua_Recursion_INTERP/10 67874 ns 67869 ns 10328 -BM_FakeLua_Recursion_INTERP/20 8065164 ns 8064800 ns 82 -BM_FakeLua_Recursion_INTERP/25 88200150 ns 88200363 ns 8 -BM_Lua_Variadic/1 548 ns 548 ns 1271307 -BM_FakeLua_Variadic_INTERP/1 2825 ns 2825 ns 250154 -BM_Lua_MultiReturn/1000 61903 ns 61903 ns 11129 -BM_Lua_MultiReturn/10000 591379 ns 591332 ns 1169 -BM_FakeLua_MultiReturn_INTERP/1000 329144 ns 329132 ns 2145 -BM_FakeLua_MultiReturn_INTERP/10000 3290976 ns 3290938 ns 213 -BM_Lua_Closure/100 24524 ns 24522 ns 28867 -BM_Lua_Closure/1000 244847 ns 244544 ns 2871 -BM_FakeLua_Closure_INTERP/100 68483 ns 68479 ns 10128 -BM_FakeLua_Closure_INTERP/1000 670507 ns 670485 ns 1049 -BM_Lua_TailRecursion/100 3762 ns 3762 ns 186916 -BM_Lua_TailRecursion/1000 36355 ns 36353 ns 19180 -BM_Lua_TailRecursion/5000 185267 ns 185121 ns 3844 -BM_FakeLua_TailRecursion_INTERP/100 40193 ns 40187 ns 17432 -BM_FakeLua_TailRecursion_INTERP/1000 503108 ns 503077 ns 1000 -BM_FakeLua_TailRecursion_INTERP/5000 6259916 ns 6259115 ns 111 -BM_Lua_TableChurn/100 59469 ns 59465 ns 11172 -BM_Lua_TableChurn/500 291246 ns 291232 ns 2342 -BM_Lua_TableChurn/1000 608475 ns 608455 ns 1156 -BM_FakeLua_TableChurn_INTERP/100 27575 ns 27573 ns 25768 -BM_FakeLua_TableChurn_INTERP/500 137383 ns 137377 ns 5133 -BM_FakeLua_TableChurn_INTERP/1000 273501 ns 273496 ns 2568 -BM_Lua_StringChurn/100 86787 ns 86784 ns 7981 -BM_Lua_StringChurn/500 560238 ns 560227 ns 1216 -BM_Lua_StringChurn/1000 1137369 ns 1137349 ns 611 -BM_FakeLua_StringChurn_INTERP/100 82985 ns 82979 ns 8970 -BM_FakeLua_StringChurn_INTERP/500 381000 ns 380990 ns 1793 -BM_FakeLua_StringChurn_INTERP/1000 812526 ns 812512 ns 882 -BM_Lua_MixedAlloc/100 108735 ns 108731 ns 6403 -BM_Lua_MixedAlloc/500 564361 ns 564349 ns 1297 -BM_Lua_MixedAlloc/1000 1095508 ns 1095467 ns 633 -BM_FakeLua_MixedAlloc_INTERP/100 61081 ns 61078 ns 11328 -BM_FakeLua_MixedAlloc_INTERP/500 317101 ns 317086 ns 2245 -BM_FakeLua_MixedAlloc_INTERP/1000 636884 ns 636886 ns 1073 -BM_Lua_MathTrig/100000 28406530 ns 28399049 ns 25 -BM_FakeLua_MathTrig_INTERP/100000 57870338 ns 57867951 ns 12 -BM_Lua_MathSqrt/100000 6499086 ns 6498149 ns 112 -BM_FakeLua_MathSqrt_INTERP/100000 19838534 ns 19837035 ns 35 -BM_Lua_MathExpLog/100000 21038376 ns 21034487 ns 34 -BM_FakeLua_MathExpLog_INTERP/100000 49770330 ns 49764563 ns 14 -BM_Lua_MathMinMax/100000 33257803 ns 33253252 ns 21 -BM_FakeLua_MathMinMax_INTERP/100000 78068647 ns 78066167 ns 9 -BM_Lua_StringLen/10 133 ns 133 ns 5255089 -BM_Lua_StringLen/100 299 ns 299 ns 2315101 -BM_Lua_StringLen/1000 598 ns 598 ns 1214737 -BM_Lua_StringLen/10000 2402 ns 2402 ns 294565 -BM_FakeLua_StringLen_INTERP/10 311 ns 311 ns 2212979 -BM_FakeLua_StringLen_INTERP/100 365 ns 365 ns 1932951 -BM_FakeLua_StringLen_INTERP/1000 565 ns 565 ns 1232995 -BM_FakeLua_StringLen_INTERP/10000 1064 ns 1064 ns 657973 -BM_Lua_StringSub/10 266 ns 266 ns 2603296 -BM_Lua_StringSub/100 500 ns 500 ns 1311387 -BM_Lua_StringSub/1000 1046 ns 1046 ns 667537 -BM_Lua_StringSub/10000 3807 ns 3806 ns 191614 -BM_FakeLua_StringSub_INTERP/10 753 ns 753 ns 926310 -BM_FakeLua_StringSub_INTERP/100 869 ns 869 ns 807951 -BM_FakeLua_StringSub_INTERP/1000 1130 ns 1130 ns 609855 -BM_FakeLua_StringSub_INTERP/10000 2046 ns 2045 ns 329919 -BM_Lua_StringRep/10 327 ns 327 ns 2154444 -BM_Lua_StringRep/100 932 ns 932 ns 727883 -BM_Lua_StringRep/1000 5267 ns 5265 ns 133440 -BM_FakeLua_StringRep_INTERP/10 726 ns 725 ns 931657 -BM_FakeLua_StringRep_INTERP/100 1117 ns 1117 ns 616015 -BM_FakeLua_StringRep_INTERP/1000 4874 ns 4874 ns 145223 -BM_Lua_StringReverse/10 258 ns 257 ns 2716507 -BM_Lua_StringReverse/100 696 ns 696 ns 970621 -BM_Lua_StringReverse/1000 1952 ns 1952 ns 361174 -BM_Lua_StringReverse/10000 12733 ns 12733 ns 54546 -BM_FakeLua_StringReverse_INTERP/10 712 ns 712 ns 979915 -BM_FakeLua_StringReverse_INTERP/100 883 ns 883 ns 793220 -BM_FakeLua_StringReverse_INTERP/1000 2191 ns 2191 ns 318712 -BM_FakeLua_StringReverse_INTERP/10000 11298 ns 11296 ns 62191 -BM_Lua_StringLower/10 252 ns 251 ns 2809706 -BM_Lua_StringLower/100 696 ns 696 ns 1013403 -BM_Lua_StringLower/1000 2313 ns 2309 ns 310662 -BM_Lua_StringLower/10000 16374 ns 16350 ns 43397 -BM_FakeLua_StringLower_INTERP/10 619 ns 619 ns 1088881 -BM_FakeLua_StringLower_INTERP/100 766 ns 766 ns 933398 -BM_FakeLua_StringLower_INTERP/1000 1179 ns 1179 ns 555653 -BM_FakeLua_StringLower_INTERP/10000 2653 ns 2653 ns 258309 -BM_Lua_StringUpper/10 250 ns 250 ns 2752023 -BM_Lua_StringUpper/100 712 ns 712 ns 992559 -BM_Lua_StringUpper/1000 2263 ns 2263 ns 299372 -BM_Lua_StringUpper/10000 15686 ns 15686 ns 44445 -BM_FakeLua_StringUpper_INTERP/10 559 ns 559 ns 1225659 -BM_FakeLua_StringUpper_INTERP/100 664 ns 664 ns 1044102 -BM_FakeLua_StringUpper_INTERP/1000 1079 ns 1079 ns 650906 -BM_FakeLua_StringUpper_INTERP/10000 2586 ns 2586 ns 271093 -BM_Lua_StringByte/10 216 ns 216 ns 3245889 -BM_Lua_StringByte/100 410 ns 410 ns 1707452 -BM_Lua_StringByte/1000 708 ns 708 ns 982592 -BM_FakeLua_StringByte_INTERP/10 665 ns 665 ns 1004509 -BM_FakeLua_StringByte_INTERP/100 737 ns 737 ns 936469 -BM_FakeLua_StringByte_INTERP/1000 918 ns 918 ns 758807 -BM_Lua_StringChar/10 3101 ns 3100 ns 227212 -BM_Lua_StringChar/100 18121 ns 18108 ns 38797 -BM_Lua_StringChar/500 80596 ns 80591 ns 8481 -BM_FakeLua_StringChar_INTERP/10 4713 ns 4713 ns 148278 -BM_FakeLua_StringChar_INTERP/100 38219 ns 38219 ns 18354 -BM_FakeLua_StringChar_INTERP/500 183011 ns 183006 ns 3867 -BM_Lua_StringFormat/10 3735 ns 3735 ns 192317 -BM_Lua_StringFormat/100 34750 ns 34749 ns 19989 -BM_Lua_StringFormat/500 186590 ns 186590 ns 3834 -BM_FakeLua_StringFormat_INTERP/10 5285 ns 5285 ns 124800 -BM_FakeLua_StringFormat_INTERP/100 47515 ns 47515 ns 14820 -BM_FakeLua_StringFormat_INTERP/500 238127 ns 238110 ns 2943 -BM_Lua_StringFind/10 371 ns 371 ns 1948124 -BM_Lua_StringFind/100 504 ns 504 ns 1312309 -BM_Lua_StringFind/1000 860 ns 860 ns 836242 -BM_Lua_StringFind/10000 3017 ns 2850 ns 246275 -BM_FakeLua_StringFind_INTERP/10 1135 ns 1082 ns 672638 -BM_FakeLua_StringFind_INTERP/100 1068 ns 1064 ns 695238 -BM_FakeLua_StringFind_INTERP/1000 1501 ns 1500 ns 526220 -BM_FakeLua_StringFind_INTERP/10000 2325 ns 2325 ns 257862 -BM_Lua_StringGsub/10 847 ns 847 ns 871151 -BM_Lua_StringGsub/100 4804 ns 4804 ns 144975 -BM_Lua_StringGsub/1000 41984 ns 41983 ns 16648 -BM_FakeLua_StringGsub_INTERP/10 2700 ns 2700 ns 257624 -BM_FakeLua_StringGsub_INTERP/100 14755 ns 14754 ns 47288 -BM_FakeLua_StringGsub_INTERP/1000 134159 ns 134159 ns 5268 -BM_Lua_ToNumber/1 210 ns 210 ns 3308067 -BM_FakeLua_ToNumber_INTERP/1 613 ns 613 ns 1157814 -BM_Lua_ToString/10 531 ns 531 ns 1317167 -BM_Lua_ToString/100 545 ns 545 ns 1287984 -BM_Lua_ToString/500 510 ns 510 ns 1268430 -BM_FakeLua_ToString_INTERP/10 687 ns 687 ns 1009822 -BM_FakeLua_ToString_INTERP/100 693 ns 693 ns 1010478 -BM_FakeLua_ToString_INTERP/500 691 ns 691 ns 1012225 -BM_Lua_StringFindPattern/1000 841315 ns 841113 ns 831 -BM_FakeLua_StringFindPattern_INTERP/1000 3395999 ns 3395933 ns 207 -BM_Lua_StringGmatch/1000 1254819 ns 1254217 ns 580 -BM_FakeLua_StringGmatch_INTERP/1000 2282902 ns 2282906 ns 309 -BM_Lua_TableInsert/100 11848 ns 11847 ns 58757 -BM_Lua_TableInsert/500 50790 ns 50783 ns 12873 -BM_Lua_TableInsert/1000 99252 ns 99248 ns 7221 -BM_Lua_TableInsert/5000 481485 ns 481467 ns 1459 -BM_FakeLua_TableInsert_INTERP/100 35333 ns 35331 ns 21032 -BM_FakeLua_TableInsert_INTERP/500 170284 ns 170282 ns 4103 -BM_FakeLua_TableInsert_INTERP/1000 338430 ns 338377 ns 2055 -BM_FakeLua_TableInsert_INTERP/5000 1675580 ns 1675498 ns 420 -BM_Lua_TableRemove/100 16745 ns 16744 ns 44233 -BM_Lua_TableRemove/500 69826 ns 69824 ns 9644 -BM_Lua_TableRemove/1000 139181 ns 139181 ns 4979 -BM_Lua_TableRemove/5000 818740 ns 818715 ns 893 -BM_FakeLua_TableRemove_INTERP/100 39744 ns 39741 ns 17697 -BM_FakeLua_TableRemove_INTERP/500 193218 ns 193204 ns 3640 -BM_FakeLua_TableRemove_INTERP/1000 417360 ns 417277 ns 1816 -BM_FakeLua_TableRemove_INTERP/5000 1980494 ns 1980486 ns 357 -BM_Lua_TableConcat/100 40270 ns 40269 ns 17312 -BM_Lua_TableConcat/500 195418 ns 195390 ns 3575 -BM_Lua_TableConcat/1000 395958 ns 395955 ns 1766 -BM_FakeLua_TableConcat_INTERP/100 38597 ns 38596 ns 17322 -BM_FakeLua_TableConcat_INTERP/500 194953 ns 194946 ns 3471 -BM_FakeLua_TableConcat_INTERP/1000 378109 ns 378105 ns 1854 -BM_Lua_TablePack/1 1391 ns 1391 ns 516095 -BM_FakeLua_TablePack_INTERP/1 1246 ns 1246 ns 583527 -BM_Lua_TableMove/100 8386 ns 8386 ns 81054 -BM_Lua_TableMove/500 29067 ns 29066 ns 27500 -BM_Lua_TableMove/1000 46120 ns 46119 ns 14068 -BM_Lua_TableMove/5000 227038 ns 227034 ns 3061 -BM_FakeLua_TableMove_INTERP/100 11804 ns 11804 ns 59495 -BM_FakeLua_TableMove_INTERP/500 52757 ns 52753 ns 13284 -BM_FakeLua_TableMove_INTERP/1000 104800 ns 104798 ns 6719 -BM_FakeLua_TableMove_INTERP/5000 589798 ns 589783 ns 1170 -BM_Lua_TableSort/100 28256 ns 28256 ns 24447 -BM_Lua_TableSort/500 167972 ns 167969 ns 4165 -BM_Lua_TableSort/1000 368101 ns 368079 ns 1912 -BM_FakeLua_TableSort_INTERP/100 18169 ns 18138 ns 38351 -BM_FakeLua_TableSort_INTERP/500 92839 ns 92823 ns 7457 -BM_FakeLua_TableSort_INTERP/1000 197154 ns 196995 ns 3544 -BM_Lua_TableCreate/1000 26310 ns 26205 ns 27662 -BM_Lua_TableCreate/3000 75548 ns 75535 ns 9256 -BM_Lua_TableCreate/5000 130772 ns 130580 ns 5470 -BM_FakeLua_TableCreate_INTERP/1000 70613 ns 70599 ns 10037 -BM_FakeLua_TableCreate_INTERP/3000 236687 ns 236599 ns 3079 -BM_FakeLua_TableCreate_INTERP/5000 432378 ns 432278 ns 1371 -BM_Lua_HashInsert/100 59467 ns 59432 ns 11755 -BM_Lua_HashInsert/500 308858 ns 308734 ns 2319 -BM_Lua_HashInsert/1000 600007 ns 599999 ns 1037 -BM_FakeLua_HashInsert_INTERP/100 39912 ns 39911 ns 17196 -BM_FakeLua_HashInsert_INTERP/500 198025 ns 198016 ns 3588 -BM_FakeLua_HashInsert_INTERP/1000 449572 ns 449553 ns 1558 -BM_Lua_HashLookup/100 48837 ns 48833 ns 15309 -BM_Lua_HashLookup/500 250008 ns 249949 ns 2682 -BM_Lua_HashLookup/1000 490965 ns 490927 ns 1423 -BM_FakeLua_HashLookup_INTERP/100 42890 ns 42887 ns 15922 -BM_FakeLua_HashLookup_INTERP/500 216841 ns 216829 ns 3157 -BM_FakeLua_HashLookup_INTERP/1000 434753 ns 434704 ns 1589 -BM_Lua_NestedTable/1000 246962 ns 246938 ns 2869 -BM_Lua_NestedTable/10000 2668007 ns 2667960 ns 274 -BM_FakeLua_NestedTable_INTERP/1000 1089201 ns 1089025 ns 650 -BM_FakeLua_NestedTable_INTERP/10000 10695225 ns 10692977 ns 67 -``` - ---- - -## Appendix: GCC/TCC/C++ raw output (2026-08-14) - - -Below is the complete google benchmark output for all 51 scenarios from `benchmark_algo.cpp` / `benchmark_string.cpp` / `benchmark_table.cpp` / `benchmark_function.cpp` / `benchmark_gc.cpp` / `benchmark_math.cpp` (including TCC data): - -> ⚠️ The eight lines for `BM_*_StringFindPattern` / `BM_*_StringGmatch` are old data from **before** the `[0-9]+` correction; the two sides have unequal workloads and should not be compared. Use the corrected values in the tables above. +- Command: `./build/bin/bench_mark --benchmark_min_time=0.2s --benchmark_repetitions=1` + +Each row uses the largest registered argument. `INTERP/Lua` is interpreter time divided by Lua time, so a value above 1 means the interpreter is slower. `GCC/Lua` and `TCC/Lua` are Lua time divided by that backend, so a value above 1 means FakeLua is faster. C++ empty loops that the compiler deletes are not used as a GCC comparison. + +## What this run shows + +1. **Named calls and closures are close to Lua.** Interpreter EmptyCall takes 1.67x Lua, Closure 1.03x, and Recursion 1.96x. The old 6–8x gap is gone. A call site caches the resolved interpreter prototype, so the hot path does not hash the name on every call, and a closure with a few upvalues no longer heap-allocates the upvalue pointer array. +2. **An exact self tail call reuses the current frame.** The TailRecursion benchmark was removed from the suite, so it has no row here. Non-tail recursion still costs more: Fibonacci n=32 takes 3.09x Lua on the interpreter. Variadic calls take 4.21x because the extra arguments are still packed into a `Multi`. +3. **Patterns are Lua 5.4 patterns, not ECMAScript.** Interpreter `StringGsub` takes 1.33x Lua, and GCC does not beat Lua there (0.78x). `StringFindPattern` is still slower: 3.29x on the interpreter, and GCC reaches only 0.35x Lua's speed. The next string hotspot is the pattern engine, not another pass on GCC Fibonacci. +4. **GCC still leads numeric hotspots; use these ratios.** Fibonacci is 53.48x Lua, FloatPoly is 24.40x and matches handwritten C++. Sum is 16.73x, not the 30x figure from the old write-up. +5. **Allocation without GC can beat Lua on the interpreter.** TableChurn, StringChurn, and MixedAlloc are all faster than Lua. + +### Algorithms + +| Scenario | Args | Lua 5.4 | INTERP | INTERP/Lua | GCC | GCC/Lua | TCC | TCC/Lua | +|---|---|---:|---:|---:|---:|---:|---:|---:| +| Fibonacci | 32 | 290.1 ms | 896.3 ms | 3.09x | 5.4 ms | 53.48x | 222.4 ms | 1.30x | +| Sum | 5000000 | 33.8 ms | 76.6 ms | 2.27x | 2.0 ms | 16.73x | 16.2 ms | 2.08x | +| Popcount | 100000 | 17.5 ms | 58.6 ms | 3.35x | 1.0 ms | 16.91x | 3.2 ms | 5.55x | +| FloatPoly | 1000000 | 91.2 ms | 131.1 ms | 1.44x | 3.7 ms | 24.40x | 13.8 ms | 6.59x | +| BubbleSort | 200 | 1.5 ms | 4.2 ms | 2.73x | 159.7 μs | 9.60x | 1.5 ms | 1.04x | +| Sieve | 5000 | 334.1 μs | 828.2 μs | 2.48x | 62.9 μs | 5.31x | 467.2 μs | 0.72x | +| InsertionSort | 200 | 712.5 μs | 2.9 ms | 4.12x | 91.5 μs | 7.79x | 1.1 ms | 0.66x | +| BinarySearch | 1000 | 621.6 μs | 1.8 ms | 2.96x | 273.5 μs | 2.27x | 874.3 μs | 0.71x | +| Vector3 | 1000000 | 213.3 ms | 603.5 ms | 2.83x | 12.1 ms | 17.66x | 147.1 ms | 1.45x | +| MatMul | — | 4.4 μs | 7.5 μs | 1.71x | 1.5 μs | 2.92x | 4.0 μs | 1.10x | +| GCD | 832040/514229 | 551 ns | 1.8 μs | 3.23x | 354 ns | 1.56x | 806 ns | 0.68x | +| PowMod | 2/1000/1000000007 | 681 ns | 1.5 μs | 2.23x | 312 ns | 2.18x | 824 ns | 0.83x | +| FastPow | 2/1000/1000000007 | 753 ns | 1.5 μs | 2.05x | 322 ns | 2.34x | 541 ns | 1.39x | + +### Calls + +| Scenario | Args | Lua 5.4 | INTERP | INTERP/Lua | GCC | GCC/Lua | TCC | TCC/Lua | +|---|---|---:|---:|---:|---:|---:|---:|---:| +| EmptyCall | 100000 | 4.7 ms | 7.8 ms | 1.67x | 384.8 μs | 12.20x | 6.4 ms | 0.74x | +| Recursion | 25 | 15.7 ms | 30.9 ms | 1.96x | 176.4 μs | 89.22x | 8.4 ms | 1.88x | +| Closure | 1000 | 250.5 μs | 257.2 μs | 1.03x | 12.7 μs | 19.76x | 215.0 μs | 1.16x | +| MultiReturn | 10000 | 721.8 μs | 1.1 ms | 1.51x | 31.4 μs | 23.01x | 388.1 μs | 1.86x | +| Variadic | 1 | 661 ns | 2.8 μs | 4.21x | 267 ns | 2.48x | 771 ns | 0.86x | + +### Strings + +| Scenario | Args | Lua 5.4 | INTERP | INTERP/Lua | GCC | GCC/Lua | TCC | TCC/Lua | +|---|---|---:|---:|---:|---:|---:|---:|---:| +| StringLen | 10000 | 2.4 μs | 1.0 μs | 0.44x | 964 ns | 2.45x | 1.0 μs | 2.31x | +| StringSub | 10000 | 3.7 μs | 2.0 μs | 0.53x | 1.9 μs | 1.95x | 1.9 μs | 1.96x | +| StringLower | 10000 | 15.9 μs | 2.9 μs | 0.18x | 2.2 μs | 7.08x | 50.5 μs | 0.31x | +| StringUpper | 10000 | 15.8 μs | 2.7 μs | 0.17x | 2.2 μs | 7.20x | 50.9 μs | 0.31x | +| StringFind | 10000 | 2.6 μs | 2.3 μs | 0.90x | 3.2 μs | 0.81x | 19.7 μs | 0.13x | +| StringGsub | 1000 | 43.6 μs | 57.8 μs | 1.33x | 55.7 μs | 0.78x | 55.8 μs | 0.78x | +| StringFindPattern | 1000 | 872.6 μs | 2.9 ms | 3.29x | 2.5 ms | 0.35x | 2.4 ms | 0.36x | +| StringGmatch | 1000 | 1.2 ms | 2.4 ms | 1.96x | 1.6 ms | 0.77x | 1.8 ms | 0.67x | +| StringFormat | 500 | 186.0 μs | 259.3 μs | 1.39x | 6.6 μs | 27.98x | 44.0 μs | 4.23x | +| StringRep | 1000 | 5.3 μs | 4.7 μs | 0.88x | 4.5 μs | 1.18x | 5.4 μs | 0.98x | +| StringReverse | 10000 | 12.8 μs | 11.2 μs | 0.87x | 8.2 μs | 1.56x | 44.2 μs | 0.29x | +| StringByte | 1000 | 751 ns | 1.0 μs | 1.36x | 455 ns | 1.65x | 540 ns | 1.39x | +| StringChar | 500 | 81.6 μs | 157.7 μs | 1.93x | 12.0 μs | 6.79x | 80.4 μs | 1.02x | +| ToNumber | 1 | 213 ns | 570 ns | 2.68x | 234 ns | 0.91x | 537 ns | 0.40x | +| ToString | 500 | 515 ns | 577 ns | 1.12x | 207 ns | 2.48x | 311 ns | 1.66x | + +### Tables + +| Scenario | Args | Lua 5.4 | INTERP | INTERP/Lua | GCC | GCC/Lua | TCC | TCC/Lua | +|---|---|---:|---:|---:|---:|---:|---:|---:| +| TableCreate | 5000 | 132.2 μs | 263.1 μs | 1.99x | 51.7 μs | 2.56x | 266.1 μs | 0.50x | +| TableInsert | 5000 | 540.7 μs | 1.7 ms | 3.20x | 64.6 μs | 8.37x | 382.7 μs | 1.41x | +| TableRemove | 5000 | 902.4 μs | 2.0 ms | 2.27x | 62.1 μs | 14.53x | 628.7 μs | 1.44x | +| TableConcat | 1000 | 393.5 μs | 324.5 μs | 0.82x | 29.6 μs | 13.28x | 135.2 μs | 2.91x | +| TableSort | 1000 | 368.5 μs | 152.7 μs | 0.41x | 100.2 μs | 3.68x | 158.2 μs | 2.33x | +| TableMove | 5000 | 231.5 μs | 336.7 μs | 1.45x | 98.5 μs | 2.35x | 533.8 μs | 0.43x | +| TablePack | 1 | 1.4 μs | 952 ns | 0.67x | 420 ns | 3.37x | 1.2 μs | 1.21x | +| HashInsert | 1000 | 570.9 μs | 402.9 μs | 0.71x | 82.5 μs | 6.92x | 316.6 μs | 1.80x | +| HashLookup | 1000 | 442.5 μs | 419.3 μs | 0.95x | 38.5 μs | 11.50x | 183.9 μs | 2.41x | +| NestedTable | 10000 | 2.5 ms | 10.0 ms | 3.95x | 798.1 μs | 3.17x | 5.6 ms | 0.45x | + +### Math + +| Scenario | Args | Lua 5.4 | INTERP | INTERP/Lua | GCC | GCC/Lua | TCC | TCC/Lua | +|---|---|---:|---:|---:|---:|---:|---:|---:| +| MathTrig | 100000 | 28.8 ms | 65.7 ms | 2.28x | 9.1 ms | 3.15x | 18.6 ms | 1.55x | +| MathSqrt | 100000 | 7.5 ms | 20.7 ms | 2.78x | 322.6 μs | 23.13x | 4.8 ms | 1.54x | +| MathExpLog | 100000 | 25.7 ms | 69.6 ms | 2.71x | 5.4 ms | 4.75x | 12.9 ms | 2.00x | +| MathMinMax | 100000 | 34.6 ms | 103.4 ms | 2.99x | 4.6 ms | 7.50x | 17.5 ms | 1.97x | + +### Allocation + +| Scenario | Args | Lua 5.4 | INTERP | INTERP/Lua | GCC | GCC/Lua | TCC | TCC/Lua | +|---|---|---:|---:|---:|---:|---:|---:|---:| +| TableChurn | 1000 | 587.6 μs | 324.2 μs | 0.55x | 39.5 μs | 14.88x | 314.6 μs | 1.87x | +| StringChurn | 1000 | 1.1 ms | 775.7 μs | 0.69x | 52.9 μs | 21.28x | 319.3 μs | 3.53x | +| MixedAlloc | 1000 | 1.1 ms | 766.8 μs | 0.68x | 68.6 μs | 16.33x | 398.8 μs | 2.81x | -```text -:1561: warning: assignment of read-only location -Starting benchmarks... -2026-08-14T12:05:23+08:00 -Running ./bin/bench_mark -Run on (2 X 2595.12 MHz CPU s) -CPU Caches: - L1 Data 32 KiB (x2) - L1 Instruction 32 KiB (x2) - L2 Unified 4096 KiB (x2) - L3 Unified 16384 KiB (x1) -Load Average: 5.87, 5.69, 4.54 -***WARNING*** ASLR is enabled, the results may have unreproducible noise in them. ----------------------------------------------------------------------------- -Benchmark Time CPU Iterations ----------------------------------------------------------------------------- -BM_CPP_BinarySearch/100 7,735 ns 4,893 ns 143,193 -BM_CPP_BinarySearch/1000 94,468 ns 65,183 ns 10,769 -BM_CPP_BinarySearch/500 53,068 ns 29,810 ns 23,430 -BM_CPP_BubbleSort/100 98,467 ns 62,909 ns 11,127 -BM_CPP_BubbleSort/200 388,207 ns 249,256 ns 2,800 -BM_CPP_BubbleSort/50 26,237 ns 15,964 ns 43,992 -BM_CPP_Closure/100 792 ns 551 ns 1,272,292 -BM_CPP_Closure/1000 8,993 ns 5,466 ns 128,618 -BM_CPP_EmptyCall/10000 9 ns 5 ns 127,122,452 -BM_CPP_EmptyCall/100000 8 ns 5 ns 127,515,986 -BM_CPP_FastPow/1234567/7654321/1000000007 514 ns 297 ns 2,357,197 -BM_CPP_FastPow/2/1000/1000000007 261 ns 121 ns 5,799,249 -BM_CPP_FastPow/7/1000000/1000000007 373 ns 205 ns 3,429,130 -BM_CPP_Fibonacci/20 342,994 ns 188,804 ns 3,720 -BM_CPP_Fibonacci/25 3,686,904 ns 2,091,372 ns 333 -BM_CPP_Fibonacci/30 37,599,358 ns 23,298,280 ns 30 -BM_CPP_Fibonacci/32 99,812,719 ns 60,640,845 ns 12 -BM_CPP_FloatPoly/1000000 7,930,843 ns 5,847,105 ns 120 -BM_CPP_GCD/123456789/987654321 82 ns 44 ns 15,634,680 -BM_CPP_GCD/2147483647/1073741823 65 ns 39 ns 17,808,461 -BM_CPP_GCD/832040/514229 270 ns 181 ns 3,855,731 -BM_CPP_HashInsert/100 42,343 ns 25,099 ns 27,962 -BM_CPP_HashInsert/1000 404,595 ns 288,678 ns 2,410 -BM_CPP_HashInsert/500 224,686 ns 141,863 ns 4,922 -BM_CPP_HashLookup/100 540,670 ns 308,545 ns 2,270 -BM_CPP_HashLookup/1000 763,223 ns 492,689 ns 1,426 -BM_CPP_HashLookup/500 609,514 ns 389,886 ns 1,793 -BM_CPP_InsertionSort/100 51,359 ns 30,374 ns 23,049 -BM_CPP_InsertionSort/200 204,452 ns 118,372 ns 5,882 -BM_CPP_InsertionSort/50 13,938 ns 8,006 ns 88,502 -BM_CPP_MatMul 485 ns 247 ns 2,846,106 -BM_CPP_MathExpLog/100000 7,276,256 ns 4,629,740 ns 151 -BM_CPP_MathMinMax/100000 3,610,545 ns 2,396,188 ns 291 -BM_CPP_MathSqrt/100000 906,821 ns 571,279 ns 1,234 -BM_CPP_MathTrig/100000 5,398,646 ns 3,531,282 ns 199 -BM_CPP_MixedAlloc/100 32,146 ns 23,078 ns 30,205 -BM_CPP_MixedAlloc/1000 433,047 ns 234,244 ns 2,990 -BM_CPP_MixedAlloc/500 200,939 ns 116,896 ns 5,985 -BM_CPP_MultiReturn/1000 10,052 ns 5,480 ns 126,527 -BM_CPP_MultiReturn/10000 91,075 ns 54,916 ns 12,734 -BM_CPP_NestedTable/1000 91,243 ns 60,937 ns 11,457 -BM_CPP_NestedTable/10000 1,132,805 ns 607,423 ns 1,155 -BM_CPP_Popcount/1000 48,426 ns 32,558 ns 21,481 -BM_CPP_Popcount/10000 649,786 ns 408,784 ns 1,712 -BM_CPP_Popcount/100000 7,400,519 ns 5,004,960 ns 140 -BM_CPP_PowMod/1234567/7654321/1000000007 489 ns 297 ns 2,355,934 -BM_CPP_PowMod/2/1000/1000000007 192 ns 121 ns 5,817,991 -BM_CPP_PowMod/7/1000000/1000000007 329 ns 205 ns 3,395,045 -BM_CPP_Recursion/10 2,623 ns 1,517 ns 461,648 -BM_CPP_Recursion/20 306,905 ns 188,664 ns 3,708 -BM_CPP_Recursion/25 3,584,836 ns 2,076,389 ns 337 -BM_CPP_Sieve/100 2,194 ns 1,462 ns 476,278 -BM_CPP_Sieve/1000 27,651 ns 15,057 ns 46,837 -BM_CPP_Sieve/500 14,586 ns 7,367 ns 94,648 -BM_CPP_Sieve/5000 125,191 ns 79,410 ns 8,797 -BM_CPP_StringByte/10 30 ns 16 ns 41,418,033 -BM_CPP_StringByte/100 27 ns 16 ns 41,401,382 -BM_CPP_StringByte/1000 27 ns 16 ns 41,359,841 -BM_CPP_StringChar/10 348 ns 208 ns 3,374,227 -BM_CPP_StringChar/100 2,491 ns 1,459 ns 479,078 -BM_CPP_StringChar/500 11,997 ns 6,820 ns 102,786 -BM_CPP_StringChurn/100 52,805 ns 29,398 ns 23,705 -BM_CPP_StringChurn/1000 572,837 ns 302,474 ns 2,282 -BM_CPP_StringChurn/500 231,629 ns 152,176 ns 4,647 -BM_CPP_StringFind/10 105 ns 66 ns 10,555,668 -BM_CPP_StringFind/100 132 ns 66 ns 10,629,430 -BM_CPP_StringFind/1000 123 ns 70 ns 9,901,078 -BM_CPP_StringFind/10000 209 ns 130 ns 5,445,641 -BM_CPP_StringFindPattern/1000 359,361 ns 193,176 ns 3,608 -BM_CPP_StringFormat/10 845 ns 522 ns 1,337,811 -BM_CPP_StringFormat/100 7,618 ns 5,158 ns 132,433 -BM_CPP_StringFormat/500 43,386 ns 28,435 ns 24,749 -BM_CPP_StringGmatch/1000 437,710 ns 226,440 ns 3,092 -BM_CPP_StringGsub/10 277 ns 184 ns 3,792,558 -BM_CPP_StringGsub/100 2,760 ns 1,736 ns 407,043 -BM_CPP_StringGsub/1000 27,888 ns 17,248 ns 40,720 -BM_CPP_StringLen/10 27 ns 16 ns 41,401,789 -BM_CPP_StringLen/100 26 ns 16 ns 41,582,886 -BM_CPP_StringLen/1000 26 ns 16 ns 41,425,791 -BM_CPP_StringLen/10000 23 ns 16 ns 41,620,113 -BM_CPP_StringLower/10 306 ns 195 ns 3,601,028 -BM_CPP_StringLower/100 2,325 ns 1,348 ns 521,413 -BM_CPP_StringLower/1000 22,828 ns 12,466 ns 56,209 -BM_CPP_StringLower/10000 195,537 ns 123,952 ns 5,665 -BM_CPP_StringRep/10 739 ns 420 ns 1,655,571 -BM_CPP_StringRep/100 5,540 ns 3,120 ns 225,710 -BM_CPP_StringRep/1000 49,757 ns 30,123 ns 23,272 -BM_CPP_StringReverse/10 175 ns 103 ns 6,811,517 -BM_CPP_StringReverse/100 692 ns 450 ns 1,556,938 -BM_CPP_StringReverse/1000 5,818 ns 3,468 ns 200,200 -BM_CPP_StringReverse/10000 53,508 ns 34,013 ns 20,679 -BM_CPP_StringSub/10 151 ns 91 ns 7,642,290 -BM_CPP_StringSub/100 212 ns 134 ns 5,214,628 -BM_CPP_StringSub/1000 211 ns 143 ns 4,924,983 -BM_CPP_StringSub/10000 566 ns 369 ns 1,910,754 -BM_CPP_StringUpper/10 331 ns 194 ns 3,595,139 -BM_CPP_StringUpper/100 2,405 ns 1,347 ns 521,995 -BM_CPP_StringUpper/1000 21,399 ns 12,455 ns 55,977 -BM_CPP_StringUpper/10000 214,600 ns 123,125 ns 5,676 -BM_CPP_Sum/10000 87,621 ns 54,921 ns 12,724 -BM_CPP_Sum/100000 855,607 ns 548,039 ns 1,282 -BM_CPP_Sum/1000000 8,922,555 ns 5,483,076 ns 128 -BM_CPP_Sum/5000000 40,471,358 ns 27,449,499 ns 26 -BM_CPP_TableChurn/100 8,172 ns 5,265 ns 131,011 -BM_CPP_TableChurn/1000 76,141 ns 52,421 ns 13,634 -BM_CPP_TableChurn/500 50,410 ns 26,018 ns 26,612 -BM_CPP_TableConcat/100 12,265 ns 8,491 ns 82,644 -BM_CPP_TableConcat/1000 124,859 ns 87,820 ns 7,873 -BM_CPP_TableConcat/500 83,736 ns 43,958 ns 15,941 -BM_CPP_TableCreate/1000 19,036 ns 11,735 ns 59,861 -BM_CPP_TableCreate/3000 53,314 ns 34,985 ns 20,129 -BM_CPP_TableCreate/5000 89,021 ns 58,477 ns 12,039 -BM_CPP_TableInsert/100 1,047 ns 659 ns 1,075,111 -BM_CPP_TableInsert/1000 11,124 ns 5,875 ns 120,021 -BM_CPP_TableInsert/500 5,603 ns 2,984 ns 234,160 -BM_CPP_TableInsert/5000 44,146 ns 28,972 ns 24,187 -BM_CPP_TableMove/100 3,080 ns 1,905 ns 366,242 -BM_CPP_TableMove/1000 25,244 ns 17,827 ns 39,325 -BM_CPP_TableMove/500 15,080 ns 9,002 ns 77,536 -BM_CPP_TableMove/5000 144,251 ns 88,188 ns 7,924 -BM_CPP_TablePack/1 10 ns 5 ns 127,192,129 -BM_CPP_TableRemove/100 3,206 ns 1,792 ns 389,950 -BM_CPP_TableRemove/1000 32,342 ns 17,231 ns 40,355 -BM_CPP_TableRemove/500 14,258 ns 8,737 ns 81,578 -BM_CPP_TableRemove/5000 167,448 ns 85,880 ns 8,243 -BM_CPP_TableSort/100 8,862 ns 5,155 ns 137,027 -BM_CPP_TableSort/1000 87,764 ns 69,572 ns 10,079 -BM_CPP_TableSort/500 53,222 ns 33,132 ns 21,144 -BM_CPP_TailRecursion/100 1,049 ns 553 ns 1,267,372 -BM_CPP_TailRecursion/1000 9,701 ns 5,491 ns 127,230 -BM_CPP_TailRecursion/5000 45,927 ns 27,491 ns 25,469 -BM_CPP_ToNumber/1 167 ns 96 ns 7,262,672 -BM_CPP_ToString/10 1,205 ns 872 ns 802,717 -BM_CPP_ToString/100 13,072 ns 8,462 ns 84,581 -BM_CPP_ToString/500 70,978 ns 44,001 ns 16,041 -BM_CPP_Variadic/1 18 ns 10 ns 64,199,090 -BM_CPP_Vector3/10000 85,966 ns 52,871 ns 13,291 -BM_CPP_Vector3/100000 984,296 ns 527,532 ns 1,322 -BM_CPP_Vector3/1000000 10,423,974 ns 5,285,754 ns 133 -BM_FakeLua_BinarySearch_GCC/100 13,922 ns 8,156 ns 85,400 -BM_FakeLua_BinarySearch_GCC/1000 271,491 ns 156,553 ns 4,320 -BM_FakeLua_BinarySearch_GCC/500 122,929 ns 67,590 ns 10,333 -BM_FakeLua_BinarySearch_TCC/100 81,768 ns 52,935 ns 13,579 -BM_FakeLua_BinarySearch_TCC/1000 1,422,010 ns 874,824 ns 774 -BM_FakeLua_BinarySearch_TCC/500 660,936 ns 373,850 ns 1,911 -BM_FakeLua_BubbleSort_GCC/100 230,707 ns 175,479 ns 3,985 -BM_FakeLua_BubbleSort_GCC/200 1,244,348 ns 698,588 ns 1,007 -BM_FakeLua_BubbleSort_GCC/50 83,555 ns 44,675 ns 15,516 -BM_FakeLua_BubbleSort_TCC/100 1,454,647 ns 836,477 ns 816 -BM_FakeLua_BubbleSort_TCC/200 5,488,115 ns 3,256,166 ns 213 -BM_FakeLua_BubbleSort_TCC/50 293,114 ns 211,591 ns 3,390 -BM_FakeLua_Closure_GCC/100 19,181 ns 11,046 ns 63,564 -BM_FakeLua_Closure_GCC/1000 211,068 ns 106,248 ns 6,492 -BM_FakeLua_Closure_TCC/100 36,088 ns 21,576 ns 32,483 -BM_FakeLua_Closure_TCC/1000 354,499 ns 211,904 ns 3,347 -BM_FakeLua_EmptyCall_GCC/10000 56,352 ns 30,976 ns 23,093 -BM_FakeLua_EmptyCall_GCC/100000 557,461 ns 307,805 ns 2,283 -BM_FakeLua_EmptyCall_TCC/10000 367,650 ns 256,049 ns 2,730 -BM_FakeLua_EmptyCall_TCC/100000 5,327,379 ns 2,558,148 ns 272 -BM_FakeLua_FastPow_GCC/1234567/7654321/1000000007 1,095 ns 729 ns 957,306 -BM_FakeLua_FastPow_GCC/2/1000/1000000007 919 ns 531 ns 1,320,878 -BM_FakeLua_FastPow_GCC/7/1000000/1000000007 1,008 ns 632 ns 1,110,756 -BM_FakeLua_FastPow_TCC/1234567/7654321/1000000007 1,148 ns 818 ns 863,171 -BM_FakeLua_FastPow_TCC/2/1000/1000000007 864 ns 630 ns 1,111,709 -BM_FakeLua_FastPow_TCC/7/1000000/1000000007 1,241 ns 734 ns 956,152 -BM_FakeLua_Fibonacci_GCC/20 45,729 ns 29,702 ns 24,017 -BM_FakeLua_Fibonacci_GCC/25 385,589 ns 271,393 ns 2,570 -BM_FakeLua_Fibonacci_GCC/30 5,624,615 ns 2,967,464 ns 241 -BM_FakeLua_Fibonacci_GCC/32 12,786,224 ns 7,590,283 ns 93 -BM_FakeLua_Fibonacci_TCC/20 123,444 ns 78,774 ns 9,099 -BM_FakeLua_Fibonacci_TCC/25 1,350,023 ns 855,121 ns 805 -BM_FakeLua_Fibonacci_TCC/30 12,850,953 ns 9,452,837 ns 74 -BM_FakeLua_Fibonacci_TCC/32 38,140,319 ns 24,861,804 ns 28 -BM_FakeLua_FloatPoly_GCC/1000000 4,232,156 ns 2,865,423 ns 245 -BM_FakeLua_FloatPoly_TCC/1000000 22,886,829 ns 13,789,552 ns 50 -BM_FakeLua_GCD_GCC/123456789/987654321 654 ns 423 ns 1,654,415 -BM_FakeLua_GCD_GCC/2147483647/1073741823 620 ns 421 ns 1,665,114 -BM_FakeLua_GCD_GCC/832040/514229 824 ns 520 ns 1,353,393 -BM_FakeLua_GCD_TCC/123456789/987654321 784 ns 490 ns 1,442,922 -BM_FakeLua_GCD_TCC/2147483647/1073741823 817 ns 478 ns 1,444,175 -BM_FakeLua_GCD_TCC/832040/514229 994 ns 615 ns 1,145,840 -BM_FakeLua_HashInsert_GCC/100 39,491 ns 24,481 ns 28,788 -BM_FakeLua_HashInsert_GCC/1000 394,557 ns 253,863 ns 2,777 -BM_FakeLua_HashInsert_GCC/500 209,448 ns 119,708 ns 5,816 -BM_FakeLua_HashInsert_TCC/100 62,986 ns 39,308 ns 17,965 -BM_FakeLua_HashInsert_TCC/1000 709,844 ns 432,322 ns 1,607 -BM_FakeLua_HashInsert_TCC/500 288,573 ns 189,151 ns 3,702 -BM_FakeLua_HashLookup_GCC/100 32,466 ns 22,404 ns 31,590 -BM_FakeLua_HashLookup_GCC/1000 363,678 ns 222,897 ns 3,149 -BM_FakeLua_HashLookup_GCC/500 161,127 ns 111,924 ns 6,177 -BM_FakeLua_HashLookup_TCC/100 50,594 ns 30,941 ns 22,728 -BM_FakeLua_HashLookup_TCC/1000 541,329 ns 316,970 ns 2,207 -BM_FakeLua_HashLookup_TCC/500 271,060 ns 156,821 ns 4,417 -BM_FakeLua_InsertionSort_GCC/100 154,078 ns 89,101 ns 7,854 -BM_FakeLua_InsertionSort_GCC/200 584,821 ns 345,628 ns 2,021 -BM_FakeLua_InsertionSort_GCC/50 44,420 ns 23,795 ns 29,465 -BM_FakeLua_InsertionSort_TCC/100 902,132 ns 532,541 ns 1,398 -BM_FakeLua_InsertionSort_TCC/200 3,914,237 ns 2,087,508 ns 346 -BM_FakeLua_InsertionSort_TCC/50 233,170 ns 139,290 ns 4,924 -BM_FakeLua_MatMul_GCC 2,165 ns 1,260 ns 545,399 -BM_FakeLua_MatMul_TCC 6,551 ns 4,132 ns 166,327 -BM_FakeLua_MathExpLog_GCC/100000 5,721,714 ns 4,046,541 ns 174 -BM_FakeLua_MathExpLog_TCC/100000 17,934,875 ns 11,444,546 ns 61 -BM_FakeLua_MathMinMax_GCC/100000 3,704,800 ns 2,159,112 ns 323 -BM_FakeLua_MathMinMax_TCC/100000 19,676,004 ns 11,465,920 ns 62 -BM_FakeLua_MathSqrt_GCC/100000 464,885 ns 275,825 ns 2,537 -BM_FakeLua_MathSqrt_TCC/100000 6,879,179 ns 4,166,763 ns 168 -BM_FakeLua_MathTrig_GCC/100000 5,560,194 ns 3,058,615 ns 229 -BM_FakeLua_MathTrig_TCC/100000 21,645,316 ns 11,484,684 ns 61 -BM_FakeLua_MixedAlloc_GCC/100 62,577 ns 40,070 ns 17,411 -BM_FakeLua_MixedAlloc_GCC/1000 536,218 ns 393,414 ns 1,779 -BM_FakeLua_MixedAlloc_GCC/500 310,651 ns 197,231 ns 3,526 -BM_FakeLua_MixedAlloc_TCC/100 102,126 ns 62,232 ns 11,196 -BM_FakeLua_MixedAlloc_TCC/1000 973,380 ns 628,448 ns 1,111 -BM_FakeLua_MixedAlloc_TCC/500 521,024 ns 314,661 ns 2,229 -BM_FakeLua_MultiReturn_GCC/1000 4,766 ns 2,915 ns 240,764 -BM_FakeLua_MultiReturn_GCC/10000 44,533 ns 25,062 ns 27,754 -BM_FakeLua_MultiReturn_TCC/1000 9,596 ns 5,775 ns 124,053 -BM_FakeLua_MultiReturn_TCC/10000 84,003 ns 52,909 ns 13,405 -BM_FakeLua_NestedTable_GCC/1000 138,830 ns 85,672 ns 8,199 -BM_FakeLua_NestedTable_GCC/10000 1,448,717 ns 845,455 ns 813 -BM_FakeLua_NestedTable_TCC/1000 769,763 ns 444,952 ns 1,569 -BM_FakeLua_NestedTable_TCC/10000 7,183,922 ns 4,455,904 ns 158 -BM_FakeLua_Popcount_GCC/1000 6,777 ns 4,367 ns 160,915 -BM_FakeLua_Popcount_GCC/10000 89,012 ns 48,663 ns 14,499 -BM_FakeLua_Popcount_GCC/100000 1,016,120 ns 592,552 ns 1,160 -BM_FakeLua_Popcount_TCC/1000 21,598 ns 13,338 ns 52,591 -BM_FakeLua_Popcount_TCC/10000 268,037 ns 153,982 ns 4,337 -BM_FakeLua_Popcount_TCC/100000 2,767,183 ns 1,913,541 ns 375 -BM_FakeLua_PowMod_GCC/1234567/7654321/1000000007 1,209 ns 759 ns 928,849 -BM_FakeLua_PowMod_GCC/2/1000/1000000007 830 ns 559 ns 1,262,622 -BM_FakeLua_PowMod_GCC/7/1000000/1000000007 957 ns 666 ns 1,059,056 -BM_FakeLua_PowMod_TCC/1234567/7654321/1000000007 1,821 ns 1,084 ns 645,501 -BM_FakeLua_PowMod_TCC/2/1000/1000000007 1,222 ns 720 ns 977,040 -BM_FakeLua_PowMod_TCC/7/1000000/1000000007 1,492 ns 947 ns 733,551 -BM_FakeLua_Recursion_GCC/10 928 ns 578 ns 1,197,261 -BM_FakeLua_Recursion_GCC/20 48,722 ns 30,172 ns 23,410 -BM_FakeLua_Recursion_GCC/25 386,863 ns 269,108 ns 2,586 -BM_FakeLua_Recursion_TCC/10 1,702 ns 1,089 ns 639,131 -BM_FakeLua_Recursion_TCC/20 138,307 ns 79,106 ns 8,834 -BM_FakeLua_Recursion_TCC/25 1,453,790 ns 857,782 ns 801 -BM_FakeLua_Sieve_GCC/100 6,701 ns 3,979 ns 175,646 -BM_FakeLua_Sieve_GCC/1000 63,689 ns 35,024 ns 19,847 -BM_FakeLua_Sieve_GCC/500 29,989 ns 17,088 ns 40,695 -BM_FakeLua_Sieve_GCC/5000 344,444 ns 213,124 ns 3,359 -BM_FakeLua_Sieve_TCC/100 29,560 ns 17,487 ns 39,646 -BM_FakeLua_Sieve_TCC/1000 263,604 ns 172,742 ns 4,041 -BM_FakeLua_Sieve_TCC/500 143,477 ns 85,340 ns 8,216 -BM_FakeLua_Sieve_TCC/5000 1,780,537 ns 1,041,591 ns 679 -BM_FakeLua_StringByte_GCC/10 939 ns 531 ns 1,304,736 -BM_FakeLua_StringByte_GCC/100 951 ns 576 ns 1,226,213 -BM_FakeLua_StringByte_GCC/1000 1,003 ns 603 ns 1,162,886 -BM_FakeLua_StringByte_TCC/10 983 ns 593 ns 1,206,309 -BM_FakeLua_StringByte_TCC/100 1,000 ns 638 ns 1,076,212 -BM_FakeLua_StringByte_TCC/1000 1,140 ns 666 ns 1,037,743 -BM_FakeLua_StringChar_GCC/10 4,717 ns 2,785 ns 252,981 -BM_FakeLua_StringChar_GCC/100 26,150 ns 16,053 ns 43,522 -BM_FakeLua_StringChar_GCC/500 124,214 ns 72,588 ns 9,375 -BM_FakeLua_StringChar_TCC/10 7,145 ns 4,461 ns 157,332 -BM_FakeLua_StringChar_TCC/100 56,569 ns 31,568 ns 22,304 -BM_FakeLua_StringChar_TCC/500 214,876 ns 144,492 ns 4,830 -BM_FakeLua_StringChurn_GCC/100 65,339 ns 44,656 ns 15,766 -BM_FakeLua_StringChurn_GCC/1000 826,591 ns 443,901 ns 1,576 -BM_FakeLua_StringChurn_GCC/500 365,738 ns 223,272 ns 3,136 -BM_FakeLua_StringChurn_TCC/100 90,760 ns 57,091 ns 12,523 -BM_FakeLua_StringChurn_TCC/1000 1,114,236 ns 571,149 ns 1,237 -BM_FakeLua_StringChurn_TCC/500 513,027 ns 283,598 ns 2,459 -BM_FakeLua_StringFindPattern_GCC/1000 11,580,621 ns 7,654,594 ns 91 -BM_FakeLua_StringFindPattern_TCC/1000 13,050,656 ns 7,914,296 ns 88 -BM_FakeLua_StringFind_GCC/10 2,628 ns 1,647 ns 424,630 -BM_FakeLua_StringFind_GCC/100 2,637 ns 1,654 ns 421,490 -BM_FakeLua_StringFind_GCC/1000 2,950 ns 1,723 ns 410,605 -BM_FakeLua_StringFind_GCC/10000 3,955 ns 2,502 ns 276,271 -BM_FakeLua_StringFind_TCC/10 2,650 ns 1,713 ns 409,465 -BM_FakeLua_StringFind_TCC/100 2,782 ns 1,721 ns 404,834 -BM_FakeLua_StringFind_TCC/1000 2,985 ns 1,771 ns 394,892 -BM_FakeLua_StringFind_TCC/10000 3,996 ns 2,588 ns 272,588 -BM_FakeLua_StringFormat_GCC/10 3,354 ns 1,954 ns 356,680 -BM_FakeLua_StringFormat_GCC/100 26,095 ns 15,567 ns 44,584 -BM_FakeLua_StringFormat_GCC/500 119,442 ns 77,076 ns 8,998 -BM_FakeLua_StringFormat_TCC/10 3,333 ns 2,324 ns 299,598 -BM_FakeLua_StringFormat_TCC/100 29,993 ns 18,834 ns 36,855 -BM_FakeLua_StringFormat_TCC/500 160,453 ns 93,895 ns 7,480 -BM_FakeLua_StringGmatch_GCC/1000 8,819,264 ns 5,272,718 ns 130 -BM_FakeLua_StringGmatch_TCC/1000 9,236,472 ns 5,559,361 ns 126 -BM_FakeLua_StringGsub_GCC/10 13,205 ns 8,969 ns 78,419 -BM_FakeLua_StringGsub_GCC/100 103,458 ns 68,161 ns 10,359 -BM_FakeLua_StringGsub_GCC/1000 1,101,351 ns 657,675 ns 1,070 -BM_FakeLua_StringGsub_TCC/10 13,795 ns 8,996 ns 77,772 -BM_FakeLua_StringGsub_TCC/100 114,274 ns 67,941 ns 10,349 -BM_FakeLua_StringGsub_TCC/1000 1,114,296 ns 658,608 ns 1,073 -BM_FakeLua_StringLen_GCC/10 818 ns 492 ns 1,432,371 -BM_FakeLua_StringLen_GCC/100 796 ns 540 ns 1,299,835 -BM_FakeLua_StringLen_GCC/1000 866 ns 561 ns 1,246,589 -BM_FakeLua_StringLen_GCC/10000 1,914 ns 1,077 ns 647,127 -BM_FakeLua_StringLen_TCC/10 877 ns 535 ns 1,273,369 -BM_FakeLua_StringLen_TCC/100 1,032 ns 585 ns 1,189,078 -BM_FakeLua_StringLen_TCC/1000 901 ns 603 ns 1,162,520 -BM_FakeLua_StringLen_TCC/10000 1,808 ns 1,112 ns 601,399 -BM_FakeLua_StringLower_GCC/10 1,048 ns 600 ns 1,152,148 -BM_FakeLua_StringLower_GCC/100 1,393 ns 702 ns 1,001,106 -BM_FakeLua_StringLower_GCC/1000 1,551 ns 846 ns 816,183 -BM_FakeLua_StringLower_GCC/10000 5,043 ns 2,822 ns 251,014 -BM_FakeLua_StringLower_TCC/10 1,073 ns 692 ns 1,016,288 -BM_FakeLua_StringLower_TCC/100 1,855 ns 1,212 ns 583,384 -BM_FakeLua_StringLower_TCC/1000 8,751 ns 5,402 ns 128,947 -BM_FakeLua_StringLower_TCC/10000 86,072 ns 48,207 ns 14,542 -BM_FakeLua_StringRep_GCC/10 1,033 ns 671 ns 1,049,153 -BM_FakeLua_StringRep_GCC/100 1,806 ns 1,100 ns 630,280 -BM_FakeLua_StringRep_GCC/1000 8,126 ns 4,888 ns 141,355 -BM_FakeLua_StringRep_TCC/10 1,328 ns 752 ns 927,065 -BM_FakeLua_StringRep_TCC/100 2,358 ns 1,389 ns 505,237 -BM_FakeLua_StringRep_TCC/1000 10,189 ns 7,129 ns 98,349 -BM_FakeLua_StringReverse_GCC/10 1,109 ns 604 ns 1,148,807 -BM_FakeLua_StringReverse_GCC/100 1,397 ns 765 ns 919,546 -BM_FakeLua_StringReverse_GCC/1000 2,668 ns 1,438 ns 477,188 -BM_FakeLua_StringReverse_GCC/10000 14,935 ns 8,698 ns 78,927 -BM_FakeLua_StringReverse_TCC/10 925 ns 681 ns 1,037,089 -BM_FakeLua_StringReverse_TCC/100 1,870 ns 1,066 ns 657,331 -BM_FakeLua_StringReverse_TCC/1000 6,110 ns 4,063 ns 172,547 -BM_FakeLua_StringReverse_TCC/10000 52,709 ns 34,603 ns 20,307 -BM_FakeLua_StringSub_GCC/10 2,264 ns 1,345 ns 518,241 -BM_FakeLua_StringSub_GCC/100 2,557 ns 1,430 ns 489,733 -BM_FakeLua_StringSub_GCC/1000 2,763 ns 1,490 ns 466,344 -BM_FakeLua_StringSub_GCC/10000 4,070 ns 2,268 ns 306,435 -BM_FakeLua_StringSub_TCC/10 2,372 ns 1,390 ns 508,219 -BM_FakeLua_StringSub_TCC/100 2,314 ns 1,485 ns 474,333 -BM_FakeLua_StringSub_TCC/1000 3,228 ns 1,543 ns 458,387 -BM_FakeLua_StringSub_TCC/10000 3,832 ns 2,334 ns 301,764 -BM_FakeLua_StringUpper_GCC/10 1,127 ns 601 ns 1,168,948 -BM_FakeLua_StringUpper_GCC/100 1,114 ns 702 ns 992,199 -BM_FakeLua_StringUpper_GCC/1000 1,386 ns 850 ns 820,082 -BM_FakeLua_StringUpper_GCC/10000 5,104 ns 2,832 ns 248,824 -BM_FakeLua_StringUpper_TCC/10 1,169 ns 696 ns 1,006,148 -BM_FakeLua_StringUpper_TCC/100 2,031 ns 1,214 ns 575,272 -BM_FakeLua_StringUpper_TCC/1000 10,406 ns 5,421 ns 129,932 -BM_FakeLua_StringUpper_TCC/10000 81,506 ns 48,151 ns 14,489 -BM_FakeLua_Sum_GCC/10000 6,452 ns 3,508 ns 199,582 -BM_FakeLua_Sum_GCC/100000 50,566 ns 31,381 ns 22,327 -BM_FakeLua_Sum_GCC/1000000 578,521 ns 309,437 ns 2,250 -BM_FakeLua_Sum_GCC/5000000 3,098,218 ns 1,547,859 ns 453 -BM_FakeLua_Sum_TCC/10000 37,857 ns 24,934 ns 27,821 -BM_FakeLua_Sum_TCC/100000 417,183 ns 246,397 ns 2,850 -BM_FakeLua_Sum_TCC/1000000 4,479,399 ns 2,452,786 ns 283 -BM_FakeLua_Sum_TCC/5000000 18,728,703 ns 12,321,859 ns 56 -BM_FakeLua_TableChurn_GCC/100 11,409 ns 6,971 ns 100,756 -BM_FakeLua_TableChurn_GCC/1000 119,994 ns 65,155 ns 10,754 -BM_FakeLua_TableChurn_GCC/500 43,896 ns 33,036 ns 21,217 -BM_FakeLua_TableChurn_TCC/100 57,082 ns 36,778 ns 19,044 -BM_FakeLua_TableChurn_TCC/1000 466,370 ns 354,670 ns 1,972 -BM_FakeLua_TableChurn_TCC/500 281,725 ns 182,147 ns 3,900 -BM_FakeLua_TableConcat_GCC/100 58,710 ns 38,108 ns 18,314 -BM_FakeLua_TableConcat_GCC/1000 708,780 ns 365,628 ns 1,886 -BM_FakeLua_TableConcat_GCC/500 309,868 ns 185,102 ns 3,781 -BM_FakeLua_TableConcat_TCC/100 101,629 ns 54,515 ns 13,123 -BM_FakeLua_TableConcat_TCC/1000 914,385 ns 523,737 ns 1,328 -BM_FakeLua_TableConcat_TCC/500 487,979 ns 262,511 ns 2,665 -BM_FakeLua_TableCreate_GCC/1000 33,890 ns 20,427 ns 34,468 -BM_FakeLua_TableCreate_GCC/3000 117,469 ns 69,451 ns 10,059 -BM_FakeLua_TableCreate_GCC/5000 251,652 ns 128,429 ns 5,479 -BM_FakeLua_TableCreate_TCC/1000 193,548 ns 117,177 ns 6,020 -BM_FakeLua_TableCreate_TCC/3000 605,946 ns 392,114 ns 1,794 -BM_FakeLua_TableCreate_TCC/5000 1,235,125 ns 705,496 ns 988 -BM_FakeLua_TableInsert_GCC/100 5,796 ns 3,230 ns 217,272 -BM_FakeLua_TableInsert_GCC/1000 37,577 ns 23,234 ns 30,067 -BM_FakeLua_TableInsert_GCC/500 18,496 ns 12,007 ns 57,879 -BM_FakeLua_TableInsert_GCC/5000 230,109 ns 141,009 ns 4,927 -BM_FakeLua_TableInsert_TCC/100 25,990 ns 14,008 ns 51,081 -BM_FakeLua_TableInsert_TCC/1000 190,463 ns 121,881 ns 5,882 -BM_FakeLua_TableInsert_TCC/500 122,192 ns 62,347 ns 11,427 -BM_FakeLua_TableInsert_TCC/5000 1,128,122 ns 726,417 ns 929 -BM_FakeLua_TableMove_GCC/100 8,264 ns 5,510 ns 123,002 -BM_FakeLua_TableMove_GCC/1000 65,516 ns 40,768 ns 17,565 -BM_FakeLua_TableMove_GCC/500 31,437 ns 20,959 ns 33,710 -BM_FakeLua_TableMove_GCC/5000 415,416 ns 250,364 ns 2,806 -BM_FakeLua_TableMove_TCC/100 40,035 ns 23,025 ns 30,393 -BM_FakeLua_TableMove_TCC/1000 291,431 ns 196,812 ns 3,556 -BM_FakeLua_TableMove_TCC/500 148,661 ns 99,540 ns 7,076 -BM_FakeLua_TableMove_TCC/5000 1,689,098 ns 1,175,769 ns 595 -BM_FakeLua_TablePack_GCC/1 1,240 ns 784 ns 894,882 -BM_FakeLua_TablePack_TCC/1 3,413 ns 1,830 ns 383,737 -BM_FakeLua_TableRemove_GCC/100 10,904 ns 5,496 ns 128,245 -BM_FakeLua_TableRemove_GCC/1000 84,466 ns 46,212 ns 15,196 -BM_FakeLua_TableRemove_GCC/500 37,355 ns 23,386 ns 28,737 -BM_FakeLua_TableRemove_GCC/5000 430,685 ns 256,491 ns 2,710 -BM_FakeLua_TableRemove_TCC/100 41,184 ns 23,119 ns 30,006 -BM_FakeLua_TableRemove_TCC/1000 419,694 ns 215,409 ns 3,251 -BM_FakeLua_TableRemove_TCC/500 177,077 ns 107,216 ns 6,561 -BM_FakeLua_TableRemove_TCC/5000 1,793,838 ns 1,185,962 ns 589 -BM_FakeLua_TableSort_GCC/100 44,298 ns 28,004 ns 25,024 -BM_FakeLua_TableSort_GCC/1000 562,565 ns 330,027 ns 2,138 -BM_FakeLua_TableSort_GCC/500 271,424 ns 154,615 ns 4,519 -BM_FakeLua_TableSort_TCC/100 66,261 ns 39,107 ns 17,856 -BM_FakeLua_TableSort_TCC/1000 730,477 ns 431,083 ns 1,617 -BM_FakeLua_TableSort_TCC/500 306,301 ns 206,069 ns 3,371 -BM_FakeLua_TailRecursion_GCC/100 777 ns 426 ns 1,648,769 -BM_FakeLua_TailRecursion_GCC/1000 1,205 ns 712 ns 979,895 -BM_FakeLua_TailRecursion_GCC/5000 3,796 ns 1,951 ns 358,052 -BM_FakeLua_TailRecursion_TCC/100 1,584 ns 927 ns 759,379 -BM_FakeLua_TailRecursion_TCC/1000 9,233 ns 5,203 ns 100,000 -BM_FakeLua_TailRecursion_TCC/5000 42,186 ns 24,371 ns 29,042 -BM_FakeLua_ToNumber_GCC/1 802 ns 542 ns 1,304,733 -BM_FakeLua_ToNumber_TCC/1 1,150 ns 693 ns 1,016,080 -BM_FakeLua_ToString_GCC/10 1,194 ns 647 ns 1,097,100 -BM_FakeLua_ToString_GCC/100 979 ns 646 ns 1,079,352 -BM_FakeLua_ToString_GCC/500 1,059 ns 650 ns 1,080,277 -BM_FakeLua_ToString_TCC/10 1,174 ns 701 ns 1,016,080 -BM_FakeLua_ToString_TCC/100 1,203 ns 700 ns 987,485 -BM_FakeLua_ToString_TCC/500 959 ns 701 ns 999,183 -BM_FakeLua_Variadic_GCC/1 1,215 ns 754 ns 928,022 -BM_FakeLua_Variadic_TCC/1 2,233 ns 1,224 ns 575,243 -BM_FakeLua_Vector3_GCC/10000 607,809 ns 309,974 ns 2,261 -BM_FakeLua_Vector3_GCC/100000 4,839,630 ns 3,056,228 ns 228 -BM_FakeLua_Vector3_GCC/1000000 47,773,627 ns 30,897,897 ns 22 -BM_FakeLua_Vector3_TCC/10000 3,344,400 ns 2,091,722 ns 331 -BM_FakeLua_Vector3_TCC/100000 42,147,447 ns 21,209,412 ns 33 -BM_FakeLua_Vector3_TCC/1000000 347,663,874 ns 210,547,754 ns 3 -BM_Lua_BinarySearch/100 60,634 ns 37,029 ns 18,896 -BM_Lua_BinarySearch/1000 959,697 ns 580,933 ns 1,221 -BM_Lua_BinarySearch/500 440,300 ns 254,492 ns 2,758 -BM_Lua_BubbleSort/100 594,801 ns 338,786 ns 2,093 -BM_Lua_BubbleSort/200 2,086,556 ns 1,328,612 ns 534 -BM_Lua_BubbleSort/50 164,153 ns 85,624 ns 8,213 -BM_Lua_Closure/100 39,957 ns 22,696 ns 30,670 -BM_Lua_Closure/1000 348,764 ns 226,293 ns 3,086 -BM_Lua_EmptyCall/10000 722,370 ns 439,736 ns 1,597 -BM_Lua_EmptyCall/100000 7,933,740 ns 4,354,814 ns 159 -BM_Lua_FastPow/1234567/7654321/1000000007 1,930 ns 1,152 ns 594,367 -BM_Lua_FastPow/2/1000/1000000007 1,039 ns 593 ns 1,199,681 -BM_Lua_FastPow/7/1000000/1000000007 1,725 ns 974 ns 722,218 -BM_Lua_Fibonacci/20 1,569,773 ns 860,801 ns 824 -BM_Lua_Fibonacci/25 15,600,871 ns 9,626,976 ns 72 -BM_Lua_Fibonacci/30 168,261,430 ns 106,484,626 ns 7 -BM_Lua_Fibonacci/32 503,047,483 ns 277,915,868 ns 3 -BM_Lua_FloatPoly/1000000 195,928,101 ns 99,944,530 ns 7 -BM_Lua_GCD/123456789/987654321 337 ns 212 ns 3,268,053 -BM_Lua_GCD/2147483647/1073741823 370 ns 191 ns 3,676,078 -BM_Lua_GCD/832040/514229 1,132 ns 730 ns 942,430 -BM_Lua_HashInsert/100 87,286 ns 48,607 ns 14,714 -BM_Lua_HashInsert/1000 786,406 ns 499,118 ns 1,378 -BM_Lua_HashInsert/500 378,706 ns 241,489 ns 2,867 -BM_Lua_HashLookup/100 68,701 ns 39,114 ns 17,775 -BM_Lua_HashLookup/1000 649,131 ns 414,686 ns 1,713 -BM_Lua_HashLookup/500 315,722 ns 205,240 ns 3,408 -BM_Lua_InsertionSort/100 389,213 ns 228,283 ns 3,094 -BM_Lua_InsertionSort/200 1,452,926 ns 894,653 ns 782 -BM_Lua_InsertionSort/50 92,414 ns 59,708 ns 11,653 -BM_Lua_MatMul 5,810 ns 3,698 ns 190,328 -BM_Lua_MathExpLog/100000 30,397,983 ns 17,460,595 ns 41 -BM_Lua_MathMinMax/100000 36,855,906 ns 22,431,932 ns 31 -BM_Lua_MathSqrt/100000 11,202,131 ns 6,162,470 ns 112 -BM_Lua_MathTrig/100000 28,341,267 ns 17,880,549 ns 39 -BM_Lua_MixedAlloc/100 148,240 ns 87,270 ns 8,030 -BM_Lua_MixedAlloc/1000 1,718,443 ns 897,348 ns 810 -BM_Lua_MixedAlloc/500 703,958 ns 442,235 ns 1,559 -BM_Lua_MultiReturn/1000 96,520 ns 59,128 ns 11,867 -BM_Lua_MultiReturn/10000 926,960 ns 582,937 ns 1,229 -BM_Lua_NestedTable/1000 466,508 ns 292,829 ns 2,366 -BM_Lua_NestedTable/10000 5,063,234 ns 2,900,521 ns 240 -BM_Lua_Popcount/1000 240,702 ns 144,200 ns 4,801 -BM_Lua_Popcount/10000 2,783,275 ns 1,828,025 ns 382 -BM_Lua_Popcount/100000 35,208,870 ns 22,105,500 ns 32 -BM_Lua_PowMod/1234567/7654321/1000000007 2,090 ns 1,299 ns 544,422 -BM_Lua_PowMod/2/1000/1000000007 1,028 ns 661 ns 1,072,320 -BM_Lua_PowMod/7/1000000/1000000007 2,075 ns 1,100 ns 631,710 -BM_Lua_Recursion/10 13,957 ns 8,364 ns 81,503 -BM_Lua_Recursion/20 1,412,171 ns 1,043,444 ns 689 -BM_Lua_Recursion/25 17,663,822 ns 11,536,223 ns 61 -BM_Lua_Sieve/100 15,105 ns 9,868 ns 70,790 -BM_Lua_Sieve/1000 131,859 ns 75,406 ns 9,411 -BM_Lua_Sieve/500 59,479 ns 39,012 ns 17,749 -BM_Lua_Sieve/5000 537,121 ns 378,408 ns 1,865 -BM_Lua_StringByte/10 498 ns 303 ns 2,260,816 -BM_Lua_StringByte/100 760 ns 460 ns 1,530,782 -BM_Lua_StringByte/1000 927 ns 624 ns 1,147,141 -BM_Lua_StringChar/10 4,724 ns 3,004 ns 233,911 -BM_Lua_StringChar/100 26,038 ns 17,295 ns 40,665 -BM_Lua_StringChar/500 115,847 ns 75,332 ns 9,010 -BM_Lua_StringChurn/100 124,238 ns 72,452 ns 9,398 -BM_Lua_StringChurn/1000 1,713,625 ns 971,816 ns 707 -BM_Lua_StringChurn/500 726,842 ns 474,138 ns 1,477 -BM_Lua_StringFind/10 816 ns 457 ns 1,510,131 -BM_Lua_StringFind/100 1,002 ns 571 ns 1,225,626 -BM_Lua_StringFind/1000 1,076 ns 756 ns 933,030 -BM_Lua_StringFind/10000 3,359 ns 2,139 ns 327,326 -BM_Lua_StringFindPattern/1000 1,536,606 ns 938,600 ns 753 -BM_Lua_StringFormat/10 4,810 ns 2,816 ns 249,862 -BM_Lua_StringFormat/100 45,648 ns 28,036 ns 25,220 -BM_Lua_StringFormat/500 217,375 ns 138,971 ns 5,005 -BM_Lua_StringGmatch/1000 1,797,588 ns 1,101,192 ns 634 -BM_Lua_StringGsub/10 1,551 ns 942 ns 743,199 -BM_Lua_StringGsub/100 7,349 ns 4,529 ns 155,040 -BM_Lua_StringGsub/1000 67,386 ns 37,743 ns 18,523 -BM_Lua_StringLen/10 255 ns 169 ns 4,208,529 -BM_Lua_StringLen/100 512 ns 317 ns 2,210,217 -BM_Lua_StringLen/1000 625 ns 491 ns 1,417,701 -BM_Lua_StringLen/10000 2,719 ns 1,979 ns 358,597 -BM_Lua_StringLower/10 596 ns 322 ns 2,189,914 -BM_Lua_StringLower/100 1,225 ns 747 ns 942,770 -BM_Lua_StringLower/1000 3,564 ns 2,181 ns 320,649 -BM_Lua_StringLower/10000 29,966 ns 17,111 ns 41,238 -BM_Lua_StringRep/10 737 ns 408 ns 1,705,699 -BM_Lua_StringRep/100 1,822 ns 997 ns 694,622 -BM_Lua_StringRep/1000 9,142 ns 5,390 ns 126,264 -BM_Lua_StringReverse/10 597 ns 307 ns 2,261,963 -BM_Lua_StringReverse/100 1,182 ns 706 ns 961,826 -BM_Lua_StringReverse/1000 3,323 ns 1,913 ns 365,656 -BM_Lua_StringReverse/10000 24,703 ns 14,399 ns 48,160 -BM_Lua_StringSub/10 617 ns 364 ns 1,942,516 -BM_Lua_StringSub/100 1,105 ns 594 ns 1,177,842 -BM_Lua_StringSub/1000 1,379 ns 906 ns 766,324 -BM_Lua_StringSub/10000 4,908 ns 3,045 ns 231,995 -BM_Lua_StringUpper/10 567 ns 323 ns 2,190,345 -BM_Lua_StringUpper/100 1,342 ns 735 ns 951,815 -BM_Lua_StringUpper/1000 3,109 ns 1,965 ns 355,243 -BM_Lua_StringUpper/10000 27,760 ns 14,242 ns 48,029 -BM_Lua_Sum/10000 180,892 ns 92,068 ns 7,391 -BM_Lua_Sum/100000 1,380,171 ns 911,977 ns 771 -BM_Lua_Sum/1000000 16,461,078 ns 9,233,507 ns 76 -BM_Lua_Sum/5000000 74,960,840 ns 46,977,732 ns 15 -BM_Lua_TableChurn/100 88,809 ns 61,258 ns 11,328 -BM_Lua_TableChurn/1000 820,002 ns 602,970 ns 1,148 -BM_Lua_TableChurn/500 477,274 ns 305,247 ns 2,384 -BM_Lua_TableConcat/100 71,908 ns 36,736 ns 19,340 -BM_Lua_TableConcat/1000 585,304 ns 361,357 ns 1,936 -BM_Lua_TableConcat/500 325,897 ns 177,956 ns 3,874 -BM_Lua_TableCreate/1000 47,111 ns 29,524 ns 23,831 -BM_Lua_TableCreate/3000 138,745 ns 86,404 ns 8,046 -BM_Lua_TableCreate/5000 257,284 ns 145,201 ns 4,849 -BM_Lua_TableInsert/100 23,857 ns 13,362 ns 53,418 -BM_Lua_TableInsert/1000 208,845 ns 116,643 ns 5,895 -BM_Lua_TableInsert/500 107,318 ns 57,789 ns 11,379 -BM_Lua_TableInsert/5000 928,408 ns 564,608 ns 1,170 -BM_Lua_TableMove/100 12,817 ns 8,743 ns 81,064 -BM_Lua_TableMove/1000 92,904 ns 50,879 ns 10,000 -BM_Lua_TableMove/500 44,285 ns 28,002 ns 25,009 -BM_Lua_TableMove/5000 355,241 ns 250,016 ns 2,787 -BM_Lua_TablePack/1 2,840 ns 1,516 ns 462,846 -BM_Lua_TableRemove/100 24,388 ns 17,343 ns 40,851 -BM_Lua_TableRemove/1000 261,457 ns 152,441 ns 4,709 -BM_Lua_TableRemove/500 126,597 ns 76,961 ns 9,300 -BM_Lua_TableRemove/5000 1,354,665 ns 828,342 ns 831 -BM_Lua_TableSort/100 42,572 ns 30,042 ns 23,123 -BM_Lua_TableSort/1000 714,671 ns 398,060 ns 1,749 -BM_Lua_TableSort/500 289,863 ns 184,677 ns 3,803 -BM_Lua_TailRecursion/100 8,300 ns 4,805 ns 160,595 -BM_Lua_TailRecursion/1000 73,087 ns 42,646 ns 15,958 -BM_Lua_TailRecursion/5000 346,152 ns 209,783 ns 3,406 -BM_Lua_ToNumber/1 529 ns 285 ns 2,447,960 -BM_Lua_ToString/10 903 ns 478 ns 1,479,778 -BM_Lua_ToString/100 922 ns 481 ns 1,460,211 -BM_Lua_ToString/500 739 ns 477 ns 1,451,251 -BM_Lua_Variadic/1 1,069 ns 685 ns 1,026,991 -BM_Lua_Vector3/10000 2,302,920 ns 1,493,404 ns 464 -BM_Lua_Vector3/100000 24,833,907 ns 14,947,118 ns 46 -BM_Lua_Vector3/1000000 239,352,621 ns 149,734,385 ns 5 -``` diff --git a/benchmark/README.zh.md b/benchmark/README.zh.md index a0ee3e09..0ddb90af 100644 --- a/benchmark/README.zh.md +++ b/benchmark/README.zh.md @@ -1,1130 +1,102 @@ # 性能基准测试结果 -本文件记录在本地以 **Release 模式**(`-O3 -DNDEBUG`)编译运行 `bench_mark` 的结果。覆盖 **6 大类共 51 个 Lua 性能场景**,每个场景均实现 C++ / Lua 5.4 / FakeLua TCC / FakeLua GCC / FakeLua INTERP(字节码解释器)横向对比。下面分析分成两块:**解释器 vs Lua 5.4** 和 **GCC JIT vs Lua / C++**。表操作场景已更新为引入连续整数键数组段(`VarTable::arr_`)后的最新测量数据。 +本文件记录 2026-11-30 在本机以 Release(`-O3 -DNDEBUG`)运行 `build/bin/bench_mark` 的 CPU 时间。旧文档里的「解释器没有尾调用优化」和「`string.gsub` 走 ECMAScript Boost.Regex」已经过时,下面的数字取代它们。 -## 运行命令 +## 运行环境 -```bash -cmake -S . -B build -DCMAKE_BUILD_TYPE=Release -DFAKELUA_BUILD_BENCHMARKS=ON -cmake --build build --target bench_mark --parallel -# 只跑 Lua 5.4 与字节码解释器: -build/bin/bench_mark --benchmark_filter='BM_Lua_|BM_FakeLua_.*_INTERP' \ - --benchmark_repetitions=1 --benchmark_report_aggregates_only=true -# 全量(C++ / Lua / TCC / GCC / INTERP): -build/bin/bench_mark --benchmark_repetitions=1 --benchmark_report_aggregates_only=true -``` - ---- - -## 解释器 vs Lua 5.4(2026-09-16) - -字节码 VM(`JIT_INTERP`):8 字节 `Inst`、GNU computed-goto 派发、Lua 5.4 风格整数 `FORPREP`/`FORLOOP` 倒计数、就地写回 `ADD`。生产路径仍是 **JIT_GCC**,不是解释器。 - -### 运行环境(INTERP 这次) - -- 日期:2026-09-16 -- 机器:AMD EPYC 7551 32-Core Processor,16 X 2000 MHz CPU s(KVM) -- CPU 缓存:L1d 64 KiB (x16),L1i 64 KiB (x16),L2 512 KiB (x16),L3 65536 KiB (x2) -- 构建模式:**Release**(`-O3 -DNDEBUG`),GCC 16.2.0 +- 日期:2026-11-30 +- 机器:AMD EPYC 7551 32-Core Processor,16 vCPU,2.0 GHz +- 构建:GCC 16.2.0,Release `-O3 -DNDEBUG` - Lua:5.4.7 -- FakeLua INTERP:`debug_mode=false`(初始化仍会编 TCC/GCC;计时路径是 `Call(..., JIT_INTERP, ...)`) -- 二进制:`build/bin/bench_mark` - -> 绝对耗时强依赖于机器与编译器版本,跨环境不可比。**INTERP vs Lua** = Lua CPU 时间 / INTERP CPU 时间(>1 表示解释器更快)。**INTERP / Lua 耗时** 列是 INTERP / Lua(解释器相对 Lua 慢几倍)。只有本节表内的比值有意义,不要和下面 2026-08-14 的 GCC 表混比。 - -各场景取最大参数: - -### 算法(algo) - -| 场景 | 参数 | INTERP vs Lua | INTERP / Lua 耗时 | 备注 | -|------|------|---------------|-------------------|------| -| Fibonacci | n=32 | 0.13x | **7.6x** | 递归 `CALL`;Lua 单次调用便宜得多 | -| GCD | 2147483647/1073741823 | 0.29x | 3.4x | 函数体极小,派发+装箱主导 | -| PowMod | 1234567/7654321/1e9+7 | 0.56x | 1.8x | | -| Sum | n=5M | 0.69x | **1.5x** | 最接近的数值循环(就地 `ADD` + 倒计数 `FORLOOP`) | -| BubbleSort | n=200 | 0.29x | 3.4x | 表下标读写,无 array part | -| Sieve | n=5000 | 0.30x | 3.3x | | -| BinarySearch | n=1000 | 0.33x | 3.1x | | -| FastPow | 1234567/7654321/1e9+7 | 0.48x | 2.1x | | -| Popcount | n=100K | 0.33x | 3.0x | | -| InsertionSort | n=200 | 0.22x | 4.5x | | -| MatMul | 3×3 | 0.51x | 1.9x | | -| Vector3 | n=1M | 0.29x | 3.5x | 纯哈希表 vs Lua array part | -| FloatPoly | n=1M | 0.77x | **1.3x** | 紧浮点循环,接近 Sum | - -### 字符串(string) - -| 场景 | 参数 | INTERP vs Lua | INTERP / Lua 耗时 | 备注 | -|------|------|---------------|-------------------|------| -| StringLen | n=10K | **2.3x** | 0.44x | 工作在 C(`string.len`),解释器更快 | -| StringSub | n=10K | 1.86x | 0.54x | | -| StringRep | n=1000 | 1.08x | 0.93x | 基本持平 | -| StringReverse | n=10K | 1.13x | 0.89x | | -| StringLower | n=10K | **6.2x** | 0.16x | C 原生 `string.lower` | -| StringUpper | n=10K | **6.1x** | 0.16x | 同上 | -| StringByte | n=1000 | 0.77x | 1.3x | | -| StringChar | n=500 | 0.44x | 2.3x | | -| StringFormat | n=500 | 0.78x | 1.3x | | -| StringFind | n=10K | 1.23x | 0.82x | 普通子串 | -| StringGsub | n=1000 | 0.31x | 3.2x | ECMAScript Boost.Regex | -| ToNumber | n=1 | 0.34x | 2.9x | 单次 `Call()` + 装箱 | -| ToString | n=500 | 0.74x | 1.4x | | -| StringFindPattern | n=1000 | 0.25x | 4.0x | ECMAScript 正则 | -| StringGmatch | n=1000 | 0.55x | 1.8x | ECMAScript 正则 | - -### 表操作(table) - -| 场景 | 参数 | INTERP vs Lua | INTERP / Lua 耗时 | 备注 | -|------|------|---------------|-------------------|------| -| TableInsert | n=5K | 0.38x | 2.6x | 包含数组段优化(原 3.5x) | -| TableRemove | n=5K | 0.55x | 1.8x | 包含数组段优化(原 2.4x) | -| TableConcat | n=1000 | 1.31x | 0.76x | 快于 Lua(C concat) | -| TablePack | n=1 | 1.66x | 0.60x | 快于 Lua | -| TableMove | n=5K | 0.75x | 1.3x | 包含数组段优化(原 2.6x) | -| TableSort | n=1000 | 3.17x | 0.32x | C `table.sort`(比 Lua 快 3.2 倍) | -| TableCreate | n=5K | 0.71x | 1.4x | 包含数组段优化(原 3.3x) | -| HashInsert | n=1000 | 1.53x | 0.65x | Arena 堆分配 vs Lua GC | -| HashLookup | n=1000 | 1.17x | 0.85x | | -| NestedTable | n=10K | 0.42x | 2.4x | 包含数组段优化(原 4.0x) | - -### 函数调用(function) - -| 场景 | 参数 | INTERP vs Lua | INTERP / Lua 耗时 | 备注 | -|------|------|---------------|-------------------|------| -| EmptyCall | n=100K | 0.13x | **7.8x** | 每次 `CallByName` / 进解释器 | -| Recursion | n=25 | 0.17x | **6.0x** | 同 Fibonacci | -| Variadic | n=1 | 0.19x | 5.2x | `select` / 变参 | -| MultiReturn | n=10K | 0.18x | 5.6x | | -| Closure | n=1000 | 0.36x | 2.7x | | -| TailRecursion | n=5K | 0.03x | **34x** | 无 TCO,真 C 递归(需 `ulimit -s unlimited`) | - -### GC 与内存压力(gc) - -| 场景 | 参数 | INTERP vs Lua | INTERP / Lua 耗时 | 备注 | -|------|------|---------------|-------------------|------| -| TableChurn | n=1000 | **2.2x** | 0.45x | arena,无 GC | -| StringChurn | n=1000 | 1.40x | 0.71x | | -| MixedAlloc | n=1000 | 1.72x | 0.58x | | - -### 数学函数(math) - -| 场景 | 参数 | INTERP vs Lua | INTERP / Lua 耗时 | 备注 | -|------|------|---------------|-------------------|------| -| MathTrig (sin+cos) | n=100K | 0.49x | 2.0x | 原生 math + 解释器循环 | -| MathSqrt | n=100K | 0.33x | 3.1x | | -| MathExpLog | n=100K | 0.42x | 2.4x | | -| MathMinMax | n=100K | 0.43x | 2.3x | | - -### 解释器 vs Lua — 核心发现 - -1. **紧整数/浮点循环最接近 Lua**:Sum n=5M 是 Lua 的 **1.5 倍时间**,FloatPoly n=1M 是 **1.3 倍**。剩下主要是带标签的 `CVar` 拷贝和每次寄存器读写的 `boxes[]` 检查。 - -2. **调用密集大约慢 6–8 倍**:Fibonacci / Recursion / EmptyCall。Lua 的调用是一条 VM 指令;FakeLua INTERP 每次都重新进入 `Call()` / `CallByName` 并装箱参数。 - -3. **尾递归是离群点(34 倍)**:Lua 5.4 把 `return f(...)` 收成循环。INTERP 没有;n=5000 是 5000 层 C 栈(默认 8 MiB 会溢出)。 - -4. **数组段(Array Part)显著缩小表操作差距**:引入连续整数键数组段后,解释器下表循环和表内置函数性能明显改善,相对 Lua 的耗时从过去的 3–4 倍收窄至 1.3–2.6 倍(`table.sort` 快 3.2 倍,`TableConcat` 快 1.3 倍,`TableInsert`/`TableRemove` 显著优化)。 - -5. **落到 C 标准库的工作可以快过 Lua**:`string.lower`/`upper` **6.2 倍**、`string.len` 2.3 倍、`table.sort` 1.9 倍、TableChurn 2.2 倍(arena)。操作码循环不是瓶颈时,原生辅助函数加上无 GC 分配会赢。 - -6. **正则仍然更慢**(Gsub 为 Lua 时间的 3.2 倍,FindPattern 4.0 倍):走 ECMAScript Boost.Regex,不是 Lua pattern——和 GCC JIT 同一取舍。 - ---- - -## JIT GCC vs Lua / C++(2026-08-14) - -下面表格来自**另一次**、**另一台机器**的运行(AMD EPYC 7K62,GCC 15.1.0),**不能**和上面的 INTERP 数字混比。**GCC vs Lua** 表示 FakeLua GCC 相对 Lua 5.4 的加速倍数(>1 表示 GCC JIT 更快);**GCC vs C++** 表示 FakeLua GCC 与手写 C++ 的耗时比值(<1 表示 FakeLua 更快)。 - -### 运行环境(GCC 那次) - -- 日期:2026-08-14 -- 机器:AMD EPYC 7K62 48-Core Processor,2 X 2595.12 MHz CPU s -- CPU 缓存:L1d 32 KiB (x2),L1i 32 KiB (x2),L2 4096 KiB (x2),L3 16384 KiB (x1) -- 构建模式:**Release**(`-O3 -DNDEBUG`),GCC 15.1.0 -- FakeLua GCC JIT:**Release 模式**(`debug_mode=false`,GCC `-O3` 优化) -- 二进制:`build/bin/bench_mark` - -> C++ 参照实现在部分场景会被编译器整体折叠(见下文标注),此时 GCC vs C++ 一列无参考价值。 - -### 算法(algo) - -| 场景 | 参数 | GCC vs Lua | GCC vs C++ | 备注 | -|------|------|-----------|-----------|------| -| Fibonacci | n=32 | **36.6x** | 0.13 | 数值特化,接近 C++ | -| GCD | 2147483647/1073741823 | 0.45x | 10.8 | 单次调用仅约 0.4 µs,被调用开销主导 | -| PowMod | 1234567/7654321/1e9+7 | 1.7x | 2.56 | | -| Sum | n=5M | **30.4x** | 0.06 | GCC 向量化,远快于 C++ | -| BubbleSort | n=200 | 1.9x | 2.80 | 表下标读写拖累 | -| Sieve | n=5000 | 1.8x | 2.68 | | -| BinarySearch | n=1000 | 3.7x | 2.40 | | -| FastPow | 1234567/7654321/1e9+7 | 1.6x | 2.45 | | -| Popcount | n=100K | **37.3x** | 0.12 | 位运算极致优化 | -| InsertionSort | n=200 | 2.6x | 2.92 | | -| MatMul | 3×3 | 2.9x | 5.10 | | -| Vector3 | n=1M | 4.8x | 5.85 | 表特化为结构体,指针偏移 | -| FloatPoly | n=1M | **34.9x** | 0.49 | 浮点特化,GCC 2x 快于 C++ | - -### 字符串(string) - -| 场景 | 参数 | GCC vs Lua | GCC vs C++ | 备注 | -|------|------|-----------|-----------|------| -| StringLen | n=10K | 1.8x | 67.3 | | -| StringSub | n=10K | 1.3x | 6.15 | | -| StringRep | n=1000 | 1.1x | 0.16 | CGen `FlStringRep` 内联 | -| StringReverse | n=10K | 1.7x | 0.26 | CGen `FlStringReverse` 内联 | -| StringLower | n=10K | **6.1x** | 0.02 | CGen 内联 + ASCII 单遍 | -| StringUpper | n=10K | **5.0x** | 0.02 | 同上 | -| StringByte | n=1000 | 1.0x | 37.7 | | -| StringChar | n=500 | 1.0x | 10.6 | | -| StringFormat | n=500 | 1.8x | 2.71 | 常量 `"%d"` → `FlFormatInt` | -| StringFind | n=10K | 0.85x | 19.2 | plain 子串查找(`FlStringFindPlain` 内联,但仍有调用开销) | -| StringGsub | n=1000 | 0.06x | 38.1 | ECMAScript 正则(见下) | -| ToNumber | n=1 | 0.53x | 5.65 | CGen `FlTonumber` 十进制整数内联解析(同一输入 `"1234567890"`) | -| ToString | n=500 | 0.74x | 0.01 | INT → `FlFormatInt` | -| StringFindPattern | n=1000 | 0.12x | 39.6 | ECMAScript 正则;已加编译缓存 | -| StringGmatch | n=1000 | 0.21x | 23.3 | ECMAScript 正则;已加编译缓存 | - -> **关于正则比 Lua 慢**:FakeLua 的 `string.find` / `match` / `gmatch` / `gsub` 走的是 **ECMAScript Boost.Regex**(已做进程级编译缓存),能力强于 Lua 5.4 自带的 pattern(lookahead、完整字符类、非贪婪等)。因此正则场景慢于 Lua(当前约 0.06~0.21x)**可以接受**,属于能力换性能;后续若要追平 Lua,方向是另做 Lua pattern 引擎,而不是继续抠 Boost.Regex。脚本统一用 `[0-9]+`(在 Lua pattern 与 ECMAScript 正则中语义一致)。 - -### 表操作(table) - -| 场景 | 参数 | GCC vs Lua | GCC vs C++ | 备注 | -|------|------|-----------|-----------|------| -| TableInsert | n=5K | **9.8x** | 16.5 | 引入数组段(原 4.0x) | -| TableRemove | n=5K | **12.2x** | 29.5 | 引入数组段(原 3.2x) | -| TableConcat | n=1000 | 1.4x | 15.0 | arena 一次写入 | -| TablePack | n=1 | 3.7x | 1229 | | -| TableMove | n=5K | 1.9x | 33.1 | 支持数组段直拷 | -| TableSort | n=1000 | 4.6x | 4.62 | 原生排序 | -| TableCreate | n=5K | 2.7x | 14.7 | 数组段连续填充 | -| HashInsert | n=1000 | 2.4x | 1.34 | | -| HashLookup | n=1000 | 2.4x | 0.82 | 快于原生 C++ map 查找 | -| NestedTable | n=10K | 3.8x | 10.0 | 数组段整数快速索引 | - -### 函数调用(function) - -| 场景 | 参数 | GCC vs Lua | GCC vs C++ | 备注 | -|------|------|-----------|-----------|------| -| EmptyCall | n=100K | **14.1x** | 61561 | C++ 内联为 0 | -| Recursion | n=25 | **42.9x** | 0.13 | 数值特化 | -| Variadic | n=1 | 0.91x | 75.4 | vararg 构建开销大 | -| MultiReturn | n=10K | **23.3x** | 0.46 | | -| Closure | n=1000 | 2.1x | 19.4 | | -| TailRecursion | n=5K | **107.5x** | 0.07 | 尾调用转循环+向量化 | - -### GC 与内存压力(gc) - -| 场景 | 参数 | GCC vs Lua | GCC vs C++ | 备注 | -|------|------|-----------|-----------|------| -| TableChurn | n=1000 | **9.3x** | 1.24 | 内存池分配器优势 | -| StringChurn | n=1000 | 2.2x | 1.47 | 字符串分配是弱项 | -| MixedAlloc | n=1000 | 2.3x | 1.68 | | - -### 数学函数(math) - -| 场景 | 参数 | GCC vs Lua | GCC vs C++ | 备注 | -|------|------|-----------|-----------|------| -| MathTrig (sin+cos) | n=100K | **5.8x** | 0.87 | 接近 C++ 原生速度 | -| MathSqrt | n=100K | **22.3x** | 0.48 | GCC 2x 快于 C++ | -| MathExpLog | n=100K | **4.3x** | 0.87 | | -| MathMinMax | n=100K | **10.4x** | 0.90 | | - -### 核心发现 - -1. **纯数值场景全面领先 Lua 5.4 并接近手写 C++**:Fibonacci 36.6x、TailRecursion 107.5x、Recursion 42.9x、FloatPoly 34.9x、Popcount 37.3x、Sum 30.4x。数值特化让这些函数生成的 C 代码与手写版本几乎一致,剩下的交给 GCC `-O3`。 - -2. **math 库四项全部快于 Lua**(4.3x~22.3x),sin/cos/sqrt 接近 C++ 原生速度。 - -3. **string 标准库函数大多快于 Lua**:lower/upper 5-6x、format 1.8x、rep 1.1x、reverse 1.7x,均通过 CGen 内联(`FlStringLower/Upper`、`FlFormatInt`、`FlStringRep`、`FlStringReverse`)避免 `FakeluaCallByName` 的调用开销。当前仅 StringFind (plain) 0.85x 略慢于 Lua,剩余差距为单次调用派发开销。 - -4. **表操作整体有优势**:table.insert 4.0x、remove 3.2x、sort 1.2x 快于 Lua,move / concat 与 Lua 基本持平。`VarTable` 缓存连续整数键前缀长度使 `#t` 为 O(1),宿主侧与 JIT 侧共用同一套哈希/桶布局。 - -5. **arena 分配器在表频繁创建场景优势明显**:TableChurn 快于 Lua 9.3x——无 GC、批量释放。 - -6. **正则场景慢于 Lua 可接受**(Gsub 0.06x、FindPattern 0.12x、Gmatch 0.21x):使用更强的 ECMAScript Boost.Regex(非 Lua pattern),已加编译缓存;能力不同,不作为追平目标。 - -7. **剩余慢项为调用派发开销主导**:GCD(0.45x)、ToNumber(0.53x)、ToString(0.74x)、Variadic(0.91x)函数体极小(< 1 µs),JIT 的 CVar 装箱/拆箱与调用约定开销占比大,非运算本身慢。需跨函数内联或调用约定优化才能追平。 - - > 说明:ToNumber 的 bench 两侧解析同一输入字符串 `"1234567890"`。剩余差距主要来自单次 `Call()` 派发与字符串装箱常数开销,非解析本身。 - ---- - -## 完整原始输出(Lua vs INTERP,2026-09-16) - -过滤:`BM_Lua_|BM_FakeLua_.*_INTERP`。上面 INTERP vs Lua 表用的是 CPU 时间。 - -```text -Starting benchmarks... -2026-09-16T15:48:38+08:00 -Running ./bin/bench_mark -Run on (16 X 2000 MHz CPU s) -CPU Caches: - L1 Data 64 KiB (x16) - L1 Instruction 64 KiB (x16) - L2 Unified 512 KiB (x16) - L3 Unified 65536 KiB (x2) -***WARNING*** ASLR is enabled, the results may have unreproducible noise in them. ----------------------------------------------------------------------------- -Benchmark Time CPU Iterations ----------------------------------------------------------------------------- -BM_Lua_Fibonacci/20 1144569 ns 1084586 ns 637 -BM_Lua_Fibonacci/25 13283206 ns 12397758 ns 57 -BM_Lua_Fibonacci/30 150373384 ns 136381787 ns 5 -BM_Lua_Fibonacci/32 334257089 ns 334253452 ns 2 -BM_FakeLua_Fibonacci_INTERP/20 8578088 ns 8577897 ns 81 -BM_FakeLua_Fibonacci_INTERP/25 89749845 ns 89746764 ns 8 -BM_FakeLua_Fibonacci_INTERP/30 975132570 ns 975108155 ns 1 -BM_FakeLua_Fibonacci_INTERP/32 2552108645 ns 2552008862 ns 1 -BM_Lua_GCD/832040/514229 556 ns 556 ns 1290670 -BM_Lua_GCD/123456789/987654321 149 ns 149 ns 4642582 -BM_Lua_GCD/2147483647/1073741823 134 ns 134 ns 5272710 -BM_FakeLua_GCD_INTERP/832040/514229 1788 ns 1788 ns 407229 -BM_FakeLua_GCD_INTERP/123456789/987654321 518 ns 518 ns 1176854 -BM_FakeLua_GCD_INTERP/2147483647/1073741823 460 ns 460 ns 1597461 -BM_Lua_PowMod/2/1000/1000000007 699 ns 699 ns 990032 -BM_Lua_PowMod/7/1000000/1000000007 1210 ns 1210 ns 590702 -BM_Lua_PowMod/1234567/7654321/1000000007 1526 ns 1526 ns 450278 -BM_FakeLua_PowMod_INTERP/2/1000/1000000007 1394 ns 1394 ns 504574 -BM_FakeLua_PowMod_INTERP/7/1000000/1000000007 2395 ns 2395 ns 299151 -BM_FakeLua_PowMod_INTERP/1234567/7654321/1000000007 2714 ns 2713 ns 257497 -BM_Lua_Sum/10000 68382 ns 68378 ns 10435 -BM_Lua_Sum/100000 690733 ns 690725 ns 987 -BM_Lua_Sum/1000000 6726799 ns 6726654 ns 104 -BM_Lua_Sum/5000000 33570042 ns 33568361 ns 21 -BM_FakeLua_Sum_INTERP/10000 98246 ns 98242 ns 7088 -BM_FakeLua_Sum_INTERP/100000 975815 ns 975789 ns 719 -BM_FakeLua_Sum_INTERP/1000000 9770269 ns 9769755 ns 72 -BM_FakeLua_Sum_INTERP/5000000 48913989 ns 48914112 ns 14 -BM_Lua_BubbleSort/50 97623 ns 97619 ns 7161 -BM_Lua_BubbleSort/100 399558 ns 399543 ns 1844 -BM_Lua_BubbleSort/200 1529475 ns 1529435 ns 440 -BM_FakeLua_BubbleSort_INTERP/50 333897 ns 333884 ns 2106 -BM_FakeLua_BubbleSort_INTERP/100 1349297 ns 1349251 ns 532 -BM_FakeLua_BubbleSort_INTERP/200 5262565 ns 5262498 ns 132 -BM_Lua_Sieve/100 9456 ns 9456 ns 74524 -BM_Lua_Sieve/500 36011 ns 36011 ns 19372 -BM_Lua_Sieve/1000 72348 ns 72345 ns 9478 -BM_Lua_Sieve/5000 324762 ns 324745 ns 2091 -BM_FakeLua_Sieve_INTERP/100 17545 ns 17544 ns 40212 -BM_FakeLua_Sieve_INTERP/500 89880 ns 89879 ns 7841 -BM_FakeLua_Sieve_INTERP/1000 192069 ns 192069 ns 3874 -BM_FakeLua_Sieve_INTERP/5000 1070652 ns 1070611 ns 654 -BM_Lua_BinarySearch/100 41313 ns 41312 ns 17030 -BM_Lua_BinarySearch/500 282204 ns 282205 ns 2473 -BM_Lua_BinarySearch/1000 636610 ns 636602 ns 1108 -BM_FakeLua_BinarySearch_INTERP/100 124217 ns 124214 ns 5632 -BM_FakeLua_BinarySearch_INTERP/500 863565 ns 863516 ns 811 -BM_FakeLua_BinarySearch_INTERP/1000 1947828 ns 1947729 ns 361 -BM_Lua_FastPow/2/1000/1000000007 704 ns 704 ns 992443 -BM_Lua_FastPow/7/1000000/1000000007 1103 ns 1103 ns 638242 -BM_Lua_FastPow/1234567/7654321/1000000007 1462 ns 1462 ns 479726 -BM_FakeLua_FastPow_INTERP/2/1000/1000000007 1523 ns 1523 ns 462710 -BM_FakeLua_FastPow_INTERP/7/1000000/1000000007 2578 ns 2578 ns 283648 -BM_FakeLua_FastPow_INTERP/1234567/7654321/1000000007 3051 ns 3050 ns 231470 -BM_Lua_Popcount/1000 118658 ns 118656 ns 5883 -BM_Lua_Popcount/10000 1471087 ns 1471053 ns 475 -BM_Lua_Popcount/100000 17968504 ns 17967236 ns 39 -BM_FakeLua_Popcount_INTERP/1000 343823 ns 343819 ns 2028 -BM_FakeLua_Popcount_INTERP/10000 4310395 ns 4309845 ns 162 -BM_FakeLua_Popcount_INTERP/100000 53686039 ns 53683637 ns 13 -BM_Lua_InsertionSort/50 49325 ns 49323 ns 13744 -BM_Lua_InsertionSort/100 180948 ns 180941 ns 3869 -BM_Lua_InsertionSort/200 705938 ns 705929 ns 989 -BM_FakeLua_InsertionSort_INTERP/50 207825 ns 207820 ns 3366 -BM_FakeLua_InsertionSort_INTERP/100 804500 ns 804489 ns 877 -BM_FakeLua_InsertionSort_INTERP/200 3155980 ns 3155957 ns 221 -BM_Lua_MatMul 4416 ns 4416 ns 158495 -BM_FakeLua_MatMul_INTERP 8596 ns 8596 ns 81727 -BM_Lua_Vector3/10000 1709644 ns 1709608 ns 425 -BM_Lua_Vector3/100000 17277215 ns 17276556 ns 41 -BM_Lua_Vector3/1000000 167336192 ns 167336712 ns 4 -BM_FakeLua_Vector3_INTERP/10000 5870958 ns 5870569 ns 120 -BM_FakeLua_Vector3_INTERP/100000 59038871 ns 59037918 ns 12 -BM_FakeLua_Vector3_INTERP/1000000 583587155 ns 583577824 ns 1 -BM_Lua_FloatPoly/1000000 92155439 ns 92148782 ns 6 -BM_FakeLua_FloatPoly_INTERP/1000000 119274266 ns 119273134 ns 6 -BM_Lua_EmptyCall/10000 410740 ns 410735 ns 1715 -BM_Lua_EmptyCall/100000 4115022 ns 4114857 ns 163 -BM_FakeLua_EmptyCall_INTERP/10000 3225408 ns 3225336 ns 217 -BM_FakeLua_EmptyCall_INTERP/100000 32207058 ns 32207147 ns 22 -BM_Lua_Recursion/10 10530 ns 10530 ns 66500 -BM_Lua_Recursion/20 1285968 ns 1285954 ns 548 -BM_Lua_Recursion/25 14696263 ns 14696145 ns 49 -BM_FakeLua_Recursion_INTERP/10 67874 ns 67869 ns 10328 -BM_FakeLua_Recursion_INTERP/20 8065164 ns 8064800 ns 82 -BM_FakeLua_Recursion_INTERP/25 88200150 ns 88200363 ns 8 -BM_Lua_Variadic/1 548 ns 548 ns 1271307 -BM_FakeLua_Variadic_INTERP/1 2825 ns 2825 ns 250154 -BM_Lua_MultiReturn/1000 61903 ns 61903 ns 11129 -BM_Lua_MultiReturn/10000 591379 ns 591332 ns 1169 -BM_FakeLua_MultiReturn_INTERP/1000 329144 ns 329132 ns 2145 -BM_FakeLua_MultiReturn_INTERP/10000 3290976 ns 3290938 ns 213 -BM_Lua_Closure/100 24524 ns 24522 ns 28867 -BM_Lua_Closure/1000 244847 ns 244544 ns 2871 -BM_FakeLua_Closure_INTERP/100 68483 ns 68479 ns 10128 -BM_FakeLua_Closure_INTERP/1000 670507 ns 670485 ns 1049 -BM_Lua_TailRecursion/100 3762 ns 3762 ns 186916 -BM_Lua_TailRecursion/1000 36355 ns 36353 ns 19180 -BM_Lua_TailRecursion/5000 185267 ns 185121 ns 3844 -BM_FakeLua_TailRecursion_INTERP/100 40193 ns 40187 ns 17432 -BM_FakeLua_TailRecursion_INTERP/1000 503108 ns 503077 ns 1000 -BM_FakeLua_TailRecursion_INTERP/5000 6259916 ns 6259115 ns 111 -BM_Lua_TableChurn/100 59469 ns 59465 ns 11172 -BM_Lua_TableChurn/500 291246 ns 291232 ns 2342 -BM_Lua_TableChurn/1000 608475 ns 608455 ns 1156 -BM_FakeLua_TableChurn_INTERP/100 27575 ns 27573 ns 25768 -BM_FakeLua_TableChurn_INTERP/500 137383 ns 137377 ns 5133 -BM_FakeLua_TableChurn_INTERP/1000 273501 ns 273496 ns 2568 -BM_Lua_StringChurn/100 86787 ns 86784 ns 7981 -BM_Lua_StringChurn/500 560238 ns 560227 ns 1216 -BM_Lua_StringChurn/1000 1137369 ns 1137349 ns 611 -BM_FakeLua_StringChurn_INTERP/100 82985 ns 82979 ns 8970 -BM_FakeLua_StringChurn_INTERP/500 381000 ns 380990 ns 1793 -BM_FakeLua_StringChurn_INTERP/1000 812526 ns 812512 ns 882 -BM_Lua_MixedAlloc/100 108735 ns 108731 ns 6403 -BM_Lua_MixedAlloc/500 564361 ns 564349 ns 1297 -BM_Lua_MixedAlloc/1000 1095508 ns 1095467 ns 633 -BM_FakeLua_MixedAlloc_INTERP/100 61081 ns 61078 ns 11328 -BM_FakeLua_MixedAlloc_INTERP/500 317101 ns 317086 ns 2245 -BM_FakeLua_MixedAlloc_INTERP/1000 636884 ns 636886 ns 1073 -BM_Lua_MathTrig/100000 28406530 ns 28399049 ns 25 -BM_FakeLua_MathTrig_INTERP/100000 57870338 ns 57867951 ns 12 -BM_Lua_MathSqrt/100000 6499086 ns 6498149 ns 112 -BM_FakeLua_MathSqrt_INTERP/100000 19838534 ns 19837035 ns 35 -BM_Lua_MathExpLog/100000 21038376 ns 21034487 ns 34 -BM_FakeLua_MathExpLog_INTERP/100000 49770330 ns 49764563 ns 14 -BM_Lua_MathMinMax/100000 33257803 ns 33253252 ns 21 -BM_FakeLua_MathMinMax_INTERP/100000 78068647 ns 78066167 ns 9 -BM_Lua_StringLen/10 133 ns 133 ns 5255089 -BM_Lua_StringLen/100 299 ns 299 ns 2315101 -BM_Lua_StringLen/1000 598 ns 598 ns 1214737 -BM_Lua_StringLen/10000 2402 ns 2402 ns 294565 -BM_FakeLua_StringLen_INTERP/10 311 ns 311 ns 2212979 -BM_FakeLua_StringLen_INTERP/100 365 ns 365 ns 1932951 -BM_FakeLua_StringLen_INTERP/1000 565 ns 565 ns 1232995 -BM_FakeLua_StringLen_INTERP/10000 1064 ns 1064 ns 657973 -BM_Lua_StringSub/10 266 ns 266 ns 2603296 -BM_Lua_StringSub/100 500 ns 500 ns 1311387 -BM_Lua_StringSub/1000 1046 ns 1046 ns 667537 -BM_Lua_StringSub/10000 3807 ns 3806 ns 191614 -BM_FakeLua_StringSub_INTERP/10 753 ns 753 ns 926310 -BM_FakeLua_StringSub_INTERP/100 869 ns 869 ns 807951 -BM_FakeLua_StringSub_INTERP/1000 1130 ns 1130 ns 609855 -BM_FakeLua_StringSub_INTERP/10000 2046 ns 2045 ns 329919 -BM_Lua_StringRep/10 327 ns 327 ns 2154444 -BM_Lua_StringRep/100 932 ns 932 ns 727883 -BM_Lua_StringRep/1000 5267 ns 5265 ns 133440 -BM_FakeLua_StringRep_INTERP/10 726 ns 725 ns 931657 -BM_FakeLua_StringRep_INTERP/100 1117 ns 1117 ns 616015 -BM_FakeLua_StringRep_INTERP/1000 4874 ns 4874 ns 145223 -BM_Lua_StringReverse/10 258 ns 257 ns 2716507 -BM_Lua_StringReverse/100 696 ns 696 ns 970621 -BM_Lua_StringReverse/1000 1952 ns 1952 ns 361174 -BM_Lua_StringReverse/10000 12733 ns 12733 ns 54546 -BM_FakeLua_StringReverse_INTERP/10 712 ns 712 ns 979915 -BM_FakeLua_StringReverse_INTERP/100 883 ns 883 ns 793220 -BM_FakeLua_StringReverse_INTERP/1000 2191 ns 2191 ns 318712 -BM_FakeLua_StringReverse_INTERP/10000 11298 ns 11296 ns 62191 -BM_Lua_StringLower/10 252 ns 251 ns 2809706 -BM_Lua_StringLower/100 696 ns 696 ns 1013403 -BM_Lua_StringLower/1000 2313 ns 2309 ns 310662 -BM_Lua_StringLower/10000 16374 ns 16350 ns 43397 -BM_FakeLua_StringLower_INTERP/10 619 ns 619 ns 1088881 -BM_FakeLua_StringLower_INTERP/100 766 ns 766 ns 933398 -BM_FakeLua_StringLower_INTERP/1000 1179 ns 1179 ns 555653 -BM_FakeLua_StringLower_INTERP/10000 2653 ns 2653 ns 258309 -BM_Lua_StringUpper/10 250 ns 250 ns 2752023 -BM_Lua_StringUpper/100 712 ns 712 ns 992559 -BM_Lua_StringUpper/1000 2263 ns 2263 ns 299372 -BM_Lua_StringUpper/10000 15686 ns 15686 ns 44445 -BM_FakeLua_StringUpper_INTERP/10 559 ns 559 ns 1225659 -BM_FakeLua_StringUpper_INTERP/100 664 ns 664 ns 1044102 -BM_FakeLua_StringUpper_INTERP/1000 1079 ns 1079 ns 650906 -BM_FakeLua_StringUpper_INTERP/10000 2586 ns 2586 ns 271093 -BM_Lua_StringByte/10 216 ns 216 ns 3245889 -BM_Lua_StringByte/100 410 ns 410 ns 1707452 -BM_Lua_StringByte/1000 708 ns 708 ns 982592 -BM_FakeLua_StringByte_INTERP/10 665 ns 665 ns 1004509 -BM_FakeLua_StringByte_INTERP/100 737 ns 737 ns 936469 -BM_FakeLua_StringByte_INTERP/1000 918 ns 918 ns 758807 -BM_Lua_StringChar/10 3101 ns 3100 ns 227212 -BM_Lua_StringChar/100 18121 ns 18108 ns 38797 -BM_Lua_StringChar/500 80596 ns 80591 ns 8481 -BM_FakeLua_StringChar_INTERP/10 4713 ns 4713 ns 148278 -BM_FakeLua_StringChar_INTERP/100 38219 ns 38219 ns 18354 -BM_FakeLua_StringChar_INTERP/500 183011 ns 183006 ns 3867 -BM_Lua_StringFormat/10 3735 ns 3735 ns 192317 -BM_Lua_StringFormat/100 34750 ns 34749 ns 19989 -BM_Lua_StringFormat/500 186590 ns 186590 ns 3834 -BM_FakeLua_StringFormat_INTERP/10 5285 ns 5285 ns 124800 -BM_FakeLua_StringFormat_INTERP/100 47515 ns 47515 ns 14820 -BM_FakeLua_StringFormat_INTERP/500 238127 ns 238110 ns 2943 -BM_Lua_StringFind/10 371 ns 371 ns 1948124 -BM_Lua_StringFind/100 504 ns 504 ns 1312309 -BM_Lua_StringFind/1000 860 ns 860 ns 836242 -BM_Lua_StringFind/10000 3017 ns 2850 ns 246275 -BM_FakeLua_StringFind_INTERP/10 1135 ns 1082 ns 672638 -BM_FakeLua_StringFind_INTERP/100 1068 ns 1064 ns 695238 -BM_FakeLua_StringFind_INTERP/1000 1501 ns 1500 ns 526220 -BM_FakeLua_StringFind_INTERP/10000 2325 ns 2325 ns 257862 -BM_Lua_StringGsub/10 847 ns 847 ns 871151 -BM_Lua_StringGsub/100 4804 ns 4804 ns 144975 -BM_Lua_StringGsub/1000 41984 ns 41983 ns 16648 -BM_FakeLua_StringGsub_INTERP/10 2700 ns 2700 ns 257624 -BM_FakeLua_StringGsub_INTERP/100 14755 ns 14754 ns 47288 -BM_FakeLua_StringGsub_INTERP/1000 134159 ns 134159 ns 5268 -BM_Lua_ToNumber/1 210 ns 210 ns 3308067 -BM_FakeLua_ToNumber_INTERP/1 613 ns 613 ns 1157814 -BM_Lua_ToString/10 531 ns 531 ns 1317167 -BM_Lua_ToString/100 545 ns 545 ns 1287984 -BM_Lua_ToString/500 510 ns 510 ns 1268430 -BM_FakeLua_ToString_INTERP/10 687 ns 687 ns 1009822 -BM_FakeLua_ToString_INTERP/100 693 ns 693 ns 1010478 -BM_FakeLua_ToString_INTERP/500 691 ns 691 ns 1012225 -BM_Lua_StringFindPattern/1000 841315 ns 841113 ns 831 -BM_FakeLua_StringFindPattern_INTERP/1000 3395999 ns 3395933 ns 207 -BM_Lua_StringGmatch/1000 1254819 ns 1254217 ns 580 -BM_FakeLua_StringGmatch_INTERP/1000 2282902 ns 2282906 ns 309 -BM_Lua_TableInsert/100 11848 ns 11847 ns 58757 -BM_Lua_TableInsert/500 50790 ns 50783 ns 12873 -BM_Lua_TableInsert/1000 99252 ns 99248 ns 7221 -BM_Lua_TableInsert/5000 481485 ns 481467 ns 1459 -BM_FakeLua_TableInsert_INTERP/100 35333 ns 35331 ns 21032 -BM_FakeLua_TableInsert_INTERP/500 170284 ns 170282 ns 4103 -BM_FakeLua_TableInsert_INTERP/1000 338430 ns 338377 ns 2055 -BM_FakeLua_TableInsert_INTERP/5000 1675580 ns 1675498 ns 420 -BM_Lua_TableRemove/100 16745 ns 16744 ns 44233 -BM_Lua_TableRemove/500 69826 ns 69824 ns 9644 -BM_Lua_TableRemove/1000 139181 ns 139181 ns 4979 -BM_Lua_TableRemove/5000 818740 ns 818715 ns 893 -BM_FakeLua_TableRemove_INTERP/100 39744 ns 39741 ns 17697 -BM_FakeLua_TableRemove_INTERP/500 193218 ns 193204 ns 3640 -BM_FakeLua_TableRemove_INTERP/1000 417360 ns 417277 ns 1816 -BM_FakeLua_TableRemove_INTERP/5000 1980494 ns 1980486 ns 357 -BM_Lua_TableConcat/100 40270 ns 40269 ns 17312 -BM_Lua_TableConcat/500 195418 ns 195390 ns 3575 -BM_Lua_TableConcat/1000 395958 ns 395955 ns 1766 -BM_FakeLua_TableConcat_INTERP/100 38597 ns 38596 ns 17322 -BM_FakeLua_TableConcat_INTERP/500 194953 ns 194946 ns 3471 -BM_FakeLua_TableConcat_INTERP/1000 378109 ns 378105 ns 1854 -BM_Lua_TablePack/1 1391 ns 1391 ns 516095 -BM_FakeLua_TablePack_INTERP/1 1246 ns 1246 ns 583527 -BM_Lua_TableMove/100 8386 ns 8386 ns 81054 -BM_Lua_TableMove/500 29067 ns 29066 ns 27500 -BM_Lua_TableMove/1000 46120 ns 46119 ns 14068 -BM_Lua_TableMove/5000 227038 ns 227034 ns 3061 -BM_FakeLua_TableMove_INTERP/100 11804 ns 11804 ns 59495 -BM_FakeLua_TableMove_INTERP/500 52757 ns 52753 ns 13284 -BM_FakeLua_TableMove_INTERP/1000 104800 ns 104798 ns 6719 -BM_FakeLua_TableMove_INTERP/5000 589798 ns 589783 ns 1170 -BM_Lua_TableSort/100 28256 ns 28256 ns 24447 -BM_Lua_TableSort/500 167972 ns 167969 ns 4165 -BM_Lua_TableSort/1000 368101 ns 368079 ns 1912 -BM_FakeLua_TableSort_INTERP/100 18169 ns 18138 ns 38351 -BM_FakeLua_TableSort_INTERP/500 92839 ns 92823 ns 7457 -BM_FakeLua_TableSort_INTERP/1000 197154 ns 196995 ns 3544 -BM_Lua_TableCreate/1000 26310 ns 26205 ns 27662 -BM_Lua_TableCreate/3000 75548 ns 75535 ns 9256 -BM_Lua_TableCreate/5000 130772 ns 130580 ns 5470 -BM_FakeLua_TableCreate_INTERP/1000 70613 ns 70599 ns 10037 -BM_FakeLua_TableCreate_INTERP/3000 236687 ns 236599 ns 3079 -BM_FakeLua_TableCreate_INTERP/5000 432378 ns 432278 ns 1371 -BM_Lua_HashInsert/100 59467 ns 59432 ns 11755 -BM_Lua_HashInsert/500 308858 ns 308734 ns 2319 -BM_Lua_HashInsert/1000 600007 ns 599999 ns 1037 -BM_FakeLua_HashInsert_INTERP/100 39912 ns 39911 ns 17196 -BM_FakeLua_HashInsert_INTERP/500 198025 ns 198016 ns 3588 -BM_FakeLua_HashInsert_INTERP/1000 449572 ns 449553 ns 1558 -BM_Lua_HashLookup/100 48837 ns 48833 ns 15309 -BM_Lua_HashLookup/500 250008 ns 249949 ns 2682 -BM_Lua_HashLookup/1000 490965 ns 490927 ns 1423 -BM_FakeLua_HashLookup_INTERP/100 42890 ns 42887 ns 15922 -BM_FakeLua_HashLookup_INTERP/500 216841 ns 216829 ns 3157 -BM_FakeLua_HashLookup_INTERP/1000 434753 ns 434704 ns 1589 -BM_Lua_NestedTable/1000 246962 ns 246938 ns 2869 -BM_Lua_NestedTable/10000 2668007 ns 2667960 ns 274 -BM_FakeLua_NestedTable_INTERP/1000 1089201 ns 1089025 ns 650 -BM_FakeLua_NestedTable_INTERP/10000 10695225 ns 10692977 ns 67 -``` - ---- - -## 附录:GCC/TCC/C++ 原始输出(2026-08-14) - - -以下为 `benchmark_algo.cpp` / `benchmark_string.cpp` / `benchmark_table.cpp` / `benchmark_function.cpp` / `benchmark_gc.cpp` / `benchmark_math.cpp` 全部 51 个场景的完整 google benchmark 输出(含 TCC 数据): - -> ⚠️ 其中 `BM_*_StringFindPattern` / `BM_*_StringGmatch` 八行是 `[0-9]+` 修正**之前**的旧数据,两边工作量不对等,不可用于比较;以上文表格中的修正值为准。 +- 命令:`./build/bin/bench_mark --benchmark_min_time=0.2s --benchmark_repetitions=1` + +每个场景取基准注册的最大参数。`INTERP/Lua` 是解释器耗时除以 Lua 耗时,大于 1 表示解释器更慢。`GCC/Lua` 和 `TCC/Lua` 是 Lua 耗时除以对应后端,大于 1 表示 FakeLua 更快。被编译器折叠掉的 C++ 空循环不拿来和 GCC 比。 + +## 这次测量说明什么 + +1. **命名调用和闭包已经接近 Lua。** EmptyCall 的解释器耗时是 Lua 的 1.67 倍,Closure 是 1.03 倍,Recursion 是 1.96 倍。旧文档里的 6–8 倍不再成立。调用点会缓存解释器原型,热路径不再每次查哈希表;创建少量 upvalue 的闭包也不再为 upvalue 指针数组做堆分配。 +2. **精确自尾调用会复用当前帧。** 套件里的 TailRecursion 基准已经删除,所以这里没有单独一行。非尾递归仍然更贵:Fibonacci n=32 的解释器耗时是 Lua 的 3.09 倍。变参调用是 4.21 倍,因为还要装箱成 `Multi`。 +3. **模式匹配是 Lua 5.4 pattern,不是 ECMAScript。** `StringGsub` 解释器耗时是 Lua 的 1.33 倍,GCC 也没有超过 Lua(0.78 倍)。`StringFindPattern` 仍慢,解释器 3.29 倍、GCC 只有 Lua 的 0.35 倍速度。下一步字符串热点应放在 pattern 引擎,而不是再挤 GCC 的 Fibonacci。 +4. **GCC 数值热点仍然领先,但倍数以这次为准。** Fibonacci 53.48 倍,FloatPoly 24.40 倍且与手写 C++ 持平。Sum 是 16.73 倍,不再沿用旧文档的 30 倍。 +5. **没有 GC 的分配场景解释器可以快过 Lua。** TableChurn、StringChurn、MixedAlloc 的解释器耗时都低于 Lua。 + +### 算法 + +| 场景 | 参数 | Lua 5.4 | INTERP | INTERP/Lua | GCC | GCC/Lua | TCC | TCC/Lua | +|---|---|---:|---:|---:|---:|---:|---:|---:| +| Fibonacci | 32 | 290.1 ms | 896.3 ms | 3.09x | 5.4 ms | 53.48x | 222.4 ms | 1.30x | +| Sum | 5000000 | 33.8 ms | 76.6 ms | 2.27x | 2.0 ms | 16.73x | 16.2 ms | 2.08x | +| Popcount | 100000 | 17.5 ms | 58.6 ms | 3.35x | 1.0 ms | 16.91x | 3.2 ms | 5.55x | +| FloatPoly | 1000000 | 91.2 ms | 131.1 ms | 1.44x | 3.7 ms | 24.40x | 13.8 ms | 6.59x | +| BubbleSort | 200 | 1.5 ms | 4.2 ms | 2.73x | 159.7 μs | 9.60x | 1.5 ms | 1.04x | +| Sieve | 5000 | 334.1 μs | 828.2 μs | 2.48x | 62.9 μs | 5.31x | 467.2 μs | 0.72x | +| InsertionSort | 200 | 712.5 μs | 2.9 ms | 4.12x | 91.5 μs | 7.79x | 1.1 ms | 0.66x | +| BinarySearch | 1000 | 621.6 μs | 1.8 ms | 2.96x | 273.5 μs | 2.27x | 874.3 μs | 0.71x | +| Vector3 | 1000000 | 213.3 ms | 603.5 ms | 2.83x | 12.1 ms | 17.66x | 147.1 ms | 1.45x | +| MatMul | — | 4.4 μs | 7.5 μs | 1.71x | 1.5 μs | 2.92x | 4.0 μs | 1.10x | +| GCD | 832040/514229 | 551 ns | 1.8 μs | 3.23x | 354 ns | 1.56x | 806 ns | 0.68x | +| PowMod | 2/1000/1000000007 | 681 ns | 1.5 μs | 2.23x | 312 ns | 2.18x | 824 ns | 0.83x | +| FastPow | 2/1000/1000000007 | 753 ns | 1.5 μs | 2.05x | 322 ns | 2.34x | 541 ns | 1.39x | + +### 函数调用 + +| 场景 | 参数 | Lua 5.4 | INTERP | INTERP/Lua | GCC | GCC/Lua | TCC | TCC/Lua | +|---|---|---:|---:|---:|---:|---:|---:|---:| +| EmptyCall | 100000 | 4.7 ms | 7.8 ms | 1.67x | 384.8 μs | 12.20x | 6.4 ms | 0.74x | +| Recursion | 25 | 15.7 ms | 30.9 ms | 1.96x | 176.4 μs | 89.22x | 8.4 ms | 1.88x | +| Closure | 1000 | 250.5 μs | 257.2 μs | 1.03x | 12.7 μs | 19.76x | 215.0 μs | 1.16x | +| MultiReturn | 10000 | 721.8 μs | 1.1 ms | 1.51x | 31.4 μs | 23.01x | 388.1 μs | 1.86x | +| Variadic | 1 | 661 ns | 2.8 μs | 4.21x | 267 ns | 2.48x | 771 ns | 0.86x | + +### 字符串 + +| 场景 | 参数 | Lua 5.4 | INTERP | INTERP/Lua | GCC | GCC/Lua | TCC | TCC/Lua | +|---|---|---:|---:|---:|---:|---:|---:|---:| +| StringLen | 10000 | 2.4 μs | 1.0 μs | 0.44x | 964 ns | 2.45x | 1.0 μs | 2.31x | +| StringSub | 10000 | 3.7 μs | 2.0 μs | 0.53x | 1.9 μs | 1.95x | 1.9 μs | 1.96x | +| StringLower | 10000 | 15.9 μs | 2.9 μs | 0.18x | 2.2 μs | 7.08x | 50.5 μs | 0.31x | +| StringUpper | 10000 | 15.8 μs | 2.7 μs | 0.17x | 2.2 μs | 7.20x | 50.9 μs | 0.31x | +| StringFind | 10000 | 2.6 μs | 2.3 μs | 0.90x | 3.2 μs | 0.81x | 19.7 μs | 0.13x | +| StringGsub | 1000 | 43.6 μs | 57.8 μs | 1.33x | 55.7 μs | 0.78x | 55.8 μs | 0.78x | +| StringFindPattern | 1000 | 872.6 μs | 2.9 ms | 3.29x | 2.5 ms | 0.35x | 2.4 ms | 0.36x | +| StringGmatch | 1000 | 1.2 ms | 2.4 ms | 1.96x | 1.6 ms | 0.77x | 1.8 ms | 0.67x | +| StringFormat | 500 | 186.0 μs | 259.3 μs | 1.39x | 6.6 μs | 27.98x | 44.0 μs | 4.23x | +| StringRep | 1000 | 5.3 μs | 4.7 μs | 0.88x | 4.5 μs | 1.18x | 5.4 μs | 0.98x | +| StringReverse | 10000 | 12.8 μs | 11.2 μs | 0.87x | 8.2 μs | 1.56x | 44.2 μs | 0.29x | +| StringByte | 1000 | 751 ns | 1.0 μs | 1.36x | 455 ns | 1.65x | 540 ns | 1.39x | +| StringChar | 500 | 81.6 μs | 157.7 μs | 1.93x | 12.0 μs | 6.79x | 80.4 μs | 1.02x | +| ToNumber | 1 | 213 ns | 570 ns | 2.68x | 234 ns | 0.91x | 537 ns | 0.40x | +| ToString | 500 | 515 ns | 577 ns | 1.12x | 207 ns | 2.48x | 311 ns | 1.66x | + +### 表 + +| 场景 | 参数 | Lua 5.4 | INTERP | INTERP/Lua | GCC | GCC/Lua | TCC | TCC/Lua | +|---|---|---:|---:|---:|---:|---:|---:|---:| +| TableCreate | 5000 | 132.2 μs | 263.1 μs | 1.99x | 51.7 μs | 2.56x | 266.1 μs | 0.50x | +| TableInsert | 5000 | 540.7 μs | 1.7 ms | 3.20x | 64.6 μs | 8.37x | 382.7 μs | 1.41x | +| TableRemove | 5000 | 902.4 μs | 2.0 ms | 2.27x | 62.1 μs | 14.53x | 628.7 μs | 1.44x | +| TableConcat | 1000 | 393.5 μs | 324.5 μs | 0.82x | 29.6 μs | 13.28x | 135.2 μs | 2.91x | +| TableSort | 1000 | 368.5 μs | 152.7 μs | 0.41x | 100.2 μs | 3.68x | 158.2 μs | 2.33x | +| TableMove | 5000 | 231.5 μs | 336.7 μs | 1.45x | 98.5 μs | 2.35x | 533.8 μs | 0.43x | +| TablePack | 1 | 1.4 μs | 952 ns | 0.67x | 420 ns | 3.37x | 1.2 μs | 1.21x | +| HashInsert | 1000 | 570.9 μs | 402.9 μs | 0.71x | 82.5 μs | 6.92x | 316.6 μs | 1.80x | +| HashLookup | 1000 | 442.5 μs | 419.3 μs | 0.95x | 38.5 μs | 11.50x | 183.9 μs | 2.41x | +| NestedTable | 10000 | 2.5 ms | 10.0 ms | 3.95x | 798.1 μs | 3.17x | 5.6 ms | 0.45x | + +### 数学 + +| 场景 | 参数 | Lua 5.4 | INTERP | INTERP/Lua | GCC | GCC/Lua | TCC | TCC/Lua | +|---|---|---:|---:|---:|---:|---:|---:|---:| +| MathTrig | 100000 | 28.8 ms | 65.7 ms | 2.28x | 9.1 ms | 3.15x | 18.6 ms | 1.55x | +| MathSqrt | 100000 | 7.5 ms | 20.7 ms | 2.78x | 322.6 μs | 23.13x | 4.8 ms | 1.54x | +| MathExpLog | 100000 | 25.7 ms | 69.6 ms | 2.71x | 5.4 ms | 4.75x | 12.9 ms | 2.00x | +| MathMinMax | 100000 | 34.6 ms | 103.4 ms | 2.99x | 4.6 ms | 7.50x | 17.5 ms | 1.97x | + +### 分配 + +| 场景 | 参数 | Lua 5.4 | INTERP | INTERP/Lua | GCC | GCC/Lua | TCC | TCC/Lua | +|---|---|---:|---:|---:|---:|---:|---:|---:| +| TableChurn | 1000 | 587.6 μs | 324.2 μs | 0.55x | 39.5 μs | 14.88x | 314.6 μs | 1.87x | +| StringChurn | 1000 | 1.1 ms | 775.7 μs | 0.69x | 52.9 μs | 21.28x | 319.3 μs | 3.53x | +| MixedAlloc | 1000 | 1.1 ms | 766.8 μs | 0.68x | 68.6 μs | 16.33x | 398.8 μs | 2.81x | -```text -:1561: warning: assignment of read-only location -Starting benchmarks... -2026-08-14T12:05:23+08:00 -Running ./bin/bench_mark -Run on (2 X 2595.12 MHz CPU s) -CPU Caches: - L1 Data 32 KiB (x2) - L1 Instruction 32 KiB (x2) - L2 Unified 4096 KiB (x2) - L3 Unified 16384 KiB (x1) -Load Average: 5.87, 5.69, 4.54 -***WARNING*** ASLR is enabled, the results may have unreproducible noise in them. ----------------------------------------------------------------------------- -Benchmark Time CPU Iterations ----------------------------------------------------------------------------- -BM_CPP_BinarySearch/100 7,735 ns 4,893 ns 143,193 -BM_CPP_BinarySearch/1000 94,468 ns 65,183 ns 10,769 -BM_CPP_BinarySearch/500 53,068 ns 29,810 ns 23,430 -BM_CPP_BubbleSort/100 98,467 ns 62,909 ns 11,127 -BM_CPP_BubbleSort/200 388,207 ns 249,256 ns 2,800 -BM_CPP_BubbleSort/50 26,237 ns 15,964 ns 43,992 -BM_CPP_Closure/100 792 ns 551 ns 1,272,292 -BM_CPP_Closure/1000 8,993 ns 5,466 ns 128,618 -BM_CPP_EmptyCall/10000 9 ns 5 ns 127,122,452 -BM_CPP_EmptyCall/100000 8 ns 5 ns 127,515,986 -BM_CPP_FastPow/1234567/7654321/1000000007 514 ns 297 ns 2,357,197 -BM_CPP_FastPow/2/1000/1000000007 261 ns 121 ns 5,799,249 -BM_CPP_FastPow/7/1000000/1000000007 373 ns 205 ns 3,429,130 -BM_CPP_Fibonacci/20 342,994 ns 188,804 ns 3,720 -BM_CPP_Fibonacci/25 3,686,904 ns 2,091,372 ns 333 -BM_CPP_Fibonacci/30 37,599,358 ns 23,298,280 ns 30 -BM_CPP_Fibonacci/32 99,812,719 ns 60,640,845 ns 12 -BM_CPP_FloatPoly/1000000 7,930,843 ns 5,847,105 ns 120 -BM_CPP_GCD/123456789/987654321 82 ns 44 ns 15,634,680 -BM_CPP_GCD/2147483647/1073741823 65 ns 39 ns 17,808,461 -BM_CPP_GCD/832040/514229 270 ns 181 ns 3,855,731 -BM_CPP_HashInsert/100 42,343 ns 25,099 ns 27,962 -BM_CPP_HashInsert/1000 404,595 ns 288,678 ns 2,410 -BM_CPP_HashInsert/500 224,686 ns 141,863 ns 4,922 -BM_CPP_HashLookup/100 540,670 ns 308,545 ns 2,270 -BM_CPP_HashLookup/1000 763,223 ns 492,689 ns 1,426 -BM_CPP_HashLookup/500 609,514 ns 389,886 ns 1,793 -BM_CPP_InsertionSort/100 51,359 ns 30,374 ns 23,049 -BM_CPP_InsertionSort/200 204,452 ns 118,372 ns 5,882 -BM_CPP_InsertionSort/50 13,938 ns 8,006 ns 88,502 -BM_CPP_MatMul 485 ns 247 ns 2,846,106 -BM_CPP_MathExpLog/100000 7,276,256 ns 4,629,740 ns 151 -BM_CPP_MathMinMax/100000 3,610,545 ns 2,396,188 ns 291 -BM_CPP_MathSqrt/100000 906,821 ns 571,279 ns 1,234 -BM_CPP_MathTrig/100000 5,398,646 ns 3,531,282 ns 199 -BM_CPP_MixedAlloc/100 32,146 ns 23,078 ns 30,205 -BM_CPP_MixedAlloc/1000 433,047 ns 234,244 ns 2,990 -BM_CPP_MixedAlloc/500 200,939 ns 116,896 ns 5,985 -BM_CPP_MultiReturn/1000 10,052 ns 5,480 ns 126,527 -BM_CPP_MultiReturn/10000 91,075 ns 54,916 ns 12,734 -BM_CPP_NestedTable/1000 91,243 ns 60,937 ns 11,457 -BM_CPP_NestedTable/10000 1,132,805 ns 607,423 ns 1,155 -BM_CPP_Popcount/1000 48,426 ns 32,558 ns 21,481 -BM_CPP_Popcount/10000 649,786 ns 408,784 ns 1,712 -BM_CPP_Popcount/100000 7,400,519 ns 5,004,960 ns 140 -BM_CPP_PowMod/1234567/7654321/1000000007 489 ns 297 ns 2,355,934 -BM_CPP_PowMod/2/1000/1000000007 192 ns 121 ns 5,817,991 -BM_CPP_PowMod/7/1000000/1000000007 329 ns 205 ns 3,395,045 -BM_CPP_Recursion/10 2,623 ns 1,517 ns 461,648 -BM_CPP_Recursion/20 306,905 ns 188,664 ns 3,708 -BM_CPP_Recursion/25 3,584,836 ns 2,076,389 ns 337 -BM_CPP_Sieve/100 2,194 ns 1,462 ns 476,278 -BM_CPP_Sieve/1000 27,651 ns 15,057 ns 46,837 -BM_CPP_Sieve/500 14,586 ns 7,367 ns 94,648 -BM_CPP_Sieve/5000 125,191 ns 79,410 ns 8,797 -BM_CPP_StringByte/10 30 ns 16 ns 41,418,033 -BM_CPP_StringByte/100 27 ns 16 ns 41,401,382 -BM_CPP_StringByte/1000 27 ns 16 ns 41,359,841 -BM_CPP_StringChar/10 348 ns 208 ns 3,374,227 -BM_CPP_StringChar/100 2,491 ns 1,459 ns 479,078 -BM_CPP_StringChar/500 11,997 ns 6,820 ns 102,786 -BM_CPP_StringChurn/100 52,805 ns 29,398 ns 23,705 -BM_CPP_StringChurn/1000 572,837 ns 302,474 ns 2,282 -BM_CPP_StringChurn/500 231,629 ns 152,176 ns 4,647 -BM_CPP_StringFind/10 105 ns 66 ns 10,555,668 -BM_CPP_StringFind/100 132 ns 66 ns 10,629,430 -BM_CPP_StringFind/1000 123 ns 70 ns 9,901,078 -BM_CPP_StringFind/10000 209 ns 130 ns 5,445,641 -BM_CPP_StringFindPattern/1000 359,361 ns 193,176 ns 3,608 -BM_CPP_StringFormat/10 845 ns 522 ns 1,337,811 -BM_CPP_StringFormat/100 7,618 ns 5,158 ns 132,433 -BM_CPP_StringFormat/500 43,386 ns 28,435 ns 24,749 -BM_CPP_StringGmatch/1000 437,710 ns 226,440 ns 3,092 -BM_CPP_StringGsub/10 277 ns 184 ns 3,792,558 -BM_CPP_StringGsub/100 2,760 ns 1,736 ns 407,043 -BM_CPP_StringGsub/1000 27,888 ns 17,248 ns 40,720 -BM_CPP_StringLen/10 27 ns 16 ns 41,401,789 -BM_CPP_StringLen/100 26 ns 16 ns 41,582,886 -BM_CPP_StringLen/1000 26 ns 16 ns 41,425,791 -BM_CPP_StringLen/10000 23 ns 16 ns 41,620,113 -BM_CPP_StringLower/10 306 ns 195 ns 3,601,028 -BM_CPP_StringLower/100 2,325 ns 1,348 ns 521,413 -BM_CPP_StringLower/1000 22,828 ns 12,466 ns 56,209 -BM_CPP_StringLower/10000 195,537 ns 123,952 ns 5,665 -BM_CPP_StringRep/10 739 ns 420 ns 1,655,571 -BM_CPP_StringRep/100 5,540 ns 3,120 ns 225,710 -BM_CPP_StringRep/1000 49,757 ns 30,123 ns 23,272 -BM_CPP_StringReverse/10 175 ns 103 ns 6,811,517 -BM_CPP_StringReverse/100 692 ns 450 ns 1,556,938 -BM_CPP_StringReverse/1000 5,818 ns 3,468 ns 200,200 -BM_CPP_StringReverse/10000 53,508 ns 34,013 ns 20,679 -BM_CPP_StringSub/10 151 ns 91 ns 7,642,290 -BM_CPP_StringSub/100 212 ns 134 ns 5,214,628 -BM_CPP_StringSub/1000 211 ns 143 ns 4,924,983 -BM_CPP_StringSub/10000 566 ns 369 ns 1,910,754 -BM_CPP_StringUpper/10 331 ns 194 ns 3,595,139 -BM_CPP_StringUpper/100 2,405 ns 1,347 ns 521,995 -BM_CPP_StringUpper/1000 21,399 ns 12,455 ns 55,977 -BM_CPP_StringUpper/10000 214,600 ns 123,125 ns 5,676 -BM_CPP_Sum/10000 87,621 ns 54,921 ns 12,724 -BM_CPP_Sum/100000 855,607 ns 548,039 ns 1,282 -BM_CPP_Sum/1000000 8,922,555 ns 5,483,076 ns 128 -BM_CPP_Sum/5000000 40,471,358 ns 27,449,499 ns 26 -BM_CPP_TableChurn/100 8,172 ns 5,265 ns 131,011 -BM_CPP_TableChurn/1000 76,141 ns 52,421 ns 13,634 -BM_CPP_TableChurn/500 50,410 ns 26,018 ns 26,612 -BM_CPP_TableConcat/100 12,265 ns 8,491 ns 82,644 -BM_CPP_TableConcat/1000 124,859 ns 87,820 ns 7,873 -BM_CPP_TableConcat/500 83,736 ns 43,958 ns 15,941 -BM_CPP_TableCreate/1000 19,036 ns 11,735 ns 59,861 -BM_CPP_TableCreate/3000 53,314 ns 34,985 ns 20,129 -BM_CPP_TableCreate/5000 89,021 ns 58,477 ns 12,039 -BM_CPP_TableInsert/100 1,047 ns 659 ns 1,075,111 -BM_CPP_TableInsert/1000 11,124 ns 5,875 ns 120,021 -BM_CPP_TableInsert/500 5,603 ns 2,984 ns 234,160 -BM_CPP_TableInsert/5000 44,146 ns 28,972 ns 24,187 -BM_CPP_TableMove/100 3,080 ns 1,905 ns 366,242 -BM_CPP_TableMove/1000 25,244 ns 17,827 ns 39,325 -BM_CPP_TableMove/500 15,080 ns 9,002 ns 77,536 -BM_CPP_TableMove/5000 144,251 ns 88,188 ns 7,924 -BM_CPP_TablePack/1 10 ns 5 ns 127,192,129 -BM_CPP_TableRemove/100 3,206 ns 1,792 ns 389,950 -BM_CPP_TableRemove/1000 32,342 ns 17,231 ns 40,355 -BM_CPP_TableRemove/500 14,258 ns 8,737 ns 81,578 -BM_CPP_TableRemove/5000 167,448 ns 85,880 ns 8,243 -BM_CPP_TableSort/100 8,862 ns 5,155 ns 137,027 -BM_CPP_TableSort/1000 87,764 ns 69,572 ns 10,079 -BM_CPP_TableSort/500 53,222 ns 33,132 ns 21,144 -BM_CPP_TailRecursion/100 1,049 ns 553 ns 1,267,372 -BM_CPP_TailRecursion/1000 9,701 ns 5,491 ns 127,230 -BM_CPP_TailRecursion/5000 45,927 ns 27,491 ns 25,469 -BM_CPP_ToNumber/1 167 ns 96 ns 7,262,672 -BM_CPP_ToString/10 1,205 ns 872 ns 802,717 -BM_CPP_ToString/100 13,072 ns 8,462 ns 84,581 -BM_CPP_ToString/500 70,978 ns 44,001 ns 16,041 -BM_CPP_Variadic/1 18 ns 10 ns 64,199,090 -BM_CPP_Vector3/10000 85,966 ns 52,871 ns 13,291 -BM_CPP_Vector3/100000 984,296 ns 527,532 ns 1,322 -BM_CPP_Vector3/1000000 10,423,974 ns 5,285,754 ns 133 -BM_FakeLua_BinarySearch_GCC/100 13,922 ns 8,156 ns 85,400 -BM_FakeLua_BinarySearch_GCC/1000 271,491 ns 156,553 ns 4,320 -BM_FakeLua_BinarySearch_TCC/100 81,768 ns 52,935 ns 13,579 -BM_FakeLua_BinarySearch_TCC/1000 1,422,010 ns 874,824 ns 774 -BM_FakeLua_BinarySearch_TCC/500 660,936 ns 373,850 ns 1,911 -BM_FakeLua_BubbleSort_GCC/100 230,707 ns 175,479 ns 3,985 -BM_FakeLua_BubbleSort_GCC/200 1,244,348 ns 698,588 ns 1,007 -BM_FakeLua_BubbleSort_GCC/50 83,555 ns 44,675 ns 15,516 -BM_FakeLua_BubbleSort_TCC/100 1,454,647 ns 836,477 ns 816 -BM_FakeLua_BubbleSort_TCC/200 5,488,115 ns 3,256,166 ns 213 -BM_FakeLua_BubbleSort_TCC/50 293,114 ns 211,591 ns 3,390 -BM_FakeLua_Closure_GCC/100 19,181 ns 11,046 ns 63,564 -BM_FakeLua_Closure_GCC/1000 211,068 ns 106,248 ns 6,492 -BM_FakeLua_Closure_TCC/100 36,088 ns 21,576 ns 32,483 -BM_FakeLua_Closure_TCC/1000 354,499 ns 211,904 ns 3,347 -BM_FakeLua_EmptyCall_GCC/10000 56,352 ns 30,976 ns 23,093 -BM_FakeLua_EmptyCall_GCC/100000 557,461 ns 307,805 ns 2,283 -BM_FakeLua_EmptyCall_TCC/10000 367,650 ns 256,049 ns 2,730 -BM_FakeLua_EmptyCall_TCC/100000 5,327,379 ns 2,558,148 ns 272 -BM_FakeLua_FastPow_GCC/1234567/7654321/1000000007 1,095 ns 729 ns 957,306 -BM_FakeLua_FastPow_GCC/2/1000/1000000007 919 ns 531 ns 1,320,878 -BM_FakeLua_FastPow_GCC/7/1000000/1000000007 1,008 ns 632 ns 1,110,756 -BM_FakeLua_FastPow_TCC/1234567/7654321/1000000007 1,148 ns 818 ns 863,171 -BM_FakeLua_FastPow_TCC/2/1000/1000000007 864 ns 630 ns 1,111,709 -BM_FakeLua_FastPow_TCC/7/1000000/1000000007 1,241 ns 734 ns 956,152 -BM_FakeLua_Fibonacci_GCC/20 45,729 ns 29,702 ns 24,017 -BM_FakeLua_Fibonacci_GCC/25 385,589 ns 271,393 ns 2,570 -BM_FakeLua_Fibonacci_GCC/30 5,624,615 ns 2,967,464 ns 241 -BM_FakeLua_Fibonacci_GCC/32 12,786,224 ns 7,590,283 ns 93 -BM_FakeLua_Fibonacci_TCC/20 123,444 ns 78,774 ns 9,099 -BM_FakeLua_Fibonacci_TCC/25 1,350,023 ns 855,121 ns 805 -BM_FakeLua_Fibonacci_TCC/30 12,850,953 ns 9,452,837 ns 74 -BM_FakeLua_Fibonacci_TCC/32 38,140,319 ns 24,861,804 ns 28 -BM_FakeLua_FloatPoly_GCC/1000000 4,232,156 ns 2,865,423 ns 245 -BM_FakeLua_FloatPoly_TCC/1000000 22,886,829 ns 13,789,552 ns 50 -BM_FakeLua_GCD_GCC/123456789/987654321 654 ns 423 ns 1,654,415 -BM_FakeLua_GCD_GCC/2147483647/1073741823 620 ns 421 ns 1,665,114 -BM_FakeLua_GCD_GCC/832040/514229 824 ns 520 ns 1,353,393 -BM_FakeLua_GCD_TCC/123456789/987654321 784 ns 490 ns 1,442,922 -BM_FakeLua_GCD_TCC/2147483647/1073741823 817 ns 478 ns 1,444,175 -BM_FakeLua_GCD_TCC/832040/514229 994 ns 615 ns 1,145,840 -BM_FakeLua_HashInsert_GCC/100 39,491 ns 24,481 ns 28,788 -BM_FakeLua_HashInsert_GCC/1000 394,557 ns 253,863 ns 2,777 -BM_FakeLua_HashInsert_GCC/500 209,448 ns 119,708 ns 5,816 -BM_FakeLua_HashInsert_TCC/100 62,986 ns 39,308 ns 17,965 -BM_FakeLua_HashInsert_TCC/1000 709,844 ns 432,322 ns 1,607 -BM_FakeLua_HashInsert_TCC/500 288,573 ns 189,151 ns 3,702 -BM_FakeLua_HashLookup_GCC/100 32,466 ns 22,404 ns 31,590 -BM_FakeLua_HashLookup_GCC/1000 363,678 ns 222,897 ns 3,149 -BM_FakeLua_HashLookup_GCC/500 161,127 ns 111,924 ns 6,177 -BM_FakeLua_HashLookup_TCC/100 50,594 ns 30,941 ns 22,728 -BM_FakeLua_HashLookup_TCC/1000 541,329 ns 316,970 ns 2,207 -BM_FakeLua_HashLookup_TCC/500 271,060 ns 156,821 ns 4,417 -BM_FakeLua_InsertionSort_GCC/100 154,078 ns 89,101 ns 7,854 -BM_FakeLua_InsertionSort_GCC/200 584,821 ns 345,628 ns 2,021 -BM_FakeLua_InsertionSort_GCC/50 44,420 ns 23,795 ns 29,465 -BM_FakeLua_InsertionSort_TCC/100 902,132 ns 532,541 ns 1,398 -BM_FakeLua_InsertionSort_TCC/200 3,914,237 ns 2,087,508 ns 346 -BM_FakeLua_InsertionSort_TCC/50 233,170 ns 139,290 ns 4,924 -BM_FakeLua_MatMul_GCC 2,165 ns 1,260 ns 545,399 -BM_FakeLua_MatMul_TCC 6,551 ns 4,132 ns 166,327 -BM_FakeLua_MathExpLog_GCC/100000 5,721,714 ns 4,046,541 ns 174 -BM_FakeLua_MathExpLog_TCC/100000 17,934,875 ns 11,444,546 ns 61 -BM_FakeLua_MathMinMax_GCC/100000 3,704,800 ns 2,159,112 ns 323 -BM_FakeLua_MathMinMax_TCC/100000 19,676,004 ns 11,465,920 ns 62 -BM_FakeLua_MathSqrt_GCC/100000 464,885 ns 275,825 ns 2,537 -BM_FakeLua_MathSqrt_TCC/100000 6,879,179 ns 4,166,763 ns 168 -BM_FakeLua_MathTrig_GCC/100000 5,560,194 ns 3,058,615 ns 229 -BM_FakeLua_MathTrig_TCC/100000 21,645,316 ns 11,484,684 ns 61 -BM_FakeLua_MixedAlloc_GCC/100 62,577 ns 40,070 ns 17,411 -BM_FakeLua_MixedAlloc_GCC/1000 536,218 ns 393,414 ns 1,779 -BM_FakeLua_MixedAlloc_GCC/500 310,651 ns 197,231 ns 3,526 -BM_FakeLua_MixedAlloc_TCC/100 102,126 ns 62,232 ns 11,196 -BM_FakeLua_MixedAlloc_TCC/1000 973,380 ns 628,448 ns 1,111 -BM_FakeLua_MixedAlloc_TCC/500 521,024 ns 314,661 ns 2,229 -BM_FakeLua_MultiReturn_GCC/1000 4,766 ns 2,915 ns 240,764 -BM_FakeLua_MultiReturn_GCC/10000 44,533 ns 25,062 ns 27,754 -BM_FakeLua_MultiReturn_TCC/1000 9,596 ns 5,775 ns 124,053 -BM_FakeLua_MultiReturn_TCC/10000 84,003 ns 52,909 ns 13,405 -BM_FakeLua_NestedTable_GCC/1000 138,830 ns 85,672 ns 8,199 -BM_FakeLua_NestedTable_GCC/10000 1,448,717 ns 845,455 ns 813 -BM_FakeLua_NestedTable_TCC/1000 769,763 ns 444,952 ns 1,569 -BM_FakeLua_NestedTable_TCC/10000 7,183,922 ns 4,455,904 ns 158 -BM_FakeLua_Popcount_GCC/1000 6,777 ns 4,367 ns 160,915 -BM_FakeLua_Popcount_GCC/10000 89,012 ns 48,663 ns 14,499 -BM_FakeLua_Popcount_GCC/100000 1,016,120 ns 592,552 ns 1,160 -BM_FakeLua_Popcount_TCC/1000 21,598 ns 13,338 ns 52,591 -BM_FakeLua_Popcount_TCC/10000 268,037 ns 153,982 ns 4,337 -BM_FakeLua_Popcount_TCC/100000 2,767,183 ns 1,913,541 ns 375 -BM_FakeLua_PowMod_GCC/1234567/7654321/1000000007 1,209 ns 759 ns 928,849 -BM_FakeLua_PowMod_GCC/2/1000/1000000007 830 ns 559 ns 1,262,622 -BM_FakeLua_PowMod_GCC/7/1000000/1000000007 957 ns 666 ns 1,059,056 -BM_FakeLua_PowMod_TCC/1234567/7654321/1000000007 1,821 ns 1,084 ns 645,501 -BM_FakeLua_PowMod_TCC/2/1000/1000000007 1,222 ns 720 ns 977,040 -BM_FakeLua_PowMod_TCC/7/1000000/1000000007 1,492 ns 947 ns 733,551 -BM_FakeLua_Recursion_GCC/10 928 ns 578 ns 1,197,261 -BM_FakeLua_Recursion_GCC/20 48,722 ns 30,172 ns 23,410 -BM_FakeLua_Recursion_GCC/25 386,863 ns 269,108 ns 2,586 -BM_FakeLua_Recursion_TCC/10 1,702 ns 1,089 ns 639,131 -BM_FakeLua_Recursion_TCC/20 138,307 ns 79,106 ns 8,834 -BM_FakeLua_Recursion_TCC/25 1,453,790 ns 857,782 ns 801 -BM_FakeLua_Sieve_GCC/100 6,701 ns 3,979 ns 175,646 -BM_FakeLua_Sieve_GCC/1000 63,689 ns 35,024 ns 19,847 -BM_FakeLua_Sieve_GCC/500 29,989 ns 17,088 ns 40,695 -BM_FakeLua_Sieve_GCC/5000 344,444 ns 213,124 ns 3,359 -BM_FakeLua_Sieve_TCC/100 29,560 ns 17,487 ns 39,646 -BM_FakeLua_Sieve_TCC/1000 263,604 ns 172,424 ns 4,041 -BM_FakeLua_Sieve_TCC/500 143,477 ns 85,340 ns 8,216 -BM_FakeLua_Sieve_TCC/5000 1,780,537 ns 1,041,591 ns 679 -BM_FakeLua_StringByte_GCC/10 939 ns 531 ns 1,304,736 -BM_FakeLua_StringByte_GCC/100 951 ns 576 ns 1,226,213 -BM_FakeLua_StringByte_GCC/1000 1,003 ns 603 ns 1,162,886 -BM_FakeLua_StringByte_TCC/10 983 ns 593 ns 1,206,309 -BM_FakeLua_StringByte_TCC/100 1,000 ns 638 ns 1,076,212 -BM_FakeLua_StringByte_TCC/1000 1,140 ns 666 ns 1,037,743 -BM_FakeLua_StringChar_GCC/10 4,717 ns 2,785 ns 252,981 -BM_FakeLua_StringChar_GCC/100 26,150 ns 16,053 ns 43,522 -BM_FakeLua_StringChar_GCC/500 124,214 ns 72,588 ns 9,375 -BM_FakeLua_StringChar_TCC/10 7,145 ns 4,461 ns 157,332 -BM_FakeLua_StringChar_TCC/100 56,569 ns 31,568 ns 22,304 -BM_FakeLua_StringChar_TCC/500 214,876 ns 144,492 ns 4,830 -BM_FakeLua_StringChurn_GCC/100 65,339 ns 44,656 ns 15,766 -BM_FakeLua_StringChurn_GCC/1000 826,591 ns 443,901 ns 1,576 -BM_FakeLua_StringChurn_GCC/500 365,738 ns 223,272 ns 3,136 -BM_FakeLua_StringChurn_TCC/100 90,760 ns 57,091 ns 12,523 -BM_FakeLua_StringChurn_TCC/1000 1,114,236 ns 571,149 ns 1,237 -BM_FakeLua_StringChurn_TCC/500 513,027 ns 283,598 ns 2,459 -BM_FakeLua_StringFindPattern_GCC/1000 11,580,621 ns 7,654,594 ns 91 -BM_FakeLua_StringFindPattern_TCC/1000 13,050,656 ns 7,914,296 ns 88 -BM_FakeLua_StringFind_GCC/10 2,628 ns 1,647 ns 424,630 -BM_FakeLua_StringFind_GCC/100 2,637 ns 1,654 ns 421,490 -BM_FakeLua_StringFind_GCC/1000 2,950 ns 1,723 ns 410,605 -BM_FakeLua_StringFind_GCC/10000 3,955 ns 2,502 ns 276,271 -BM_FakeLua_StringFind_TCC/10 2,650 ns 1,713 ns 409,465 -BM_FakeLua_StringFind_TCC/100 2,782 ns 1,721 ns 404,834 -BM_FakeLua_StringFind_TCC/1000 2,985 ns 1,771 ns 394,892 -BM_FakeLua_StringFind_TCC/10000 3,996 ns 2,588 ns 272,588 -BM_FakeLua_StringFormat_GCC/10 3,354 ns 1,954 ns 356,680 -BM_FakeLua_StringFormat_GCC/100 26,095 ns 15,567 ns 44,584 -BM_FakeLua_StringFormat_GCC/500 119,442 ns 77,076 ns 8,998 -BM_FakeLua_StringFormat_TCC/10 3,333 ns 2,324 ns 299,598 -BM_FakeLua_StringFormat_TCC/100 29,993 ns 18,834 ns 36,855 -BM_FakeLua_StringFormat_TCC/500 160,453 ns 93,895 ns 7,480 -BM_FakeLua_StringGmatch_GCC/1000 8,819,264 ns 5,272,718 ns 130 -BM_FakeLua_StringGmatch_TCC/1000 9,236,472 ns 5,559,361 ns 126 -BM_FakeLua_StringGsub_GCC/10 13,205 ns 8,969 ns 78,419 -BM_FakeLua_StringGsub_GCC/100 103,458 ns 68,161 ns 10,359 -BM_FakeLua_StringGsub_GCC/1000 1,101,351 ns 657,675 ns 1,070 -BM_FakeLua_StringGsub_TCC/10 13,795 ns 8,996 ns 77,772 -BM_FakeLua_StringGsub_TCC/100 114,274 ns 67,941 ns 10,349 -BM_FakeLua_StringGsub_TCC/1000 1,114,296 ns 658,608 ns 1,073 -BM_FakeLua_StringLen_GCC/10 818 ns 492 ns 1,432,371 -BM_FakeLua_StringLen_GCC/100 796 ns 540 ns 1,299,835 -BM_FakeLua_StringLen_GCC/1000 866 ns 561 ns 1,246,589 -BM_FakeLua_StringLen_GCC/10000 1,914 ns 1,077 ns 647,127 -BM_FakeLua_StringLen_TCC/10 877 ns 535 ns 1,273,369 -BM_FakeLua_StringLen_TCC/100 1,032 ns 585 ns 1,189,078 -BM_FakeLua_StringLen_TCC/1000 901 ns 603 ns 1,162,520 -BM_FakeLua_StringLen_TCC/10000 1,808 ns 1,112 ns 601,399 -BM_FakeLua_StringLower_GCC/10 1,048 ns 600 ns 1,152,148 -BM_FakeLua_StringLower_GCC/100 1,393 ns 702 ns 1,001,106 -BM_FakeLua_StringLower_GCC/1000 1,551 ns 846 ns 816,183 -BM_FakeLua_StringLower_GCC/10000 5,043 ns 2,822 ns 251,014 -BM_FakeLua_StringLower_TCC/10 1,073 ns 692 ns 1,016,288 -BM_FakeLua_StringLower_TCC/100 1,855 ns 1,212 ns 583,384 -BM_FakeLua_StringLower_TCC/1000 8,751 ns 5,402 ns 128,947 -BM_FakeLua_StringLower_TCC/10000 86,072 ns 48,207 ns 14,542 -BM_FakeLua_StringRep_GCC/10 1,033 ns 671 ns 1,049,153 -BM_FakeLua_StringRep_GCC/100 1,806 ns 1,100 ns 630,280 -BM_FakeLua_StringRep_GCC/1000 8,126 ns 4,888 ns 141,355 -BM_FakeLua_StringRep_TCC/10 1,328 ns 752 ns 927,065 -BM_FakeLua_StringRep_TCC/100 2,358 ns 1,389 ns 505,237 -BM_FakeLua_StringRep_TCC/1000 10,189 ns 7,129 ns 98,349 -BM_FakeLua_StringReverse_GCC/10 1,109 ns 604 ns 1,148,807 -BM_FakeLua_StringReverse_GCC/100 1,397 ns 765 ns 919,546 -BM_FakeLua_StringReverse_GCC/1000 2,668 ns 1,438 ns 477,188 -BM_FakeLua_StringReverse_GCC/10000 14,935 ns 8,698 ns 78,927 -BM_FakeLua_StringReverse_TCC/10 925 ns 681 ns 1,037,089 -BM_FakeLua_StringReverse_TCC/100 1,870 ns 1,066 ns 657,331 -BM_FakeLua_StringReverse_TCC/1000 6,110 ns 4,063 ns 172,547 -BM_FakeLua_StringReverse_TCC/10000 52,709 ns 34,603 ns 20,307 -BM_FakeLua_StringSub_GCC/10 2,264 ns 1,345 ns 518,241 -BM_FakeLua_StringSub_GCC/100 2,557 ns 1,430 ns 489,733 -BM_FakeLua_StringSub_GCC/1000 2,763 ns 1,490 ns 466,344 -BM_FakeLua_StringSub_GCC/10000 4,070 ns 2,268 ns 306,435 -BM_FakeLua_StringSub_TCC/10 2,372 ns 1,390 ns 508,219 -BM_FakeLua_StringSub_TCC/100 2,314 ns 1,485 ns 474,333 -BM_FakeLua_StringSub_TCC/1000 3,228 ns 1,543 ns 458,387 -BM_FakeLua_StringSub_TCC/10000 3,832 ns 2,334 ns 301,764 -BM_FakeLua_StringUpper_GCC/10 1,127 ns 601 ns 1,168,948 -BM_FakeLua_StringUpper_GCC/100 1,114 ns 702 ns 992,199 -BM_FakeLua_StringUpper_GCC/1000 1,386 ns 850 ns 820,082 -BM_FakeLua_StringUpper_GCC/10000 5,104 ns 2,832 ns 248,824 -BM_FakeLua_StringUpper_TCC/10 1,169 ns 696 ns 1,006,148 -BM_FakeLua_StringUpper_TCC/100 2,031 ns 1,214 ns 575,272 -BM_FakeLua_StringUpper_TCC/1000 10,406 ns 5,421 ns 129,932 -BM_FakeLua_StringUpper_TCC/10000 81,506 ns 48,151 ns 14,489 -BM_FakeLua_Sum_GCC/10000 6,452 ns 3,508 ns 199,582 -BM_FakeLua_Sum_GCC/100000 50,566 ns 31,381 ns 22,327 -BM_FakeLua_Sum_GCC/1000000 578,521 ns 309,437 ns 2,250 -BM_FakeLua_Sum_GCC/5000000 3,098,218 ns 1,547,859 ns 453 -BM_FakeLua_Sum_TCC/10000 37,857 ns 24,934 ns 27,821 -BM_FakeLua_Sum_TCC/100000 417,183 ns 246,397 ns 2,850 -BM_FakeLua_Sum_TCC/1000000 4,479,399 ns 2,452,786 ns 283 -BM_FakeLua_Sum_TCC/5000000 18,728,703 ns 12,321,859 ns 56 -BM_FakeLua_TableChurn_GCC/100 11,409 ns 6,971 ns 100,756 -BM_FakeLua_TableChurn_GCC/1000 119,994 ns 65,155 ns 10,754 -BM_FakeLua_TableChurn_GCC/500 43,896 ns 33,036 ns 21,217 -BM_FakeLua_TableChurn_TCC/100 57,082 ns 36,778 ns 19,044 -BM_FakeLua_TableChurn_TCC/1000 466,370 ns 354,670 ns 1,972 -BM_FakeLua_TableChurn_TCC/500 281,725 ns 182,147 ns 3,900 -BM_FakeLua_TableConcat_GCC/100 58,710 ns 38,108 ns 18,314 -BM_FakeLua_TableConcat_GCC/1000 708,780 ns 365,628 ns 1,886 -BM_FakeLua_TableConcat_GCC/500 309,868 ns 185,102 ns 3,781 -BM_FakeLua_TableConcat_TCC/100 101,629 ns 54,515 ns 13,123 -BM_FakeLua_TableConcat_TCC/1000 914,385 ns 523,737 ns 1,328 -BM_FakeLua_TableConcat_TCC/500 487,979 ns 262,511 ns 2,665 -BM_FakeLua_TableCreate_GCC/1000 33,890 ns 20,427 ns 34,468 -BM_FakeLua_TableCreate_GCC/3000 117,469 ns 69,451 ns 10,059 -BM_FakeLua_TableCreate_GCC/5000 251,652 ns 128,429 ns 5,479 -BM_FakeLua_TableCreate_TCC/1000 193,548 ns 117,177 ns 6,020 -BM_FakeLua_TableCreate_TCC/3000 605,946 ns 392,114 ns 1,794 -BM_FakeLua_TableCreate_TCC/5000 1,235,125 ns 705,496 ns 988 -BM_FakeLua_TableInsert_GCC/100 5,796 ns 3,230 ns 217,272 -BM_FakeLua_TableInsert_GCC/1000 37,577 ns 23,234 ns 30,067 -BM_FakeLua_TableInsert_GCC/500 18,496 ns 12,007 ns 57,879 -BM_FakeLua_TableInsert_GCC/5000 230,109 ns 141,009 ns 4,927 -BM_FakeLua_TableInsert_TCC/100 25,990 ns 14,008 ns 51,081 -BM_FakeLua_TableInsert_TCC/1000 190,463 ns 121,881 ns 5,882 -BM_FakeLua_TableInsert_TCC/500 122,192 ns 62,347 ns 11,427 -BM_FakeLua_TableInsert_TCC/5000 1,128,122 ns 726,417 ns 929 -BM_FakeLua_TableMove_GCC/100 8,264 ns 5,510 ns 123,002 -BM_FakeLua_TableMove_GCC/1000 65,516 ns 40,768 ns 17,565 -BM_FakeLua_TableMove_GCC/500 31,437 ns 20,959 ns 33,710 -BM_FakeLua_TableMove_GCC/5000 415,416 ns 250,364 ns 2,806 -BM_FakeLua_TableMove_TCC/100 40,035 ns 23,025 ns 30,393 -BM_FakeLua_TableMove_TCC/1000 291,431 ns 196,812 ns 3,556 -BM_FakeLua_TableMove_TCC/500 148,661 ns 99,540 ns 7,076 -BM_FakeLua_TableMove_TCC/5000 1,689,098 ns 1,175,769 ns 595 -BM_FakeLua_TablePack_GCC/1 1,240 ns 784 ns 894,882 -BM_FakeLua_TablePack_TCC/1 3,413 ns 1,830 ns 383,737 -BM_FakeLua_TableRemove_GCC/100 10,904 ns 5,496 ns 128,245 -BM_FakeLua_TableRemove_GCC/1000 84,466 ns 46,212 ns 15,196 -BM_FakeLua_TableRemove_GCC/500 37,355 ns 23,386 ns 28,737 -BM_FakeLua_TableRemove_GCC/5000 430,685 ns 256,491 ns 2,710 -BM_FakeLua_TableRemove_TCC/100 41,184 ns 23,119 ns 30,006 -BM_FakeLua_TableRemove_TCC/1000 419,694 ns 215,409 ns 3,251 -BM_FakeLua_TableRemove_TCC/500 177,077 ns 107,216 ns 6,561 -BM_FakeLua_TableRemove_TCC/5000 1,793,838 ns 1,185,962 ns 589 -BM_FakeLua_TableSort_GCC/100 44,298 ns 28,004 ns 25,024 -BM_FakeLua_TableSort_GCC/1000 562,565 ns 330,027 ns 2,138 -BM_FakeLua_TableSort_GCC/500 271,424 ns 154,615 ns 4,519 -BM_FakeLua_TableSort_TCC/100 66,261 ns 39,107 ns 17,856 -BM_FakeLua_TableSort_TCC/1000 730,477 ns 431,083 ns 1,617 -BM_FakeLua_TableSort_TCC/500 306,301 ns 206,069 ns 3,371 -BM_FakeLua_TailRecursion_GCC/100 777 ns 426 ns 1,648,769 -BM_FakeLua_TailRecursion_GCC/1000 1,205 ns 712 ns 979,895 -BM_FakeLua_TailRecursion_GCC/5000 3,796 ns 1,951 ns 358,052 -BM_FakeLua_TailRecursion_TCC/100 1,584 ns 927 ns 759,379 -BM_FakeLua_TailRecursion_TCC/1000 9,233 ns 5,203 ns 100,000 -BM_FakeLua_TailRecursion_TCC/5000 42,186 ns 24,371 ns 29,042 -BM_FakeLua_ToNumber_GCC/1 802 ns 542 ns 1,304,733 -BM_FakeLua_ToNumber_TCC/1 1,150 ns 693 ns 1,016,080 -BM_FakeLua_ToString_GCC/10 1,194 ns 647 ns 1,097,100 -BM_FakeLua_ToString_GCC/100 979 ns 646 ns 1,079,352 -BM_FakeLua_ToString_GCC/500 1,059 ns 650 ns 1,080,277 -BM_FakeLua_ToString_TCC/10 1,174 ns 701 ns 1,016,080 -BM_FakeLua_ToString_TCC/100 1,203 ns 700 ns 987,485 -BM_FakeLua_ToString_TCC/500 959 ns 701 ns 999,183 -BM_FakeLua_Variadic_GCC/1 1,215 ns 754 ns 928,022 -BM_FakeLua_Variadic_TCC/1 2,233 ns 1,224 ns 575,243 -BM_FakeLua_Vector3_GCC/10000 607,809 ns 309,974 ns 2,261 -BM_FakeLua_Vector3_GCC/100000 4,839,630 ns 3,056,228 ns 228 -BM_FakeLua_Vector3_GCC/1000000 47,773,627 ns 30,897,897 ns 22 -BM_FakeLua_Vector3_TCC/10000 3,344,400 ns 2,091,722 ns 331 -BM_FakeLua_Vector3_TCC/100000 42,147,447 ns 21,209,412 ns 33 -BM_FakeLua_Vector3_TCC/1000000 347,663,874 ns 210,547,754 ns 3 -BM_Lua_BinarySearch/100 60,634 ns 37,029 ns 18,896 -BM_Lua_BinarySearch/1000 959,697 ns 580,933 ns 1,221 -BM_Lua_BinarySearch/500 440,300 ns 254,492 ns 2,758 -BM_Lua_BubbleSort/100 594,801 ns 338,786 ns 2,093 -BM_Lua_BubbleSort/200 2,086,556 ns 1,328,612 ns 534 -BM_Lua_BubbleSort/50 164,153 ns 85,624 ns 8,213 -BM_Lua_Closure/100 39,957 ns 22,696 ns 30,670 -BM_Lua_Closure/1000 348,764 ns 226,293 ns 3,086 -BM_Lua_EmptyCall/10000 722,370 ns 439,736 ns 1,597 -BM_Lua_EmptyCall/100000 7,933,740 ns 4,354,814 ns 159 -BM_Lua_FastPow/1234567/7654321/1000000007 1,930 ns 1,152 ns 594,367 -BM_Lua_FastPow/2/1000/1000000007 1,039 ns 593 ns 1,199,681 -BM_Lua_FastPow/7/1000000/1000000007 1,725 ns 974 ns 722,218 -BM_Lua_Fibonacci/20 1,569,773 ns 860,801 ns 824 -BM_Lua_Fibonacci/25 15,600,871 ns 9,626,976 ns 72 -BM_Lua_Fibonacci/30 168,261,430 ns 106,484,626 ns 7 -BM_Lua_Fibonacci/32 503,047,483 ns 277,915,868 ns 3 -BM_Lua_FloatPoly/1000000 195,928,101 ns 99,944,530 ns 7 -BM_Lua_GCD/123456789/987654321 337 ns 212 ns 3,268,053 -BM_Lua_GCD/2147483647/1073741823 370 ns 191 ns 3,676,078 -BM_Lua_GCD/832040/514229 1,132 ns 730 ns 942,430 -BM_Lua_HashInsert/100 87,286 ns 48,607 ns 14,714 -BM_Lua_HashInsert/1000 786,406 ns 499,118 ns 1,378 -BM_Lua_HashInsert/500 378,706 ns 241,489 ns 2,867 -BM_Lua_HashLookup/100 68,701 ns 39,114 ns 17,775 -BM_Lua_HashLookup/1000 649,131 ns 414,686 ns 1,713 -BM_Lua_HashLookup/500 315,722 ns 205,240 ns 3,408 -BM_Lua_InsertionSort/100 389,213 ns 228,283 ns 3,094 -BM_Lua_InsertionSort/200 1,452,926 ns 894,653 ns 782 -BM_Lua_InsertionSort/50 92,414 ns 59,708 ns 11,653 -BM_Lua_MatMul 5,810 ns 3,698 ns 190,328 -BM_Lua_MathExpLog/100000 30,397,983 ns 17,460,595 ns 41 -BM_Lua_MathMinMax/100000 36,855,906 ns 22,431,932 ns 31 -BM_Lua_MathSqrt/100000 11,202,131 ns 6,162,470 ns 112 -BM_Lua_MathTrig/100000 28,341,267 ns 17,880,549 ns 39 -BM_Lua_MixedAlloc/100 148,240 ns 87,270 ns 8,030 -BM_Lua_MixedAlloc/1000 1,718,443 ns 897,348 ns 810 -BM_Lua_MixedAlloc/500 703,958 ns 442,235 ns 1,559 -BM_Lua_MultiReturn/1000 96,520 ns 59,128 ns 11,867 -BM_Lua_MultiReturn/10000 926,960 ns 582,937 ns 1,229 -BM_Lua_NestedTable/1000 466,508 ns 292,829 ns 2,366 -BM_Lua_NestedTable/10000 5,063,234 ns 2,900,521 ns 240 -BM_Lua_Popcount/1000 240,702 ns 144,200 ns 4,801 -BM_Lua_Popcount/10000 2,783,275 ns 1,828,025 ns 382 -BM_Lua_Popcount/100000 35,208,870 ns 22,105,500 ns 32 -BM_Lua_PowMod/1234567/7654321/1000000007 2,090 ns 1,299 ns 544,422 -BM_Lua_PowMod/2/1000/1000000007 1,028 ns 661 ns 1,072,320 -BM_Lua_PowMod/7/1000000/1000000007 2,075 ns 1,100 ns 631,710 -BM_Lua_Recursion/10 13,957 ns 8,364 ns 81,503 -BM_Lua_Recursion/20 1,412,171 ns 1,043,444 ns 689 -BM_Lua_Recursion/25 17,663,822 ns 11,536,223 ns 61 -BM_Lua_Sieve/100 15,105 ns 9,868 ns 70,790 -BM_Lua_Sieve/1000 131,859 ns 75,406 ns 9,411 -BM_Lua_Sieve/500 59,479 ns 39,012 ns 17,749 -BM_Lua_Sieve/5000 537,121 ns 378,408 ns 1,865 -BM_Lua_StringByte/10 498 ns 303 ns 2,260,816 -BM_Lua_StringByte/100 760 ns 460 ns 1,530,782 -BM_Lua_StringByte/1000 927 ns 624 ns 1,147,141 -BM_Lua_StringChar/10 4,724 ns 3,004 ns 233,911 -BM_Lua_StringChar/100 26,038 ns 17,295 ns 40,665 -BM_Lua_StringChar/500 115,847 ns 75,332 ns 9,010 -BM_Lua_StringChurn/100 124,238 ns 72,452 ns 9,398 -BM_Lua_StringChurn/1000 1,713,625 ns 971,816 ns 707 -BM_Lua_StringChurn/500 726,842 ns 474,138 ns 1,477 -BM_Lua_StringFind/10 816 ns 457 ns 1,510,131 -BM_Lua_StringFind/100 1,002 ns 571 ns 1,225,626 -BM_Lua_StringFind/1000 1,076 ns 756 ns 933,030 -BM_Lua_StringFind/10000 3,359 ns 2,139 ns 327,326 -BM_Lua_StringFindPattern/1000 1,536,606 ns 938,600 ns 753 -BM_Lua_StringFormat/10 4,810 ns 2,816 ns 249,862 -BM_Lua_StringFormat/100 45,648 ns 28,036 ns 25,220 -BM_Lua_StringFormat/500 217,375 ns 138,971 ns 5,005 -BM_Lua_StringGmatch/1000 1,797,588 ns 1,101,192 ns 634 -BM_Lua_StringGsub/10 1,551 ns 942 ns 743,199 -BM_Lua_StringGsub/100 7,349 ns 4,529 ns 155,040 -BM_Lua_StringGsub/1000 67,386 ns 37,743 ns 18,523 -BM_Lua_StringLen/10 255 ns 169 ns 4,208,529 -BM_Lua_StringLen/100 512 ns 317 ns 2,210,217 -BM_Lua_StringLen/1000 625 ns 491 ns 1,417,701 -BM_Lua_StringLen/10000 2,719 ns 1,979 ns 358,597 -BM_Lua_StringLower/10 596 ns 322 ns 2,189,914 -BM_Lua_StringLower/100 1,225 ns 747 ns 942,770 -BM_Lua_StringLower/1000 3,564 ns 2,181 ns 320,649 -BM_Lua_StringLower/10000 29,966 ns 17,111 ns 41,238 -BM_Lua_StringRep/10 737 ns 408 ns 1,705,699 -BM_Lua_StringRep/100 1,822 ns 997 ns 694,622 -BM_Lua_StringRep/1000 9,142 ns 5,390 ns 126,264 -BM_Lua_StringReverse/10 597 ns 307 ns 2,261,963 -BM_Lua_StringReverse/100 1,182 ns 706 ns 961,826 -BM_Lua_StringReverse/1000 3,323 ns 1,913 ns 365,656 -BM_Lua_StringReverse/10000 24,703 ns 14,399 ns 48,160 -BM_Lua_StringSub/10 617 ns 364 ns 1,942,516 -BM_Lua_StringSub/100 1,105 ns 594 ns 1,177,842 -BM_Lua_StringSub/1000 1,379 ns 906 ns 766,324 -BM_Lua_StringSub/10000 4,908 ns 3,045 ns 231,995 -BM_Lua_StringUpper/10 567 ns 323 ns 2,190,345 -BM_Lua_StringUpper/100 1,342 ns 735 ns 951,815 -BM_Lua_StringUpper/1000 3,109 ns 1,965 ns 355,243 -BM_Lua_StringUpper/10000 27,760 ns 14,242 ns 48,029 -BM_Lua_Sum/10000 180,892 ns 92,068 ns 7,391 -BM_Lua_Sum/100000 1,380,171 ns 911,977 ns 771 -BM_Lua_Sum/1000000 16,461,078 ns 9,233,507 ns 76 -BM_Lua_Sum/5000000 74,960,840 ns 46,977,732 ns 15 -BM_Lua_TableChurn/100 88,809 ns 61,258 ns 11,328 -BM_Lua_TableChurn/1000 820,002 ns 602,970 ns 1,148 -BM_Lua_TableChurn/500 477,274 ns 305,247 ns 2,384 -BM_Lua_TableConcat/100 71,908 ns 36,736 ns 19,340 -BM_Lua_TableConcat/1000 585,304 ns 361,357 ns 1,936 -BM_Lua_TableConcat/500 325,897 ns 177,956 ns 3,874 -BM_Lua_TableCreate/1000 47,111 ns 29,524 ns 23,831 -BM_Lua_TableCreate/3000 138,745 ns 86,404 ns 8,046 -BM_Lua_TableCreate/5000 257,284 ns 145,201 ns 4,849 -BM_Lua_TableInsert/100 23,857 ns 13,362 ns 53,418 -BM_Lua_TableInsert/1000 208,845 ns 116,643 ns 5,895 -BM_Lua_TableInsert/500 107,318 ns 57,789 ns 11,379 -BM_Lua_TableInsert/5000 928,408 ns 564,608 ns 1,170 -BM_Lua_TableMove/100 12,817 ns 8,743 ns 81,064 -BM_Lua_TableMove/1000 92,904 ns 50,879 ns 10,000 -BM_Lua_TableMove/500 44,285 ns 28,002 ns 25,009 -BM_Lua_TableMove/5000 355,241 ns 250,016 ns 2,787 -BM_Lua_TablePack/1 2,840 ns 1,516 ns 462,846 -BM_Lua_TableRemove/100 24,388 ns 17,343 ns 40,851 -BM_Lua_TableRemove/1000 261,457 ns 152,441 ns 4,709 -BM_Lua_TableRemove/500 126,597 ns 76,961 ns 9,300 -BM_Lua_TableRemove/5000 1,354,665 ns 828,342 ns 831 -BM_Lua_TableSort/100 42,572 ns 30,042 ns 23,123 -BM_Lua_TableSort/1000 714,671 ns 398,060 ns 1,749 -BM_Lua_TableSort/500 289,863 ns 184,677 ns 3,803 -BM_Lua_TailRecursion/100 8,300 ns 4,805 ns 160,595 -BM_Lua_TailRecursion/1000 73,087 ns 42,646 ns 15,958 -BM_Lua_TailRecursion/5000 346,152 ns 209,783 ns 3,406 -BM_Lua_ToNumber/1 529 ns 285 ns 2,447,960 -BM_Lua_ToString/10 903 ns 478 ns 1,479,778 -BM_Lua_ToString/100 922 ns 481 ns 1,460,211 -BM_Lua_ToString/500 739 ns 477 ns 1,451,251 -BM_Lua_Variadic/1 1,069 ns 685 ns 1,026,991 -BM_Lua_Vector3/10000 2,302,920 ns 1,493,404 ns 464 -BM_Lua_Vector3/100000 24,833,907 ns 14,947,118 ns 46 -BM_Lua_Vector3/1000000 239,352,621 ns 149,734,385 ns 5 -``` diff --git a/cmake/gen_c_runtime_embed.py b/cmake/gen_c_runtime_embed.py new file mode 100644 index 00000000..ae1019c1 --- /dev/null +++ b/cmake/gen_c_runtime_embed.py @@ -0,0 +1,36 @@ +#!/usr/bin/env python3 +"""Generate the constexpr C runtime string from the single checked-in body. + +src/compile/c_runtime_cov.h is the editable source and the gcov view. The +generated embed starts with a newline so CGen's `#line 1` maps the first +content line back onto line 1 of that file. +""" + +import pathlib +import sys + +DELIMITER = b")FLRT" + + +def main() -> int: + if len(sys.argv) != 3: + print("usage: gen_c_runtime_embed.py INPUT OUTPUT", file=sys.stderr) + return 2 + src = pathlib.Path(sys.argv[1]) + dst = pathlib.Path(sys.argv[2]) + body = src.read_bytes() + if DELIMITER in body: + print(f"{src} contains raw-string delimiter {DELIMITER.decode()}", file=sys.stderr) + return 1 + text = b'inline constexpr const char *kCRuntimeHeader = R"FLRT(\n' + body + b')FLRT";\n' + dst.parent.mkdir(parents=True, exist_ok=True) + if dst.is_file() and dst.read_bytes() == text: + return 0 + tmp = dst.with_suffix(dst.suffix + ".tmp") + tmp.write_bytes(text) + tmp.replace(dst) + return 0 + + +if __name__ == "__main__": + raise SystemExit(main()) diff --git a/fuzz/README.md b/fuzz/README.md index 0d2e14e7..2893805b 100644 --- a/fuzz/README.md +++ b/fuzz/README.md @@ -38,9 +38,9 @@ fuzz/ ## Fuzz Target 说明 -### fuzz_compile — GCC 后端健壮性测试 +### fuzz_compile — GCC 与解释器健壮性测试 -测试 `CompileString()` 在生产路径(GCC 后端)下的健壮性。检测: +测试 `CompileString()` 在生产路径(GCC 后端)和 `JIT_INTERP` 下的健壮性。两条路径使用各自独立的 State。检测: - Parser / Lexer / Compiler pipeline 中的 crash / segfault - GCC 编译生成代码、`__fakelua_init` 执行中的 crash / 内存错误 @@ -48,11 +48,11 @@ fuzz/ - UBSan 检测的未定义行为(空指针解引用、整数溢出) - 非预期的异常类型 -**策略**:关闭 TCC,只走 GCC。对任意输入调用 `CompileString`,捕获 `FakeluaException`(预期行为),任何其他 crash 或异常都是 bug。 +**策略**:GCC 路径关闭 TCC 和解释器;解释器路径关闭 TCC 和 GCC。对任意输入调用 `CompileString`,捕获 `FakeluaException`(预期行为),任何其他 crash 或异常都是 bug。 ### fuzz_differential — 差分测试(fakelua vs Lua 5.4) -同一个 Lua 脚本分别在 fakelua(GCC 后端)和 Lua 5.4 中编译执行,对比结果: +同一个 Lua 脚本分别在 fakelua GCC、fakelua 解释器和 Lua 5.4 中编译执行。Lua 只调用一次,两个后端都和这次结果对比: | 场景 | fakelua | Lua 5.4 | 判断 | |------|---------|---------|------| diff --git a/fuzz/fuzz_bridge.cpp b/fuzz/fuzz_bridge.cpp index 9e579c2c..02293c58 100644 --- a/fuzz/fuzz_bridge.cpp +++ b/fuzz/fuzz_bridge.cpp @@ -59,6 +59,7 @@ int fuzz_fakelua_compile_string(void *s, const char *src, int len) { fakelua::CompileConfig cfg; cfg.debug_mode = false; cfg.disable_jit[fakelua::JIT_TCC] = true; + cfg.disable_jit[fakelua::JIT_INTERP] = true; return CompileWith(s, src, len, cfg); } @@ -68,9 +69,26 @@ int fuzz_fakelua_compile_string_executable(void *s, const char *src, int len) { fakelua::CompileConfig cfg; cfg.debug_mode = false; cfg.disable_jit[fakelua::JIT_TCC] = true; + cfg.disable_jit[fakelua::JIT_INTERP] = true; return CompileWith(s, src, len, cfg); } +static fakelua::CompileConfig InterpOnlyConfig() { + fakelua::CompileConfig cfg; + cfg.debug_mode = false; + cfg.disable_jit[fakelua::JIT_TCC] = true; + cfg.disable_jit[fakelua::JIT_GCC] = true; + return cfg; +} + +int fuzz_fakelua_compile_string_interp(void *s, const char *src, int len) { + return CompileWith(s, src, len, InterpOnlyConfig()); +} + +int fuzz_fakelua_compile_string_interp_executable(void *s, const char *src, int len) { + return CompileWith(s, src, len, InterpOnlyConfig()); +} + int fuzz_fakelua_call_int(void *s, const char *name, int64_t *out) { if (!s || !name || !out) return 0; @@ -88,4 +106,21 @@ int fuzz_fakelua_call_int(void *s, const char *name, int64_t *out) { } } +int fuzz_fakelua_call_int_interp(void *s, const char *name, int64_t *out) { + if (!s || !name || !out) return 0; + + auto *state = static_cast(s); + + try { + fakelua::Call(state, fakelua::JIT_INTERP, name, *out); + return 1; + } catch (const fakelua::FakeluaException &) { + return 0; + } catch (const std::exception &) { + std::abort(); + } catch (...) { + std::abort(); + } +} + }// extern "C" diff --git a/fuzz/fuzz_bridge.h b/fuzz/fuzz_bridge.h index 01bde481..80cfe5c8 100644 --- a/fuzz/fuzz_bridge.h +++ b/fuzz/fuzz_bridge.h @@ -28,6 +28,12 @@ int fuzz_fakelua_compile_string_executable(void *s, const char *src, int len); // Returns 1 on success, 0 on expected failure, aborts on unexpected failure. int fuzz_fakelua_call_int(void *s, const char *name, int64_t *out); +// Interpreter-only counterparts. GCC and TCC are disabled so the input +// exercises bytecode codegen and the VM, not the C JIT. +int fuzz_fakelua_compile_string_interp(void *s, const char *src, int len); +int fuzz_fakelua_compile_string_interp_executable(void *s, const char *src, int len); +int fuzz_fakelua_call_int_interp(void *s, const char *name, int64_t *out); + #ifdef __cplusplus } #endif diff --git a/fuzz/fuzz_compile.cpp b/fuzz/fuzz_compile.cpp index 378faba6..a99d4b50 100644 --- a/fuzz/fuzz_compile.cpp +++ b/fuzz/fuzz_compile.cpp @@ -16,7 +16,11 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { // fuzz_bridge aborts on unexpected exceptions; returns 0/1 for expected cases fuzz_fakelua_compile_string(state, reinterpret_cast(data), static_cast(size)); - fuzz_fakelua_delete_state(state); + + void *interp = fuzz_fakelua_new_state(); + if (!interp) return 0; + fuzz_fakelua_compile_string_interp(interp, reinterpret_cast(data), static_cast(size)); + fuzz_fakelua_delete_state(interp); return 0; } diff --git a/fuzz/fuzz_differential.cpp b/fuzz/fuzz_differential.cpp index a3713e16..f65463d8 100644 --- a/fuzz/fuzz_differential.cpp +++ b/fuzz/fuzz_differential.cpp @@ -89,50 +89,57 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { } } - // ---- Compare compilation results ---- - // Lua-accepted + fakelua-rejected → expected (fakelua is a subset) - // fakelua-accepted + Lua-rejected → INTERESTING (potential bug) - if (flua_compiled && !lua_compiled) { - fprintf(stderr, "\n[DIFF FUZZ] fakelua compiled but Lua 5.4 rejected:\n%s\n", script.c_str()); - std::abort(); - } - - // ---- If both compiled, compare execution ---- - if (flua_compiled && lua_compiled) { - // fakelua call - int64_t flua_ret = -999; - int flua_called = fuzz_fakelua_call_int(flua, "fuzz_test", &flua_ret); - - // Lua call - int64_t lua_ret = -999; - int lua_called = 0; + // Lua is called once. Both backends compare against that result; a second + // Lua call would disagree with a non-idempotent function. + int64_t lua_ret = -999; + int lua_called = 0; + if (lua_compiled) { lua_getglobal(L, "fuzz_test"); - if (lua_isfunction(L, -1)) { - if (lua_pcall(L, 0, 1, 0) == LUA_OK) { - if (lua_isinteger(L, -1)) { - lua_ret = static_cast(lua_tointeger(L, -1)); - lua_called = 1; - } - } + if (lua_isfunction(L, -1) && lua_pcall(L, 0, 1, 0) == LUA_OK && lua_isinteger(L, -1)) { + lua_ret = static_cast(lua_tointeger(L, -1)); + lua_called = 1; } + } - // Same asymmetry as the compilation check above: fakelua is a subset, so it - // may reject at runtime what Lua accepts (it has no implicit string→number - // coercion, for one). Only the other direction — fakelua succeeding where - // Lua raises an error — means fakelua is too permissive. - if (flua_called && !lua_called) { - fprintf(stderr, "\n[DIFF FUZZ] fakelua call succeeded but Lua 5.4 failed:\n%s\n", script.c_str()); + auto compare_backend = [&](const char *which, int compiled, int called, int64_t ret) { + if (compiled && !lua_compiled) { + fprintf(stderr, "\n[DIFF FUZZ] %s compiled but Lua 5.4 rejected:\n%s\n", which, script.c_str()); std::abort(); } - if (flua_called && lua_called && flua_ret != lua_ret) { - fprintf(stderr, "\n[DIFF FUZZ] return mismatch: flua=%ld lua=%ld\n%s\n", (long) flua_ret, (long) lua_ret, script.c_str()); + if (!(compiled && lua_compiled)) return; + if (called && !lua_called) { + fprintf(stderr, "\n[DIFF FUZZ] %s call succeeded but Lua 5.4 failed:\n%s\n", which, script.c_str()); std::abort(); } + if (called && lua_called && ret != lua_ret) { + fprintf(stderr, "\n[DIFF FUZZ] %s return mismatch: flua=%ld lua=%ld\n%s\n", which, (long) ret, (long) lua_ret, script.c_str()); + std::abort(); + } + }; + + int64_t flua_ret = -999; + int flua_called = 0; + if (flua_compiled && lua_compiled) { + flua_called = fuzz_fakelua_call_int(flua, "fuzz_test", &flua_ret); + } + compare_backend("gcc", flua_compiled, flua_called, flua_ret); + + int interp_compiled = 0; + void *interp = fuzz_fakelua_new_state(); + if (interp) { + interp_compiled = fuzz_fakelua_compile_string_interp_executable(interp, script.c_str(), static_cast(script.size())); + } + int64_t interp_ret = -999; + int interp_called = 0; + if (interp_compiled && lua_compiled) { + interp_called = fuzz_fakelua_call_int_interp(interp, "fuzz_test", &interp_ret); } + compare_backend("interpreter", interp_compiled, interp_called, interp_ret); // Cleanup if (L) lua_close(L); if (flua) fuzz_fakelua_delete_state(flua); + if (interp) fuzz_fakelua_delete_state(interp); return 0; } diff --git a/include/fakelua.h b/include/fakelua.h index 4719813d..e0bcb54b 100644 --- a/include/fakelua.h +++ b/include/fakelua.h @@ -13,9 +13,9 @@ namespace fakelua { // 版本号定义(主版本.次版本.patch),_VERSION 字符串在此基础上生成 #define FAKELUA_VERSION_MAJOR 2 -#define FAKELUA_VERSION_MINOR 0 +#define FAKELUA_VERSION_MINOR 1 #define FAKELUA_VERSION_PATCH 0 -#define FAKELUA_VERSION_STRING "2.0.0" +#define FAKELUA_VERSION_STRING "2.1.0" constexpr size_t kMaxFunctionInputParams = 32; constexpr size_t kMaxMathSpecializedParams = 8; diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index e2cac2a1..f9cd1ffb 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -57,7 +57,12 @@ if (NOT FAKELUA_TINYCC_BUILD_JOBS GREATER 0) endif () set(FAKELUA_TINYCC_GIT_URL "https://github.com/TinyCC/tinycc.git" CACHE STRING "TinyCC git repository URL") -set(FAKELUA_TINYCC_GIT_TAG "mob" CACHE STRING "TinyCC git branch/tag/commit") +# Pin the previously floating "mob" tip. A caller can still override the tag; +# an old cache that still says "mob" is moved onto this commit. +set(FAKELUA_TINYCC_GIT_TAG "0fb54300b56512754221d80adda85ddb9815bceb" CACHE STRING "TinyCC git branch/tag/commit") +if (FAKELUA_TINYCC_GIT_TAG STREQUAL "mob") + set(FAKELUA_TINYCC_GIT_TAG "0fb54300b56512754221d80adda85ddb9815bceb" CACHE STRING "TinyCC git branch/tag/commit" FORCE) +endif () CPMAddPackage( NAME tinycc_src GIT_REPOSITORY ${FAKELUA_TINYCC_GIT_URL} @@ -602,7 +607,7 @@ target_include_directories(fakelua PUBLIC ) set_target_properties(fakelua PROPERTIES - VERSION 2.0.0 + VERSION 2.1.0 SOVERSION 2 INSTALL_NAME_DIR "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}" ) diff --git a/src/compile/c_gen.cpp b/src/compile/c_gen.cpp index 15a01042..2bee1dc4 100644 --- a/src/compile/c_gen.cpp +++ b/src/compile/c_gen.cpp @@ -620,7 +620,7 @@ void CGen::GenerateHeader() { Out() << "static void * fl_temp_heap;\n"; Out() << "static void * fl_const_heap;\n"; - // C 运行时类型定义、宏 and 函数(从 c_runtime_header.h 提取,便于独立维护) + // C 运行时类型定义、宏 and 函数。正文只维护 c_runtime_cov.h,由 CMake 生成嵌入串。 Out() << "static const char *fl_lua_file;\n"; // Attribute the pasted Fl* runtime to the checked-in view header so USE_COV diff --git a/src/compile/c_runtime_header.h b/src/compile/c_runtime_header.h index f137e541..3cddd1d4 100644 --- a/src/compile/c_runtime_header.h +++ b/src/compile/c_runtime_header.h @@ -1,3527 +1,16 @@ #pragma once -// C runtime header for generated JIT code. -// This file contains the C type definitions, macros, and runtime functions -// that are injected into every generated C compilation unit. -// Extracted from c_gen.cpp for maintainability. -// -// Coverage view: src/compile/c_runtime_cov.h holds the same raw C body (no C++ -// wrapper) so Codecov can browse Fl* hits. CGen emits -// #line 1 "src/compile/c_runtime_cov.h" -// around kCRuntimeHeader. Keep that file byte-identical to the raw-string body -// (leading newline in the string is not part of the file; see GenerateHeader). +// C runtime injected into every generated JIT translation unit. +// Edit src/compile/c_runtime_cov.h. CMake generates c_runtime_embed.inc from +// that file; the embed is the only copy of the body inside the C++ binary. +// CGen writes `#line 1 "src/compile/c_runtime_cov.h"` immediately before +// kCRuntimeHeader. The generated string starts with '\n', so logical line 1 +// is the first line of c_runtime_cov.h. namespace fakelua { -// Stable in-repo path used in #line for gcov/lcov/Codecov (repo-root relative). inline constexpr const char *kCRuntimeCovPath = "src/compile/c_runtime_cov.h"; -// The C runtime header content as a raw string literal. -// The _S (State pointer) is prepended by CGen::GenerateHeader() before this content. -// Source of truth for the runtime text; sync c_runtime_cov.h when editing. -inline constexpr const char *kCRuntimeHeader = R"FL_RUNTIME( -#include - -// 运行期报错定位槽。字段顺序与类型必须和 state/state.h 的 fakelua::LuaLoc 一致: -// 生成的代码直接按这个布局读写 State 上的实例。 -typedef struct FlLuaLoc { - const char *file; - int line; -} FlLuaLoc; - -typedef struct VarString { - int size_; - uint32_t hash_; - char data_[0]; -} VarString; - -typedef struct VarTable VarTable; -typedef struct VarMulti VarMulti; -typedef struct VarClosure VarClosure; - -typedef struct CVar { - int type_; - int flag_; - - union { - bool b; - int64_t i; - double f; - VarString *s; - VarTable *t; - VarMulti *m; - VarClosure *cl; - } data_; -} CVar; - -typedef struct VarClosure { - void *func_ptr; - int upvalue_count; - int expected_arg_count; - bool is_vararg; - const char *code_str; - CVar *upvalues[0]; -} VarClosure; - -// 编译期断言:确保 CVar 大小为 16 字节 -typedef char check_cvar_size[sizeof(CVar) == 16 ? 1 : -1]; - -typedef struct VarEntry { - CVar key; - CVar val; - uint32_t hash; -} VarEntry; - -typedef struct TableNode { - VarEntry entry; - uint32_t next; - uint32_t active_pos; -} TableNode; - -struct VarTable; - -typedef CVar (*SpecGetFn)(VarTable *tbl, CVar k, bool *finish); -typedef void (*SpecSetFn)(VarTable *tbl, CVar k, CVar v, bool *finish); - -struct VarTable { - uint32_t count_; - uint32_t bucket_count_; - TableNode *nodes_; - uint32_t *active_list_; - VarEntry quick_data_[8]; - uint32_t free_list_idx_; - void *spec; - SpecGetFn spec_get; - SpecSetFn spec_set; - CVar *spec_keys; - CVar *spec_vals; - uint32_t spec_count; - /* 连续整数键前缀长度(# 运算符结果)的缓存。seq_len_valid_ == 0 代表缓存无效, - 需要重算——因此把 VarTable 整体清零的分配路径天然落在安全的重算分支上。 */ - uint32_t seq_len_valid_; - int64_t seq_len_; - /* 数组部分:整数键 1..arr_size_ 只存放在 arr_ 中(允许 nil 空洞),哈希部分不含 [1, arr_size_+1] - 范围内的整数键。arr_size_ 只增不减,遍历时偏移保持稳定。spec 表不使用数组部分。 - 哈希部分写满时按 Lua 的规则(整数键占用率 > 50%)把整数键迁入数组,见 FlArrTryResize。 */ - CVar *arr_; - uint32_t arr_size_; - uint32_t arr_cap_; -}; - -typedef struct State State; - -#define LIKELY(x) __builtin_expect(!!(x), 1) -#define UNLIKELY(x) __builtin_expect(!!(x), 0) - -#define STR_SIZE(s) ((s)->size_) -#define STR_DATA(s) ((s)->data_) - -enum { - VAR_NIL = 0, - VAR_BOOL = 1, - VAR_INT = 2, - VAR_FLOAT = 3, - VAR_STRING = 4, - VAR_STRINGID = 5, - VAR_TABLE = 6, - VAR_MULTI = 7, - VAR_CLOSURE = 8, -}; - -// CVar flag bits: 第 0 位表示该变量为只读(const),不可修改其值或表字段。 -// __fakelua_init_flag__:false=仍在 __fakelua_init(可填充 const 表);true=init 已结束。 -// init 期间 NEWTABLE/SET_TABLE 创建的表直接打 CONST_FLAG(整棵树,含嵌套); -// SETTABLE 仅在 init 结束后才因 CONST_FLAG 抛错,以便构造器写字段。 -static const bool fl_const_alloc_false = false; -static const bool *fl_const_alloc = &fl_const_alloc_false; -#define FL_IN_CONST_INIT (!__fakelua_init_flag__ || *fl_const_alloc) - -#define CONST_FLAG 0x1 -#define FL_CHECK_CONST_TABLE(t) \ - do { \ - if (UNLIKELY(((t).flag_ & CONST_FLAG) && !FL_IN_CONST_INIT)) { \ - FL_RAISE(_S, "attempt to modify a const table"); \ - } \ - } while (0) -#define FL_CHECK_CONST_TABLE_HINT(t) \ - do { \ - if (UNLIKELY(((t).flag_ & CONST_FLAG) && !FL_IN_CONST_INIT)) { \ - FL_RAISE_HINT(_S, "attempt to modify a const table"); \ - } \ - } while (0) - -/* 探测整数键 k 是否存在且值非 nil。查找逻辑与 FlGetTableInt 一致,但直接作用于 - VarTable*,供序列长度计算与缓存维护复用。必须查 spec:特化表的 1..n 只在 - spec 里,不在 quick/nodes。以前从 spec_count 起扫,等于把字段个数当成 #。 */ -static inline bool FlTableHasIntKey(VarTable *t, int64_t k) { - if (t->spec_get) { - CVar key_cvar; - key_cvar.type_ = VAR_INT; - key_cvar.flag_ = 0; - key_cvar.data_.i = k; - bool __finish = false; - CVar __r = t->spec_get(t, key_cvar, &__finish); - if (__finish) return __r.type_ != VAR_NIL; - } - if ((uint64_t) k - 1 < (uint64_t) t->arr_size_) { - return t->arr_[k - 1].type_ != VAR_NIL; - } - if (t->spec_count > 0 && t->spec_keys && t->spec_vals) { - uint32_t i; - for (i = 0; i < t->spec_count; ++i) { - if (t->spec_keys[i].type_ == VAR_INT && t->spec_keys[i].data_.i == k && t->spec_vals[i].type_ != VAR_NIL) { - return true; - } - } - } - if (LIKELY(t->bucket_count_ == 0)) { - uint32_t i; - for (i = 0; i < t->count_; ++i) { - if (t->quick_data_[i].key.type_ == VAR_INT && t->quick_data_[i].key.data_.i == k && t->quick_data_[i].val.type_ != VAR_NIL) { - return true; - } - } - return false; - } else { - uint32_t h = (uint32_t) (k ^ (k >> 32)); - uint32_t mask = t->bucket_count_ - 1; - TableNode *curr = &t->nodes_[h & mask]; - while (1) { - if (curr->entry.key.type_ == VAR_INT && curr->entry.key.data_.i == k && curr->entry.val.type_ != VAR_NIL) { - return true; - } - if (curr->next == 0xFFFFFFFF) return false; - curr = &t->nodes_[curr->next]; - } - } -} - -/* 从 base 起向上数连续存在的整数键,返回前缀长度。*/ -static inline int64_t FlSeqScanFrom(VarTable *t, int64_t base) { - while (base < INT64_MAX && FlTableHasIntKey(t, base + 1)) { - base++; - } - return base; -} - -/* 带 spec 的表字段布局在构造期固定且规模很小,不参与缓存,每次直接重算。*/ -static inline bool FlSeqCacheable(VarTable *t) { - return t->spec == NULL && t->spec_count == 0; -} - -static inline int64_t FlGetTableSeqLen(VarTable *t) { - if (!t) return 0; - if (UNLIKELY(!FlSeqCacheable(t))) { - /* spec_count 是特化字段总数(含字符串键),不是数组前缀。从 0 起扫。 */ - return FlSeqScanFrom(t, 0); - } - if (LIKELY(t->seq_len_valid_ != 0)) { - return t->seq_len_; - } - t->seq_len_ = FlSeqScanFrom(t, 0); - t->seq_len_valid_ = 1; - return t->seq_len_; -} - -/* 在整数键写入完成后同步序列长度缓存。缓存无效时什么都不做,交由下一次 - FlGetTableSeqLen 懒重算——这保证漏挂钩的写入路径只会退化性能而不会算错长度。 - 本函数对同一次写入重复调用是幂等的。 */ -static inline void FlSeqNoteIntSet(VarTable *t, int64_t k, bool is_nil) { - if (!t || t->seq_len_valid_ == 0) return; - if (UNLIKELY(!FlSeqCacheable(t))) { - t->seq_len_valid_ = 0; - return; - } - if (is_nil) { - /* t[k] 变为 nil,前缀在 k-1 处截断;1..k-1 都还在,故新长度恰为 k-1。*/ - if (k >= 1 && k <= t->seq_len_) { - t->seq_len_ = k - 1; - } - return; - } - /* 只有正好补上 t[len+1] 才会延长前缀,随后继续吸收先前写入的更高键。*/ - if (t->seq_len_ < INT64_MAX && k == t->seq_len_ + 1) { - t->seq_len_ = FlSeqScanFrom(t, k); - } -} - -/* NativeObject Wrap 惰性快照:pairs/GET_TABLE_ENTRY 读 spec_count 前调用。非 Native 表 no-op。 */ -extern void FlEnsureNativeSpecSnapshot(VarTable *t); - -static inline void FlEnsureNativeSpecSnapshotInline(VarTable *t) { - if (UNLIKELY(t != NULL && t->spec != NULL)) { - FlEnsureNativeSpecSnapshot(t); - } -} - -#define TABLE_SIZE(t) ((t)->count_ + (t)->spec_count + (t)->arr_size_) - -typedef struct VarMulti { - uint32_t count; - CVar vars[0]; -} VarMulti; - -typedef struct FlHeapSlot { - char *curr; - char *end; -} FlHeapSlot; - -extern void *FakeluaAlloc(State *s, size_t size, bool is_const); - -static inline void *FlFastAlloc(State *state, size_t size, bool is_const) { - FlHeapSlot *hs = (is_const || FL_IN_CONST_INIT) ? (FlHeapSlot *) fl_const_heap : (FlHeapSlot *) fl_temp_heap; - if (LIKELY(hs != NULL)) { - size_t aligned_size = (size + 15) & ~(size_t) 15; - char *c = hs->curr; - if (LIKELY(c != NULL && aligned_size >= size && hs->end >= c && (size_t) (hs->end - c) >= aligned_size)) { - hs->curr = c + aligned_size; - return (void *) c; - } - } - return FakeluaAlloc(state, size, is_const); -} - -static inline CVar FlAllocMulti(State *state, uint32_t count) { - VarMulti *m = (VarMulti *) FlFastAlloc(state, sizeof(VarMulti) + count * sizeof(CVar), FL_IN_CONST_INIT); - m->count = count; - CVar res; - res.type_ = VAR_MULTI; - res.flag_ = 0; - res.data_.m = m; - return res; -} - -// 设置 multi-value 的元素,避免链接 inter::SetMultiCVarElement(Windows TCC 找不到符号) -static inline void FlSetMultiCVarElement(CVar multi, int idx, CVar val) { - VarMulti *m = multi.data_.m; - if (idx >= 0 && idx < (int) m->count) { - m->vars[idx] = val; - } -} - -static inline CVar FlMakeMulti(State *state, uint32_t count, ...) { - VarMulti *m = (VarMulti *) FlFastAlloc(state, sizeof(VarMulti) + count * sizeof(CVar), FL_IN_CONST_INIT); - m->count = count; - va_list args_list; - va_start(args_list, count); - for (uint32_t i = 0; i < count; ++i) { - m->vars[i] = va_arg(args_list, CVar); - } - va_end(args_list); - CVar res; - res.type_ = VAR_MULTI; - res.flag_ = 0; - res.data_.m = m; - return res; -} - -static inline CVar FlCombineMulti(State *state, uint32_t prefix_count, const CVar *prefix_vars, CVar last) { - uint32_t last_count = 1; - const CVar *last_vars = &last; - if (last.type_ == VAR_MULTI) { - VarMulti *m = last.data_.m; - last_count = m->count; - last_vars = m->vars; - } - - uint32_t total_count = prefix_count + last_count; - VarMulti *m = (VarMulti *) FlFastAlloc(state, sizeof(VarMulti) + total_count * sizeof(CVar), FL_IN_CONST_INIT); - m->count = total_count; - - for (uint32_t i = 0; i < prefix_count; ++i) { - if (prefix_vars[i].type_ == VAR_MULTI) { - m->vars[i] = prefix_vars[i].data_.m->count > 0 ? prefix_vars[i].data_.m->vars[0] : (CVar) {VAR_NIL}; - } else { - m->vars[i] = prefix_vars[i]; - } - } - for (uint32_t i = 0; i < last_count; ++i) { - m->vars[prefix_count + i] = last_vars[i]; - } - - CVar res; - res.type_ = VAR_MULTI; - res.flag_ = 0; - res.data_.m = m; - return res; -} - -static inline CVar FlUnboxMulti(CVar v, uint32_t idx) { - if (LIKELY(v.type_ != VAR_MULTI)) { - return (idx == 0) ? v : (CVar) {VAR_NIL}; - } - VarMulti *m = v.data_.m; - return (idx < m->count) ? m->vars[idx] : (CVar) {VAR_NIL}; -} - -static inline CVar FlSliceMulti(State *state, CVar v, uint32_t start_idx) { - if (v.type_ != VAR_MULTI) { - return (start_idx == 0) ? v : (CVar) {VAR_NIL}; - } - VarMulti *m = v.data_.m; - if (start_idx >= m->count) { - return (CVar) {VAR_NIL}; - } - uint32_t count = m->count - start_idx; - VarMulti *nm = (VarMulti *) FlFastAlloc(state, sizeof(VarMulti) + count * sizeof(CVar), FL_IN_CONST_INIT); - nm->count = count; - for (uint32_t i = 0; i < count; ++i) { - nm->vars[i] = m->vars[start_idx + i]; - } - CVar res; - res.type_ = VAR_MULTI; - res.flag_ = 0; - res.data_.m = nm; - return res; -} - -#define SET_NIL(v) \ - do { \ - (v).type_ = VAR_NIL; \ - } while (0) -#define SET_BOOL(v, val) \ - do { \ - (v).type_ = VAR_BOOL; \ - (v).data_.i = 0; \ - (v).data_.b = (val); \ - } while (0) -#define SET_INT(v, val) \ - do { \ - (v).type_ = VAR_INT; \ - (v).data_.i = (val); \ - } while (0) -#define SET_FLOAT(v, val) \ - do { \ - double __f = (val); \ - (v).type_ = VAR_FLOAT; \ - (v).data_.f = __f; \ - } while (0) - -/* 2^63:精确 double,也是第一个无法放入 int64 的整数。 - * (double)INT64_MAX 会向上舍入成这个值,因此不能用 <= (double)INT64_MAX。 */ -#define FL_INT64_FLOAT_EXCL 9223372036854775808.0 - -static inline int FlDoubleFitsInt64(double d, int64_t *out) { - double ip; - if (!isfinite(d)) return 0; - if (modf(d, &ip) != 0.0) return 0; - if (ip < (double) INT64_MIN || ip >= FL_INT64_FLOAT_EXCL) return 0; - *out = (int64_t) ip; - return 1; -} - -#define NORMALIZE_TABLE_KEY(key) \ - ({ \ - CVar __k = (key); \ - if (UNLIKELY(__k.type_ == VAR_FLOAT)) { \ - int64_t __i; \ - if (FlDoubleFitsInt64(__k.data_.f, &__i)) { \ - __k.type_ = VAR_INT; \ - __k.data_.i = __i; \ - } \ - } \ - __k; \ - }) - -#ifdef __cplusplus -extern "C" { -#endif -extern void *FakeluaAlloc(State *s, size_t size, bool is_const); -typedef struct FlCallCache { - uint64_t version; - void *target; - int expected_args; - uint8_t kind; - uint8_t is_vararg; -} FlCallCache; - -extern void FakeluaThrowError(State *s, const char *msg); -extern void FakeluaThrowSpec(State *s, const char *msg, const char *spec_name, const char *spec_file, int spec_line); -#ifndef FL_SPEC_NAME -#define FL_SPEC_NAME ((const char *)0) -#define FL_SPEC_FILE ((const char *)0) -#define FL_SPEC_LINE 0 -#endif -#ifndef FAKELUA_JIT_TYPE -#define FAKELUA_JIT_TYPE 0 -#endif -/* JIT_TCC == 0。TCC 代码页没有 .eh_frame,调用时写 Lua 帧。GCC 不写,抛错分支带上函数名。 */ -#if FAKELUA_JIT_TYPE == 0 -#define FL_SPEC_TRACK_FRAME 1 -#else -#define FL_SPEC_TRACK_FRAME 0 -#endif -#define FL_RAISE(s, msg) \ - do { \ - if (!FL_SPEC_TRACK_FRAME && (FL_SPEC_NAME) != (const char *)0) { \ - FakeluaThrowSpec((s), (msg), (FL_SPEC_NAME), (FL_SPEC_FILE), (FL_SPEC_LINE)); \ - } else { \ - FakeluaThrowError((s), (msg)); \ - } \ - } while (0) -// static inline 运行时辅助函数在 Section::Headers 先于函数体预处理,直接写 FL_SPEC_NAME 只能拿到空指针。 -// 开启优化(-O3)时经包装宏把调用点的函数名/文件/定义行传进内联体,内联后只在冷抛错分支具象化,热路径无额外指令; -// -O0 与 TCC 不做函数内联,不传额外实参,免掉每次调用的参数搬运。 -#if !FL_SPEC_TRACK_FRAME && defined(__OPTIMIZE__) -#define FL_HINT_PARAMS , const char *__fl_fn, const char *__fl_file, int __fl_line -#define FL_HINT_PARAMS_HEAD const char *__fl_fn, const char *__fl_file, int __fl_line, -#define FL_HINT_ARGS , (FL_SPEC_NAME), (FL_SPEC_FILE), (FL_SPEC_LINE) -#define FL_HINT_ARGS_HEAD (FL_SPEC_NAME), (FL_SPEC_FILE), (FL_SPEC_LINE), -#define FL_HINT_PASS , __fl_fn, __fl_file, __fl_line -#define FL_HINT_PASS_HEAD __fl_fn, __fl_file, __fl_line, -#define FL_RAISE_HINT(s, msg) \ - do { \ - if (__fl_fn != (const char *) 0) { \ - FakeluaThrowSpec((s), (msg), __fl_fn, __fl_file, __fl_line); \ - } else { \ - FakeluaThrowError((s), (msg)); \ - } \ - } while (0) -#else -#define FL_HINT_PARAMS -#define FL_HINT_PARAMS_HEAD -#define FL_HINT_ARGS -#define FL_HINT_ARGS_HEAD -#define FL_HINT_PASS -#define FL_HINT_PASS_HEAD -#define FL_RAISE_HINT(s, msg) FL_RAISE((s), (msg)) -#endif -// GCC 不在热路径写 Lua 帧,抛错时宿主按代码地址查这张表;dispatcher 仅标记数值特化函数的入口分发器。 -typedef struct FlLuaFnMap { - const void *addr; - const char *lua_name; - int line; - int dispatcher; -} FlLuaFnMap; -extern void FakeluaRegisterFnMap(State *s, const void *owner, const FlLuaFnMap *map, int count, const char *file); -extern CVar FakeluaCallByName(State *s, int jit_type, const char *name, int arg_num, ...); -extern CVar FakeluaCallByNameCached(State *s, int jit_type, const char *name, FlCallCache *cache, int arg_num, ...); -extern CVar FakeluaCallNativeCached(State *s, const void *native_entry_ptr, int arg_num, CVar *args); -extern CVar FakeluaAllocMultiCVar(State *s, int count); -extern void FakeluaSetMultiCVarElement(CVar *multi, int idx, CVar val); -extern CVar FlEvalLoadClosure(State *state, VarClosure *cl, int arg_num, const CVar *args); -extern CVar FakeluaInterpCall(State *state, VarClosure *cl, int arg_num, const CVar *args); -extern CVar FakeluaPromoteConstBoxValue(State *state, CVar val); -// 当前执行引擎上下文(供 JIT C 代码调用闭包前后维护):push 切到 jit 并返回上一个引擎, -// pop 恢复。native 对象方法等在闭包内执行时据此登记派回本引擎的异步回调。 -extern int FakeluaJitContextPush(State *s, int jit); -extern void FakeluaJitContextPop(State *s, int prev); -extern void FakeluaRegisterLuaLoc(State *s, int jit_type, FlLuaLoc *loc); -#ifdef __cplusplus -} -#endif - -// 记录当前执行的 Lua 语句位置:__fakelua_bind 将 fl_active_loc 指向 State 上的活动槽位; -// fl_lua_loc 作为兜底与 RegisterLuaLoc 测试观测点保留。 -static FlLuaLoc fl_lua_loc = {0, 0}; -static FlLuaLoc *fl_active_loc = &fl_lua_loc; -static const uint64_t fl_vm_version_zero = 0; -static const uint64_t *fl_vm_version = &fl_vm_version_zero; -// pcall 用 longjmp 跳出时会跳过 FL_LUA_POP,深度由宿主恢复,指针仍可能指着内层帧。 -// 按恢复后的深度定位栈顶槽(超出上限统一落在溢出顶槽 FL_LUA_STACK_MAX),避免写回已出栈的内层帧。 -#define FL_LOC_SET(f, l) \ - do { \ - if (fl_lua_frames && fl_lua_depth && *fl_lua_depth > 0) { \ - int __fl_top = *fl_lua_depth - 1; \ - if (__fl_top >= FL_LUA_STACK_MAX) __fl_top = FL_LUA_STACK_MAX; \ - fl_active_loc = (FlLuaLoc *) &fl_lua_frames[__fl_top]; \ - } \ - fl_active_loc->file = (f); \ - fl_active_loc->line = (l); \ - } while (0) - -// 与 state.h 的 LuaFrame 同布局。file/line 在最前,FL_LOC_SET 写栈顶帧时不会覆盖函数名。 -// 0..FL_LUA_STACK_MAX-1 固定保存浅层帧,不回绕覆盖;FL_LUA_STACK_MAX 保存超深递归时的当前最内层帧。 -typedef struct FlLuaFrame { - const char *file; - int line; - const char *func; -} FlLuaFrame; -#define FL_LUA_STACK_MAX 256 -static FlLuaFrame *fl_lua_frames = 0; -static int *fl_lua_depth = 0; -static FlLuaLoc *fl_active_base = &fl_lua_loc; - -static inline void FlLuaPush(const char *func, int line, FlLuaFrame *saved) { - if (!fl_lua_frames || !fl_lua_depth) return; - int d = *fl_lua_depth; - if (d < 0) d = 0; - int idx = d < FL_LUA_STACK_MAX ? d : FL_LUA_STACK_MAX; - FlLuaFrame *fr = &fl_lua_frames[idx]; - if (UNLIKELY(d >= FL_LUA_STACK_MAX) && saved) { - *saved = *fr; - } - fr->file = fl_lua_file; - fr->line = line; - fr->func = func; - fl_active_loc = (FlLuaLoc *) fr; - *fl_lua_depth = d + 1; -} - -static inline void FlLuaPop(const FlLuaFrame *saved) { - if (!fl_lua_depth || *fl_lua_depth <= 0) return; - int d = *fl_lua_depth - 1; - *fl_lua_depth = d; - if (UNLIKELY(d >= FL_LUA_STACK_MAX) && saved && fl_lua_frames) { - fl_lua_frames[FL_LUA_STACK_MAX] = *saved; - } - if (d > 0 && fl_lua_frames) { - int idx = (d - 1) < FL_LUA_STACK_MAX ? (d - 1) : FL_LUA_STACK_MAX; - fl_active_loc = (FlLuaLoc *) &fl_lua_frames[idx]; - } else if (fl_active_base) { - fl_active_loc = fl_active_base; - } -} - -#define FL_LUA_PUSH(func, line, saved) FlLuaPush((func), (line), (saved)) -#define FL_LUA_POP(saved) FlLuaPop((saved)) - -#define kMaxFunctionInputParams 32 - -#define FL_CONST_BOX_FLAG 0x2 - -static inline CVar *FlAllocUpvalueBox(State *state, bool is_const) { - CVar *box = (CVar *) FlFastAlloc(state, sizeof(CVar), is_const); - box->type_ = VAR_NIL; - box->flag_ = is_const ? FL_CONST_BOX_FLAG : 0; - box->data_.i = 0; - return box; -} - -static inline CVar FlLoadUpvalueBox(const CVar *box) { - CVar v = *box; - v.flag_ &= ~FL_CONST_BOX_FLAG; - return v; -} - -static inline void FlStoreUpvalueBox(State *state, CVar *box, CVar v) { - if (UNLIKELY((box->flag_ & FL_CONST_BOX_FLAG) != 0)) { - if (!FL_IN_CONST_INIT) { - v = FakeluaPromoteConstBoxValue(state, v); - } - v.flag_ |= FL_CONST_BOX_FLAG; - } - *box = v; -} - -// 注意:is_vararg 必须声明为 int 而非 bool。C17 7.16.1.4 规定 va_start 的最后一个 -// 具名形参若类型会受默认实参提升影响(bool/_Bool 会被提升为 int),行为未定义。 -// 调用处传入的 true/false 会隐式转换为 int。 -static inline CVar FlMakeClosure(State *state, void *func_ptr, int upvalue_count, int expected_arg_count, int is_vararg, ...) { - VarClosure *cl = (VarClosure *) FlFastAlloc(state, sizeof(VarClosure) + upvalue_count * sizeof(CVar *), FL_IN_CONST_INIT); - cl->func_ptr = func_ptr; - cl->upvalue_count = upvalue_count; - cl->expected_arg_count = expected_arg_count; - cl->is_vararg = is_vararg; - cl->code_str = (FAKELUA_JIT_TYPE == 1) ? (const char *) 2 : NULL; - va_list args_list; - va_start(args_list, is_vararg); - for (int i = 0; i < upvalue_count; ++i) { - cl->upvalues[i] = va_arg(args_list, CVar *); - } - va_end(args_list); - CVar res; - res.type_ = VAR_CLOSURE; - res.flag_ = 0; - res.data_.cl = cl; - return res; -} - -static inline CVar FlMakeClosure0(State *state, void *func_ptr, int expected_arg_count, int is_vararg) { - VarClosure *cl = (VarClosure *) FlFastAlloc(state, sizeof(VarClosure), FL_IN_CONST_INIT); - cl->func_ptr = func_ptr; - cl->upvalue_count = 0; - cl->expected_arg_count = expected_arg_count; - cl->is_vararg = is_vararg; - cl->code_str = (FAKELUA_JIT_TYPE == 1) ? (const char *) 2 : NULL; - CVar res; - res.type_ = VAR_CLOSURE; - res.flag_ = 0; - res.data_.cl = cl; - return res; -} - -static inline CVar FlMakeClosure1(State *state, void *func_ptr, int expected_arg_count, int is_vararg, CVar *up0) { - VarClosure *cl = (VarClosure *) FlFastAlloc(state, sizeof(VarClosure) + sizeof(CVar *), FL_IN_CONST_INIT); - cl->func_ptr = func_ptr; - cl->upvalue_count = 1; - cl->expected_arg_count = expected_arg_count; - cl->is_vararg = is_vararg; - cl->code_str = (FAKELUA_JIT_TYPE == 1) ? (const char *) 2 : NULL; - cl->upvalues[0] = up0; - CVar res; - res.type_ = VAR_CLOSURE; - res.flag_ = 0; - res.data_.cl = cl; - return res; -} - -static inline CVar FlCallClosureFn(FL_HINT_PARAMS_HEAD State *state, CVar cl_var, int arg_num, ...) { - if (UNLIKELY(cl_var.type_ != VAR_CLOSURE)) { - FL_RAISE_HINT(state, "attempt to call a non-function value"); - } - // 与 FakeluaCallByName 一致:先拦超限,再读 varargs。 - // raw_arg_arr 只有 32 槽,arg_num>32 会栈缓冲溢出。 - if (UNLIKELY(arg_num < 0 || arg_num > (int) kMaxFunctionInputParams)) { - FL_RAISE_HINT(state, "too many arguments"); - } - - CVar raw_arg_arr[kMaxFunctionInputParams]; - va_list args_list; - va_start(args_list, arg_num); - for (int i = 0; i < arg_num; ++i) { - raw_arg_arr[i] = va_arg(args_list, CVar); - } - va_end(args_list); - - VarClosure *cl = cl_var.data_.cl; - void *addr = cl->func_ptr; - if (UNLIKELY(addr == NULL)) { - return FlEvalLoadClosure(state, cl, arg_num, arg_num > 0 ? raw_arg_arr : NULL); - } - const bool is_vararg = cl->is_vararg; - const int expected_arg_count = cl->expected_arg_count; - const int fixed_arg_count = is_vararg ? (expected_arg_count > 0 ? expected_arg_count - 1 : 0) : expected_arg_count; - // 形参个数也要限:下面按 expected_arg_count 往 temp_arg_arr(定长 32)里写, - // 声明了超过上限的形参会栈缓冲溢出(与 vm.cpp 的 CallByNameImpl 一致)。 - if (UNLIKELY(expected_arg_count < 0 || expected_arg_count > (int) kMaxFunctionInputParams)) { - FL_RAISE_HINT(state, "too many arguments"); - } - - const CVar *arg_arr = NULL; - const bool last_is_multi = (arg_num > 0 && raw_arg_arr[arg_num - 1].type_ == VAR_MULTI); - CVar temp_arg_arr[kMaxFunctionInputParams]; - if (LIKELY(!is_vararg && arg_num == expected_arg_count && !last_is_multi)) { - arg_arr = raw_arg_arr; - } else { - CVar flat_args_buf[kMaxFunctionInputParams]; - int flat_count = 0; - for (int i = 0; i < arg_num; ++i) { - if (i == arg_num - 1 && raw_arg_arr[i].type_ == VAR_MULTI) { - VarMulti *m = raw_arg_arr[i].data_.m; - const int extra = m ? (int) m->count : 0; - if (flat_count + extra > (int) kMaxFunctionInputParams) { - FL_RAISE_HINT(state, "too many arguments"); - } - if (m) { - for (uint32_t j = 0; j < m->count; ++j) { - flat_args_buf[flat_count++] = m->vars[j]; - } - } - } else if (raw_arg_arr[i].type_ == VAR_MULTI) { - VarMulti *m = raw_arg_arr[i].data_.m; - if (flat_count >= (int) kMaxFunctionInputParams) { - FL_RAISE_HINT(state, "too many arguments"); - } - flat_args_buf[flat_count++] = (m && m->count > 0) ? m->vars[0] : (CVar) {VAR_NIL}; - } else { - if (flat_count >= (int) kMaxFunctionInputParams) { - FL_RAISE_HINT(state, "too many arguments"); - } - flat_args_buf[flat_count++] = raw_arg_arr[i]; - } - } - - if (UNLIKELY(is_vararg)) { - for (int i = 0; i < fixed_arg_count; ++i) { - temp_arg_arr[i] = i < flat_count ? flat_args_buf[i] : (CVar) {VAR_NIL}; - } - const int vararg_count = flat_count - fixed_arg_count; - VarMulti *m = (VarMulti *) FlFastAlloc(state, sizeof(VarMulti) + (vararg_count > 0 ? vararg_count : 0) * sizeof(CVar), FL_IN_CONST_INIT); - m->count = vararg_count > 0 ? vararg_count : 0; - for (int i = 0; i < vararg_count; ++i) { - m->vars[i] = flat_args_buf[fixed_arg_count + i]; - } - CVar vararg_cvar; - vararg_cvar.type_ = VAR_MULTI; - vararg_cvar.flag_ = 0; - vararg_cvar.data_.m = m; - temp_arg_arr[fixed_arg_count] = vararg_cvar; - } else { - for (int i = 0; i < expected_arg_count; ++i) { - temp_arg_arr[i] = i < flat_count ? flat_args_buf[i] : (CVar) {VAR_NIL}; - } - } - arg_arr = temp_arg_arr; - } - - if (cl->code_str == (const char *) 1) { - int npass = expected_arg_count; - if (is_vararg) { - npass = fixed_arg_count + 1; - if (npass < 1) npass = 1; - } - return FakeluaInterpCall(state, cl, npass, arg_arr); - } - -#define FCCVAR_0 -#define FCCVAR_1 , CVar -#define FCCVAR_2 FCCVAR_1, CVar -#define FCCVAR_3 FCCVAR_2, CVar -#define FCCVAR_4 FCCVAR_3, CVar -#define FCCVAR_5 FCCVAR_4, CVar -#define FCCVAR_6 FCCVAR_5, CVar -#define FCCVAR_7 FCCVAR_6, CVar -#define FCCVAR_8 FCCVAR_7, CVar -#define FCCVAR_9 FCCVAR_8, CVar -#define FCCVAR_10 FCCVAR_9, CVar -#define FCCVAR_11 FCCVAR_10, CVar -#define FCCVAR_12 FCCVAR_11, CVar -#define FCCVAR_13 FCCVAR_12, CVar -#define FCCVAR_14 FCCVAR_13, CVar -#define FCCVAR_15 FCCVAR_14, CVar -#define FCCVAR_16 FCCVAR_15, CVar -#define FCCVAR_17 FCCVAR_16, CVar -#define FCCVAR_18 FCCVAR_17, CVar -#define FCCVAR_19 FCCVAR_18, CVar -#define FCCVAR_20 FCCVAR_19, CVar -#define FCCVAR_21 FCCVAR_20, CVar -#define FCCVAR_22 FCCVAR_21, CVar -#define FCCVAR_23 FCCVAR_22, CVar -#define FCCVAR_24 FCCVAR_23, CVar -#define FCCVAR_25 FCCVAR_24, CVar -#define FCCVAR_26 FCCVAR_25, CVar -#define FCCVAR_27 FCCVAR_26, CVar -#define FCCVAR_28 FCCVAR_27, CVar -#define FCCVAR_29 FCCVAR_28, CVar -#define FCCVAR_30 FCCVAR_29, CVar -#define FCCVAR_31 FCCVAR_30, CVar -#define FCCVAR_32 FCCVAR_31, CVar - -#define FCARG_0 -#define FCARG_1 , arg_arr[0] -#define FCARG_2 FCARG_1, arg_arr[1] -#define FCARG_3 FCARG_2, arg_arr[2] -#define FCARG_4 FCARG_3, arg_arr[3] -#define FCARG_5 FCARG_4, arg_arr[4] -#define FCARG_6 FCARG_5, arg_arr[5] -#define FCARG_7 FCARG_6, arg_arr[6] -#define FCARG_8 FCARG_7, arg_arr[7] -#define FCARG_9 FCARG_8, arg_arr[8] -#define FCARG_10 FCARG_9, arg_arr[9] -#define FCARG_11 FCARG_10, arg_arr[10] -#define FCARG_12 FCARG_11, arg_arr[11] -#define FCARG_13 FCARG_12, arg_arr[12] -#define FCARG_14 FCARG_13, arg_arr[13] -#define FCARG_15 FCARG_14, arg_arr[14] -#define FCARG_16 FCARG_15, arg_arr[15] -#define FCARG_17 FCARG_16, arg_arr[16] -#define FCARG_18 FCARG_17, arg_arr[17] -#define FCARG_19 FCARG_18, arg_arr[18] -#define FCARG_20 FCARG_19, arg_arr[19] -#define FCARG_21 FCARG_20, arg_arr[20] -#define FCARG_22 FCARG_21, arg_arr[21] -#define FCARG_23 FCARG_22, arg_arr[22] -#define FCARG_24 FCARG_23, arg_arr[23] -#define FCARG_25 FCARG_24, arg_arr[24] -#define FCARG_26 FCARG_25, arg_arr[25] -#define FCARG_27 FCARG_26, arg_arr[26] -#define FCARG_28 FCARG_27, arg_arr[27] -#define FCARG_29 FCARG_28, arg_arr[28] -#define FCARG_30 FCARG_29, arg_arr[29] -#define FCARG_31 FCARG_30, arg_arr[30] -#define FCARG_32 FCARG_31, arg_arr[31] - -#define FCCASE(N) \ - case N: { \ - FlLuaLoc __fl_saved_loc = *fl_active_loc; \ - int __fl_prev_jit = FakeluaJitContextPush(state, (cl->code_str == (const char *) 2 ? 1 : 0)); \ - CVar __fl_ret = ((CVar (*)(VarClosure * FCCVAR_##N))(addr))(cl FCARG_##N); \ - FakeluaJitContextPop(state, __fl_prev_jit); \ - *fl_active_loc = __fl_saved_loc; \ - return __fl_ret; \ - } - - switch (expected_arg_count) { - FCCASE(0) - FCCASE(1) - FCCASE(2) - FCCASE(3) - FCCASE(4) - FCCASE(5) - FCCASE(6) - FCCASE(7) - FCCASE(8) - FCCASE(9) - FCCASE(10) - FCCASE(11) - FCCASE(12) - FCCASE(13) - FCCASE(14) - FCCASE(15) - FCCASE(16) - FCCASE(17) - FCCASE(18) - FCCASE(19) - FCCASE(20) - FCCASE(21) - FCCASE(22) - FCCASE(23) - FCCASE(24) FCCASE(25) FCCASE(26) FCCASE(27) FCCASE(28) FCCASE(29) FCCASE(30) FCCASE(31) FCCASE(32) default : FL_RAISE_HINT(state, "FlCallClosure: expected_arg_count out of range"); - return (CVar) {VAR_NIL}; - } - -#undef FCCASE -#undef FCCVAR_0 -#undef FCCVAR_1 -#undef FCCVAR_2 -#undef FCCVAR_3 -#undef FCCVAR_4 -#undef FCCVAR_5 -#undef FCCVAR_6 -#undef FCCVAR_7 -#undef FCCVAR_8 -#undef FCCVAR_9 -#undef FCCVAR_10 -#undef FCCVAR_11 -#undef FCCVAR_12 -#undef FCCVAR_13 -#undef FCCVAR_14 -#undef FCCVAR_15 -#undef FCCVAR_16 -#undef FCCVAR_17 -#undef FCCVAR_18 -#undef FCCVAR_19 -#undef FCCVAR_20 -#undef FCCVAR_21 -#undef FCCVAR_22 -#undef FCCVAR_23 -#undef FCCVAR_24 -#undef FCCVAR_25 -#undef FCCVAR_26 -#undef FCCVAR_27 -#undef FCCVAR_28 -#undef FCCVAR_29 -#undef FCCVAR_30 -#undef FCCVAR_31 -#undef FCCVAR_32 -#undef FCARG_0 -#undef FCARG_1 -#undef FCARG_2 -#undef FCARG_3 -#undef FCARG_4 -#undef FCARG_5 -#undef FCARG_6 -#undef FCARG_7 -#undef FCARG_8 -#undef FCARG_9 -#undef FCARG_10 -#undef FCARG_11 -#undef FCARG_12 -#undef FCARG_13 -#undef FCARG_14 -#undef FCARG_15 -#undef FCARG_16 -#undef FCARG_17 -#undef FCARG_18 -#undef FCARG_19 -#undef FCARG_20 -#undef FCARG_21 -#undef FCARG_22 -#undef FCARG_23 -#undef FCARG_24 -#undef FCARG_25 -#undef FCARG_26 -#undef FCARG_27 -#undef FCARG_28 -#undef FCARG_29 -#undef FCARG_30 -#undef FCARG_31 -#undef FCARG_32 -} -#define FlCallClosure(...) FlCallClosureFn(FL_HINT_ARGS_HEAD __VA_ARGS__) - -static inline CVar FlCallClosure0Fn(State *state, CVar cl_var FL_HINT_PARAMS) { - if (LIKELY(cl_var.type_ == VAR_CLOSURE)) { - VarClosure *cl = cl_var.data_.cl; - void *addr = cl->func_ptr; - if (LIKELY(addr != NULL && !cl->is_vararg && cl->expected_arg_count == 0 && cl->code_str == ((FAKELUA_JIT_TYPE == 1) ? (const char *) 2 : (const char *) 0))) { - FlLuaLoc __fl_saved_loc = *fl_active_loc; - CVar __fl_ret = ((CVar (*)(VarClosure *))(addr))(cl); - *fl_active_loc = __fl_saved_loc; - return __fl_ret; - } - } - return FlCallClosureFn(FL_HINT_PASS_HEAD state, cl_var, 0); -} -#define FlCallClosure0(...) FlCallClosure0Fn(__VA_ARGS__ FL_HINT_ARGS) - -static inline CVar FlCallClosure1Fn(State *state, CVar cl_var, CVar a0 FL_HINT_PARAMS) { - if (LIKELY(cl_var.type_ == VAR_CLOSURE)) { - VarClosure *cl = cl_var.data_.cl; - void *addr = cl->func_ptr; - if (LIKELY(addr != NULL && !cl->is_vararg && cl->expected_arg_count == 1 && a0.type_ != VAR_MULTI && cl->code_str == ((FAKELUA_JIT_TYPE == 1) ? (const char *) 2 : (const char *) 0))) { - FlLuaLoc __fl_saved_loc = *fl_active_loc; - CVar __fl_ret = ((CVar (*)(VarClosure *, CVar))(addr))(cl, a0); - *fl_active_loc = __fl_saved_loc; - return __fl_ret; - } - } - return FlCallClosureFn(FL_HINT_PASS_HEAD state, cl_var, 1, a0); -} -#define FlCallClosure1(...) FlCallClosure1Fn(__VA_ARGS__ FL_HINT_ARGS) - -static inline CVar FlCallClosure2Fn(State *state, CVar cl_var, CVar a0, CVar a1 FL_HINT_PARAMS) { - if (LIKELY(cl_var.type_ == VAR_CLOSURE)) { - VarClosure *cl = cl_var.data_.cl; - void *addr = cl->func_ptr; - if (LIKELY(addr != NULL && !cl->is_vararg && cl->expected_arg_count == 2 && a0.type_ != VAR_MULTI && a1.type_ != VAR_MULTI && cl->code_str == ((FAKELUA_JIT_TYPE == 1) ? (const char *) 2 : (const char *) 0))) { - FlLuaLoc __fl_saved_loc = *fl_active_loc; - CVar __fl_ret = ((CVar (*)(VarClosure *, CVar, CVar))(addr))(cl, a0, a1); - *fl_active_loc = __fl_saved_loc; - return __fl_ret; - } - } - return FlCallClosureFn(FL_HINT_PASS_HEAD state, cl_var, 2, a0, a1); -} -#define FlCallClosure2(...) FlCallClosure2Fn(__VA_ARGS__ FL_HINT_ARGS) - -static inline CVar FlCallClosure3Fn(State *state, CVar cl_var, CVar a0, CVar a1, CVar a2 FL_HINT_PARAMS) { - if (LIKELY(cl_var.type_ == VAR_CLOSURE)) { - VarClosure *cl = cl_var.data_.cl; - void *addr = cl->func_ptr; - if (LIKELY(addr != NULL && !cl->is_vararg && cl->expected_arg_count == 3 && a0.type_ != VAR_MULTI && a1.type_ != VAR_MULTI && a2.type_ != VAR_MULTI && cl->code_str == ((FAKELUA_JIT_TYPE == 1) ? (const char *) 2 : (const char *) 0))) { - FlLuaLoc __fl_saved_loc = *fl_active_loc; - CVar __fl_ret = ((CVar (*)(VarClosure *, CVar, CVar, CVar))(addr))(cl, a0, a1, a2); - *fl_active_loc = __fl_saved_loc; - return __fl_ret; - } - } - return FlCallClosureFn(FL_HINT_PASS_HEAD state, cl_var, 3, a0, a1, a2); -} -#define FlCallClosure3(...) FlCallClosure3Fn(__VA_ARGS__ FL_HINT_ARGS) - -static inline CVar FlCallNamed0(State *state, const char *name, FlCallCache *cache) { - if (FAKELUA_JIT_TYPE == 1) { - if (LIKELY(cache->version == *fl_vm_version && !cache->is_vararg && cache->expected_args == 0)) { - if (cache->kind == 1) { - FlLuaLoc __fl_saved_loc = *fl_active_loc; - CVar __fl_ret = ((CVar (*)(VarClosure *))(cache->target))(NULL); - *fl_active_loc = __fl_saved_loc; - return __fl_ret; - } - if (cache->kind == 2) { - return FakeluaCallNativeCached(state, cache->target, 0, NULL); - } - } - } - return FakeluaCallByNameCached(state, FAKELUA_JIT_TYPE, name, cache, 0); -} - -#define FL_DEF_CALL_NAMED(N, PARAMS, ARGS, CHK_MULTI, CTYPES) \ - static inline CVar FlCallNamed##N(State *state, const char *name, FlCallCache *cache, PARAMS) { \ - if (FAKELUA_JIT_TYPE == 1) { \ - if (LIKELY(cache->version == *fl_vm_version && !cache->is_vararg && cache->expected_args == (N) && (CHK_MULTI))) { \ - if (cache->kind == 1) { \ - FlLuaLoc __fl_saved_loc = *fl_active_loc; \ - CVar __fl_ret = ((CVar (*)(VarClosure *, CTYPES))(cache->target))(NULL, ARGS); \ - *fl_active_loc = __fl_saved_loc; \ - return __fl_ret; \ - } \ - if (cache->kind == 2) { \ - CVar __fl_args[N] = {ARGS}; \ - return FakeluaCallNativeCached(state, cache->target, N, __fl_args); \ - } \ - } \ - } \ - return FakeluaCallByNameCached(state, FAKELUA_JIT_TYPE, name, cache, N, ARGS); \ - } - -#define FL_COMMA , -#define FL_P1 CVar a0 -#define FL_P2 FL_P1 FL_COMMA CVar a1 -#define FL_P3 FL_P2 FL_COMMA CVar a2 -#define FL_P4 FL_P3 FL_COMMA CVar a3 -#define FL_P5 FL_P4 FL_COMMA CVar a4 -#define FL_P6 FL_P5 FL_COMMA CVar a5 -#define FL_P7 FL_P6 FL_COMMA CVar a6 -#define FL_P8 FL_P7 FL_COMMA CVar a7 -#define FL_P9 FL_P8 FL_COMMA CVar a8 -#define FL_P10 FL_P9 FL_COMMA CVar a9 -#define FL_P11 FL_P10 FL_COMMA CVar a10 -#define FL_P12 FL_P11 FL_COMMA CVar a11 -#define FL_A1 a0 -#define FL_A2 FL_A1 FL_COMMA a1 -#define FL_A3 FL_A2 FL_COMMA a2 -#define FL_A4 FL_A3 FL_COMMA a3 -#define FL_A5 FL_A4 FL_COMMA a4 -#define FL_A6 FL_A5 FL_COMMA a5 -#define FL_A7 FL_A6 FL_COMMA a6 -#define FL_A8 FL_A7 FL_COMMA a7 -#define FL_A9 FL_A8 FL_COMMA a8 -#define FL_A10 FL_A9 FL_COMMA a9 -#define FL_A11 FL_A10 FL_COMMA a10 -#define FL_A12 FL_A11 FL_COMMA a11 -#define FL_M1 (a0.type_ != VAR_MULTI) -#define FL_M2 FL_M1 && (a1.type_ != VAR_MULTI) -#define FL_M3 FL_M2 && (a2.type_ != VAR_MULTI) -#define FL_M4 FL_M3 && (a3.type_ != VAR_MULTI) -#define FL_M5 FL_M4 && (a4.type_ != VAR_MULTI) -#define FL_M6 FL_M5 && (a5.type_ != VAR_MULTI) -#define FL_M7 FL_M6 && (a6.type_ != VAR_MULTI) -#define FL_M8 FL_M7 && (a7.type_ != VAR_MULTI) -#define FL_M9 FL_M8 && (a8.type_ != VAR_MULTI) -#define FL_M10 FL_M9 && (a9.type_ != VAR_MULTI) -#define FL_M11 FL_M10 && (a10.type_ != VAR_MULTI) -#define FL_M12 FL_M11 && (a11.type_ != VAR_MULTI) -#define FL_T1 CVar -#define FL_T2 FL_T1 FL_COMMA CVar -#define FL_T3 FL_T2 FL_COMMA CVar -#define FL_T4 FL_T3 FL_COMMA CVar -#define FL_T5 FL_T4 FL_COMMA CVar -#define FL_T6 FL_T5 FL_COMMA CVar -#define FL_T7 FL_T6 FL_COMMA CVar -#define FL_T8 FL_T7 FL_COMMA CVar -#define FL_T9 FL_T8 FL_COMMA CVar -#define FL_T10 FL_T9 FL_COMMA CVar -#define FL_T11 FL_T10 FL_COMMA CVar -#define FL_T12 FL_T11 FL_COMMA CVar - -FL_DEF_CALL_NAMED(1, FL_P1, FL_A1, FL_M1, FL_T1) -FL_DEF_CALL_NAMED(2, FL_P2, FL_A2, FL_M2, FL_T2) -FL_DEF_CALL_NAMED(3, FL_P3, FL_A3, FL_M3, FL_T3) -FL_DEF_CALL_NAMED(4, FL_P4, FL_A4, FL_M4, FL_T4) -FL_DEF_CALL_NAMED(5, FL_P5, FL_A5, FL_M5, FL_T5) -FL_DEF_CALL_NAMED(6, FL_P6, FL_A6, FL_M6, FL_T6) -FL_DEF_CALL_NAMED(7, FL_P7, FL_A7, FL_M7, FL_T7) -FL_DEF_CALL_NAMED(8, FL_P8, FL_A8, FL_M8, FL_T8) -FL_DEF_CALL_NAMED(9, FL_P9, FL_A9, FL_M9, FL_T9) -FL_DEF_CALL_NAMED(10, FL_P10, FL_A10, FL_M10, FL_T10) -FL_DEF_CALL_NAMED(11, FL_P11, FL_A11, FL_M11, FL_T11) -FL_DEF_CALL_NAMED(12, FL_P12, FL_A12, FL_M12, FL_T12) - -#undef FL_DEF_CALL_NAMED -#undef FL_COMMA -#undef FL_P1 -#undef FL_P2 -#undef FL_P3 -#undef FL_P4 -#undef FL_P5 -#undef FL_P6 -#undef FL_P7 -#undef FL_P8 -#undef FL_P9 -#undef FL_P10 -#undef FL_P11 -#undef FL_P12 -#undef FL_A1 -#undef FL_A2 -#undef FL_A3 -#undef FL_A4 -#undef FL_A5 -#undef FL_A6 -#undef FL_A7 -#undef FL_A8 -#undef FL_A9 -#undef FL_A10 -#undef FL_A11 -#undef FL_A12 -#undef FL_M1 -#undef FL_M2 -#undef FL_M3 -#undef FL_M4 -#undef FL_M5 -#undef FL_M6 -#undef FL_M7 -#undef FL_M8 -#undef FL_M9 -#undef FL_M10 -#undef FL_M11 -#undef FL_M12 -#undef FL_T1 -#undef FL_T2 -#undef FL_T3 -#undef FL_T4 -#undef FL_T5 -#undef FL_T6 -#undef FL_T7 -#undef FL_T8 -#undef FL_T9 -#undef FL_T10 -#undef FL_T11 -#undef FL_T12 - -static inline bool FlIsTrue(CVar v) { - return LIKELY(v.type_ != VAR_NIL) && (v.type_ != VAR_BOOL || v.data_.b); -} - - -static inline uint32_t FlHashString(const char *str, int len) { - uint32_t hash = 5381; - int i; - for (i = 0; i < len; ++i) { - hash = ((hash << 5) + hash) + (uint8_t) str[i]; - } - if (hash == 0) { - hash = 1; - } - return hash; -} - -#define SET_STRING(v, str, len) \ - do { \ - int __len = (len); \ - const char *__s_ptr = (str); \ - VarString *__vs = (VarString *) FlFastAlloc(_S, sizeof(VarString) + __len, FL_IN_CONST_INIT); \ - __vs->size_ = __len; \ - __vs->hash_ = 0; \ - memcpy(__vs->data_, __s_ptr, __len); \ - (v).type_ = VAR_STRING; \ - (v).data_.s = __vs; \ - } while (0) - -#define SET_TABLE(v) \ - do { \ - VarTable *__t = (VarTable *) FlFastAlloc(_S, sizeof(VarTable), FL_IN_CONST_INIT); \ - __t->count_ = 0; \ - __t->bucket_count_ = 0; \ - __t->nodes_ = NULL; \ - __t->active_list_ = NULL; \ - __t->free_list_idx_ = 0xFFFFFFFF; \ - __t->spec = NULL; \ - __t->spec_get = NULL; \ - __t->spec_set = NULL; \ - __t->spec_keys = NULL; \ - __t->spec_vals = NULL; \ - __t->spec_count = 0; \ - __t->seq_len_valid_ = 1; \ - __t->seq_len_ = 0; \ - __t->arr_ = NULL; \ - __t->arr_size_ = 0; \ - __t->arr_cap_ = 0; \ - assert(sizeof(__t->quick_data_) == 8 * sizeof(VarEntry)); \ - { \ - int __i; \ - for (__i = 0; __i < 8; ++__i) { \ - __t->quick_data_[__i].key.type_ = VAR_NIL; \ - __t->quick_data_[__i].val.type_ = VAR_NIL; \ - __t->quick_data_[__i].hash = 0; \ - } \ - } \ - (v).type_ = VAR_TABLE; \ - /* init 中创建 → CONST_FLAG(嵌套表同样走 SET_TABLE,整树只读);init 后创建 → 可变 */ \ - (v).flag_ = FL_IN_CONST_INIT ? CONST_FLAG : 0; \ - (v).data_.t = __t; \ - } while (0) - -#define SET_TABLE_SPEC(v, SpecType, get_fn, set_fn, field_count) \ - do { \ - SET_TABLE(v); \ - SpecType *__sp = (SpecType *) FlFastAlloc(_S, sizeof(SpecType), FL_IN_CONST_INIT); \ - (v).data_.t->spec = __sp; \ - (v).data_.t->spec_get = (get_fn); \ - (v).data_.t->spec_set = (set_fn); \ - (v).data_.t->spec_keys = (CVar *) FlFastAlloc(_S, sizeof(CVar) * (field_count), FL_IN_CONST_INIT); \ - (v).data_.t->spec_vals = (CVar *) FlFastAlloc(_S, sizeof(CVar) * (field_count), FL_IN_CONST_INIT); \ - (v).data_.t->spec_count = (field_count); \ - assert(sizeof(SpecType) == (field_count) * sizeof(CVar)); \ - } while (0) - -#define FL_SPEC(SpecType, v, field) (((SpecType *) (v).data_.t->spec)->field) -#define FL_SET_SPEC(SpecType, v, field, index, val) \ - do { \ - CVar __flsv = (val); \ - FL_CHECK_CONST_TABLE(v); \ - FL_SPEC(SpecType, (v), field) = __flsv; \ - (v).data_.t->spec_vals[(index)] = __flsv; \ - } while (0) - -#define IsTrue(v, result) \ - do { \ - CVar __tv = (v); \ - (result) = (LIKELY(__tv.type_ != VAR_NIL) && (__tv.type_ != VAR_BOOL || __tv.data_.b)); \ - } while (0) - -static inline int FlCompareIntDouble(int64_t integer, double floating) { - if (isnan(floating)) return 0; - if (floating >= 9223372036854775808.0) return -1; - if (floating < -9223372036854775808.0) return 1; - int64_t truncated = (int64_t) floating; - if (integer < truncated) return -1; - if (integer > truncated) return 1; - double truncated_as_double = (double) truncated; - if (floating > truncated_as_double) return -1; - if (floating < truncated_as_double) return 1; - return 0; -} - -#define FL_INT_FLOAT_EQUAL(i, f) (!isnan((f)) && FlCompareIntDouble((i), (f)) == 0) - -#define VarEqual(_fa, _fb, result) \ - do { \ - CVar __a = (_fa), __b = (_fb); \ - if (__a.type_ != __b.type_) { \ - if ((__a.type_ == VAR_STRING || __a.type_ == VAR_STRINGID) && (__b.type_ == VAR_STRING || __b.type_ == VAR_STRINGID)) { \ - VarString *__sa = (__a.type_ == VAR_STRING) ? __a.data_.s : (VarString *) __a.data_.i; \ - VarString *__sb = (__b.type_ == VAR_STRING) ? __b.data_.s : (VarString *) __b.data_.i; \ - (result) = (__sa == __sb) || (__sa->size_ == __sb->size_ && memcmp(__sa->data_, __sb->data_, __sa->size_) == 0); \ - } else if (__a.type_ == VAR_INT && __b.type_ == VAR_FLOAT) { \ - (result) = FL_INT_FLOAT_EQUAL(__a.data_.i, __b.data_.f); \ - } else if (__a.type_ == VAR_FLOAT && __b.type_ == VAR_INT) { \ - (result) = FL_INT_FLOAT_EQUAL(__b.data_.i, __a.data_.f); \ - } else { \ - (result) = false; \ - } \ - break; \ - } \ - switch (__a.type_) { \ - case VAR_NIL: \ - (result) = true; \ - break; \ - case VAR_BOOL: \ - (result) = (__a.data_.b == __b.data_.b); \ - break; \ - case VAR_INT: \ - (result) = (__a.data_.i == __b.data_.i); \ - break; \ - case VAR_FLOAT: \ - (result) = (__a.data_.f == __b.data_.f); \ - break; \ - case VAR_STRING: { \ - if (__a.data_.s == __b.data_.s) { \ - (result) = true; \ - break; \ - } \ - (result) = (__a.data_.s->size_ == __b.data_.s->size_ && memcmp(__a.data_.s->data_, __b.data_.s->data_, __a.data_.s->size_) == 0); \ - break; \ - } \ - case VAR_STRINGID: { \ - if (__a.data_.i == __b.data_.i) { \ - (result) = true; \ - break; \ - } \ - VarString *__sa = (VarString *) __a.data_.i; \ - VarString *__sb = (VarString *) __b.data_.i; \ - (result) = (__sa->size_ == __sb->size_ && memcmp(__sa->data_, __sb->data_, __sa->size_) == 0); \ - break; \ - } \ - case VAR_TABLE: \ - (result) = (__a.data_.t == __b.data_.t); \ - break; \ - case VAR_CLOSURE: \ - (result) = (__a.data_.cl == __b.data_.cl); \ - break; \ - default: \ - (result) = false; \ - break; \ - } \ - } while (0) - -#define VarHash(v, hash) \ - do { \ - CVar __v = (v); \ - switch (__v.type_) { \ - case VAR_NIL: \ - (hash) = 0; \ - break; \ - case VAR_BOOL: \ - (hash) = (__v.data_.b ? 1 : 0); \ - break; \ - case VAR_INT: \ - (hash) = (uint32_t) (__v.data_.i ^ (__v.data_.i >> 32)); \ - break; \ - case VAR_FLOAT: { \ - union { \ - double __d; \ - uint64_t __u; \ - } __fu; \ - __fu.__d = __v.data_.f; \ - (hash) = (uint32_t) (__fu.__u ^ (__fu.__u >> 32)); \ - break; \ - } \ - case VAR_STRING: { \ - if (__v.data_.s->hash_ == 0) { \ - __v.data_.s->hash_ = FlHashString(__v.data_.s->data_, __v.data_.s->size_); \ - } \ - (hash) = __v.data_.s->hash_; \ - break; \ - } \ - case VAR_STRINGID: { \ - VarString *__vs = (VarString *) __v.data_.i; \ - if (__vs->hash_ == 0) { \ - __vs->hash_ = FlHashString(__vs->data_, __vs->size_); \ - } \ - (hash) = __vs->hash_; \ - break; \ - } \ - case VAR_TABLE: \ - (hash) = (uint32_t) ((uintptr_t) __v.data_.t ^ ((uintptr_t) __v.data_.t >> 32)); \ - break; \ - case VAR_CLOSURE: \ - (hash) = (uint32_t) ((uintptr_t) __v.data_.cl ^ ((uintptr_t) __v.data_.cl >> 32)); \ - break; \ - default: \ - (hash) = 0; \ - break; \ - } \ - } while (0) - -static inline CVar FlGetTableFn(CVar t, CVar k FL_HINT_PARAMS) { - k = NORMALIZE_TABLE_KEY(k); - if (UNLIKELY(t.type_ != VAR_TABLE)) { - FL_RAISE_HINT(_S, "attempt to index a non-table value"); - } - if (UNLIKELY(k.type_ == VAR_NIL)) { - FL_RAISE_HINT(_S, "table index is nil"); - } - if (UNLIKELY(k.type_ == VAR_FLOAT && isnan(k.data_.f))) { - FL_RAISE_HINT(_S, "table index is NaN"); - } - VarTable *tbl = t.data_.t; - if (tbl->spec_get) { - bool __finish = false; - CVar __r = tbl->spec_get(tbl, k, &__finish); - if (__finish) return __r; - } - if (k.type_ == VAR_INT && (uint64_t) k.data_.i - 1 < (uint64_t) tbl->arr_size_) { - return tbl->arr_[k.data_.i - 1]; - } - if (UNLIKELY(tbl->count_ == 0)) { - return (CVar) {VAR_NIL}; - } - uint32_t h; - VarHash(k, h); - if (LIKELY(tbl->bucket_count_ == 0)) { - bool __eq; - if (tbl->quick_data_[0].hash == h) { - VarEqual(tbl->quick_data_[0].key, k, __eq); - if (__eq) { - return tbl->quick_data_[0].val; - } - } - if (tbl->count_ > 1 && tbl->quick_data_[1].hash == h) { - VarEqual(tbl->quick_data_[1].key, k, __eq); - if (__eq) { - return tbl->quick_data_[1].val; - } - } - if (tbl->count_ > 2 && tbl->quick_data_[2].hash == h) { - VarEqual(tbl->quick_data_[2].key, k, __eq); - if (__eq) { - return tbl->quick_data_[2].val; - } - } - if (tbl->count_ > 3 && tbl->quick_data_[3].hash == h) { - VarEqual(tbl->quick_data_[3].key, k, __eq); - if (__eq) { - return tbl->quick_data_[3].val; - } - } - if (tbl->count_ > 4 && tbl->quick_data_[4].hash == h) { - VarEqual(tbl->quick_data_[4].key, k, __eq); - if (__eq) { - return tbl->quick_data_[4].val; - } - } - if (tbl->count_ > 5 && tbl->quick_data_[5].hash == h) { - VarEqual(tbl->quick_data_[5].key, k, __eq); - if (__eq) { - return tbl->quick_data_[5].val; - } - } - if (tbl->count_ > 6 && tbl->quick_data_[6].hash == h) { - VarEqual(tbl->quick_data_[6].key, k, __eq); - if (__eq) { - return tbl->quick_data_[6].val; - } - } - if (tbl->count_ > 7 && tbl->quick_data_[7].hash == h) { - VarEqual(tbl->quick_data_[7].key, k, __eq); - if (__eq) { - return tbl->quick_data_[7].val; - } - } - } else { - uint32_t mask = tbl->bucket_count_ - 1; - uint32_t idx = h & mask; - TableNode *curr = &tbl->nodes_[idx]; - if (UNLIKELY(curr->entry.key.type_ == VAR_NIL)) { - return (CVar) {VAR_NIL}; - } - while (1) { - if (curr->entry.hash == h) { - bool __eq; - VarEqual(curr->entry.key, k, __eq); - if (__eq) { - return curr->entry.val; - } - } - uint32_t next = curr->next; - if (next == 0xFFFFFFFF) { - break; - } - curr = &tbl->nodes_[next]; - } - } - return (CVar) {VAR_NIL}; -} -#define FlGetTable(...) FlGetTableFn(__VA_ARGS__ FL_HINT_ARGS) - -static inline uint32_t NextPowerOfTwo(uint32_t v) { - v--; - v |= v >> 1; - v |= v >> 2; - v |= v >> 4; - v |= v >> 8; - v |= v >> 16; - v++; - return v; -} - -static inline bool FlTableInsertRaw(VarTable *tbl, CVar key, CVar val, uint32_t hash) { - uint32_t mask = tbl->bucket_count_ - 1; - uint32_t total_nodes = tbl->bucket_count_ + tbl->bucket_count_ / 2; - assert(tbl->count_ < total_nodes); - uint32_t idx = hash & mask; - TableNode *main_node = &tbl->nodes_[idx]; - if (LIKELY(main_node->entry.key.type_ == VAR_NIL)) { - main_node->entry.key = key; - main_node->entry.val = val; - main_node->entry.hash = hash; - main_node->next = 0xFFFFFFFF; - main_node->active_pos = tbl->count_; - tbl->active_list_[tbl->count_] = idx; - tbl->count_++; - return true; - } - uint32_t curr_idx = idx; - while (1) { - TableNode *curr = &tbl->nodes_[curr_idx]; - if (curr->entry.hash == hash) { - bool __eq; - VarEqual(curr->entry.key, key, __eq); - if (__eq) { - curr->entry.val = val; - return true; - } - } - if (curr->next == 0xFFFFFFFF) { - break; - } - curr_idx = curr->next; - } - if (UNLIKELY(tbl->free_list_idx_ == 0xFFFFFFFF)) { - return false; - } - uint32_t new_node_idx = tbl->free_list_idx_; - TableNode *new_node = &tbl->nodes_[new_node_idx]; - tbl->free_list_idx_ = new_node->next; - new_node->entry.key = key; - new_node->entry.val = val; - new_node->entry.hash = hash; - new_node->next = main_node->next; - main_node->next = new_node_idx; - new_node->active_pos = tbl->count_; - tbl->active_list_[tbl->count_] = new_node_idx; - tbl->count_++; - return true; -} - -static inline void FlTableRehash(VarTable *tbl) { - uint32_t old_count = tbl->count_; - uint32_t old_bucket_count = tbl->bucket_count_; - TableNode *old_nodes = tbl->nodes_; - uint32_t new_bucket_count = NextPowerOfTwo(old_count + 1); - if (new_bucket_count <= old_bucket_count) { - new_bucket_count = old_bucket_count * 2; - } - while (1) { - uint32_t overflow_count = new_bucket_count / 2; - uint32_t total_nodes = new_bucket_count + overflow_count; - size_t nodes_size = total_nodes * sizeof(TableNode); - size_t active_list_size = total_nodes * sizeof(uint32_t); - char *buffer = (char *) FlFastAlloc(_S, nodes_size + active_list_size, FL_IN_CONST_INIT); - TableNode *new_nodes = (TableNode *) buffer; - uint32_t *new_active_list = (uint32_t *) (buffer + nodes_size); - { - uint32_t i; - for (i = 0; i < total_nodes; ++i) { - new_nodes[i].entry.key.type_ = VAR_NIL; - new_nodes[i].next = 0xFFFFFFFF; - new_nodes[i].active_pos = 0xFFFFFFFF; - } - } - TableNode *prev_nodes = tbl->nodes_; - uint32_t *prev_active_list = tbl->active_list_; - uint32_t prev_bucket_count = tbl->bucket_count_; - uint32_t prev_count = tbl->count_; - uint32_t prev_free_list_idx = tbl->free_list_idx_; - tbl->nodes_ = new_nodes; - tbl->active_list_ = new_active_list; - tbl->bucket_count_ = new_bucket_count; - tbl->count_ = 0; - if (LIKELY(overflow_count > 0)) { - { - uint32_t i; - for (i = 0; i < overflow_count - 1; ++i) { - tbl->nodes_[new_bucket_count + i].next = new_bucket_count + i + 1; - } - } - tbl->nodes_[new_bucket_count + overflow_count - 1].next = 0xFFFFFFFF; - tbl->free_list_idx_ = new_bucket_count; - } else { - tbl->free_list_idx_ = 0xFFFFFFFF; - } - bool success = true; - if (LIKELY(old_bucket_count == 0)) { - { - uint32_t i; - for (i = 0; i < old_count; ++i) { - if (!FlTableInsertRaw(tbl, tbl->quick_data_[i].key, tbl->quick_data_[i].val, tbl->quick_data_[i].hash)) { - success = false; - break; - } - } - } - } else { - { - uint32_t i; - for (i = 0; i < old_bucket_count; ++i) { - uint32_t curr_idx = i; - while (curr_idx != 0xFFFFFFFF) { - TableNode *old_node = &old_nodes[curr_idx]; - if (LIKELY(old_node->entry.key.type_ != VAR_NIL)) { - if (!FlTableInsertRaw(tbl, old_node->entry.key, old_node->entry.val, old_node->entry.hash)) { - success = false; - break; - } - } - curr_idx = old_node->next; - } - if (!success) { - break; - } - } - } - } - if (LIKELY(success)) { - break; - } else { - tbl->nodes_ = prev_nodes; - tbl->active_list_ = prev_active_list; - tbl->bucket_count_ = prev_bucket_count; - tbl->count_ = prev_count; - tbl->free_list_idx_ = prev_free_list_idx; - new_bucket_count *= 2; - } - } -} - -static inline bool FlArrResizeInsert(VarTable *tbl, CVar k, CVar v); - -static inline void FlHashSet(VarTable *tbl, CVar k, CVar v) { - uint32_t h; - VarHash(k, h); - if (UNLIKELY(v.type_ == VAR_NIL)) { - if (UNLIKELY(tbl->count_ == 0)) { - return; - } - if (LIKELY(tbl->bucket_count_ == 0)) { - bool __eq; - if (tbl->quick_data_[0].hash == h) { - VarEqual(tbl->quick_data_[0].key, k, __eq); - if (__eq) { - if (tbl->count_ > 1) { - tbl->quick_data_[0] = tbl->quick_data_[tbl->count_ - 1]; - } - tbl->count_--; - return; - } - } - if (tbl->count_ > 1 && tbl->quick_data_[1].hash == h) { - VarEqual(tbl->quick_data_[1].key, k, __eq); - if (__eq) { - if (tbl->count_ > 2) { - tbl->quick_data_[1] = tbl->quick_data_[tbl->count_ - 1]; - } - tbl->count_--; - return; - } - } - if (tbl->count_ > 2 && tbl->quick_data_[2].hash == h) { - VarEqual(tbl->quick_data_[2].key, k, __eq); - if (__eq) { - if (tbl->count_ > 3) { - tbl->quick_data_[2] = tbl->quick_data_[tbl->count_ - 1]; - } - tbl->count_--; - return; - } - } - if (tbl->count_ > 3 && tbl->quick_data_[3].hash == h) { - VarEqual(tbl->quick_data_[3].key, k, __eq); - if (__eq) { - if (tbl->count_ > 4) { - tbl->quick_data_[3] = tbl->quick_data_[tbl->count_ - 1]; - } - tbl->count_--; - return; - } - } - if (tbl->count_ > 4 && tbl->quick_data_[4].hash == h) { - VarEqual(tbl->quick_data_[4].key, k, __eq); - if (__eq) { - if (tbl->count_ > 5) { - tbl->quick_data_[4] = tbl->quick_data_[tbl->count_ - 1]; - } - tbl->count_--; - return; - } - } - if (tbl->count_ > 5 && tbl->quick_data_[5].hash == h) { - VarEqual(tbl->quick_data_[5].key, k, __eq); - if (__eq) { - if (tbl->count_ > 6) { - tbl->quick_data_[5] = tbl->quick_data_[tbl->count_ - 1]; - } - tbl->count_--; - return; - } - } - if (tbl->count_ > 6 && tbl->quick_data_[6].hash == h) { - VarEqual(tbl->quick_data_[6].key, k, __eq); - if (__eq) { - if (tbl->count_ > 7) { - tbl->quick_data_[6] = tbl->quick_data_[tbl->count_ - 1]; - } - tbl->count_--; - return; - } - } - if (tbl->count_ > 7 && tbl->quick_data_[7].hash == h) { - VarEqual(tbl->quick_data_[7].key, k, __eq); - if (__eq) { - tbl->count_--; - return; - } - } - } else { - uint32_t mask = tbl->bucket_count_ - 1; - uint32_t idx = h & mask; - TableNode *curr = &tbl->nodes_[idx]; - if (UNLIKELY(curr->entry.key.type_ == VAR_NIL)) { - return; - } - { - bool __eq; - VarEqual(curr->entry.key, k, __eq); - if (curr->entry.hash == h && __eq) { - if (curr->next != 0xFFFFFFFF) { - uint32_t next_idx = curr->next; - TableNode *next_node = &tbl->nodes_[next_idx]; - uint32_t pos = next_node->active_pos; - uint32_t last_node_idx = tbl->active_list_[tbl->count_ - 1]; - if (next_idx != last_node_idx) { - tbl->active_list_[pos] = last_node_idx; - tbl->nodes_[last_node_idx].active_pos = pos; - } - next_node->active_pos = 0xFFFFFFFF; - curr->entry = next_node->entry; - curr->next = next_node->next; - next_node->next = tbl->free_list_idx_; - tbl->free_list_idx_ = next_idx; - } else { - uint32_t pos = curr->active_pos; - uint32_t last_node_idx = tbl->active_list_[tbl->count_ - 1]; - if (idx != last_node_idx) { - tbl->active_list_[pos] = last_node_idx; - tbl->nodes_[last_node_idx].active_pos = pos; - } - curr->active_pos = 0xFFFFFFFF; - curr->entry.key.type_ = VAR_NIL; - } - tbl->count_--; - return; - } - } - uint32_t prev_idx = idx; - uint32_t curr_idx = curr->next; - while (curr_idx != 0xFFFFFFFF) { - TableNode *node = &tbl->nodes_[curr_idx]; - { - bool __eq; - VarEqual(node->entry.key, k, __eq); - if (node->entry.hash == h && __eq) { - uint32_t pos = node->active_pos; - uint32_t last_node_idx = tbl->active_list_[tbl->count_ - 1]; - if (curr_idx != last_node_idx) { - tbl->active_list_[pos] = last_node_idx; - tbl->nodes_[last_node_idx].active_pos = pos; - } - node->active_pos = 0xFFFFFFFF; - tbl->nodes_[prev_idx].next = node->next; - node->next = tbl->free_list_idx_; - tbl->free_list_idx_ = curr_idx; - tbl->count_--; - return; - } - } - prev_idx = curr_idx; - curr_idx = node->next; - } - } - return; - } - if (LIKELY(tbl->bucket_count_ == 0)) { - bool __eq; - if (tbl->count_ > 0 && tbl->quick_data_[0].hash == h) { - VarEqual(tbl->quick_data_[0].key, k, __eq); - if (__eq) { - tbl->quick_data_[0].val = v; - return; - } - } - if (tbl->count_ > 1 && tbl->quick_data_[1].hash == h) { - VarEqual(tbl->quick_data_[1].key, k, __eq); - if (__eq) { - tbl->quick_data_[1].val = v; - return; - } - } - if (tbl->count_ > 2 && tbl->quick_data_[2].hash == h) { - VarEqual(tbl->quick_data_[2].key, k, __eq); - if (__eq) { - tbl->quick_data_[2].val = v; - return; - } - } - if (tbl->count_ > 3 && tbl->quick_data_[3].hash == h) { - VarEqual(tbl->quick_data_[3].key, k, __eq); - if (__eq) { - tbl->quick_data_[3].val = v; - return; - } - } - if (tbl->count_ > 4 && tbl->quick_data_[4].hash == h) { - VarEqual(tbl->quick_data_[4].key, k, __eq); - if (__eq) { - tbl->quick_data_[4].val = v; - return; - } - } - if (tbl->count_ > 5 && tbl->quick_data_[5].hash == h) { - VarEqual(tbl->quick_data_[5].key, k, __eq); - if (__eq) { - tbl->quick_data_[5].val = v; - return; - } - } - if (tbl->count_ > 6 && tbl->quick_data_[6].hash == h) { - VarEqual(tbl->quick_data_[6].key, k, __eq); - if (__eq) { - tbl->quick_data_[6].val = v; - return; - } - } - if (tbl->count_ > 7 && tbl->quick_data_[7].hash == h) { - VarEqual(tbl->quick_data_[7].key, k, __eq); - if (__eq) { - tbl->quick_data_[7].val = v; - return; - } - } - if (tbl->count_ < 8) { - tbl->quick_data_[tbl->count_].key = k; - tbl->quick_data_[tbl->count_].val = v; - tbl->quick_data_[tbl->count_].hash = h; - tbl->count_++; - return; - } - if (k.type_ == VAR_INT && FlArrResizeInsert(tbl, k, v)) { - return; - } - FlTableRehash(tbl); - } - if (UNLIKELY(tbl->count_ >= tbl->bucket_count_ || tbl->free_list_idx_ == 0xFFFFFFFF)) { - if (k.type_ == VAR_INT && FlArrResizeInsert(tbl, k, v)) { - return; - } - FlTableRehash(tbl); - } - FlTableInsertRaw(tbl, k, v, h); -} - -static inline CVar FlHashGetInt(VarTable *tbl, int64_t k) { - if (UNLIKELY(tbl->count_ == 0)) { - return (CVar) {VAR_NIL}; - } - uint32_t h = (uint32_t) (k ^ (k >> 32)); - if (LIKELY(tbl->bucket_count_ == 0)) { - for (uint32_t i = 0; i < tbl->count_; ++i) { - if (tbl->quick_data_[i].hash == h && tbl->quick_data_[i].key.type_ == VAR_INT && tbl->quick_data_[i].key.data_.i == k) { - return tbl->quick_data_[i].val; - } - } - } else { - uint32_t mask = tbl->bucket_count_ - 1; - uint32_t idx = h & mask; - TableNode *curr = &tbl->nodes_[idx]; - if (UNLIKELY(curr->entry.key.type_ == VAR_NIL)) { - return (CVar) {VAR_NIL}; - } - while (1) { - if (curr->entry.hash == h && curr->entry.key.type_ == VAR_INT && curr->entry.key.data_.i == k) { - return curr->entry.val; - } - uint32_t next = curr->next; - if (next == 0xFFFFFFFF) { - break; - } - curr = &tbl->nodes_[next]; - } - } - return (CVar) {VAR_NIL}; -} - -__attribute__((noinline)) static void FlArrGrow(VarTable *tbl) { - uint32_t cap = tbl->arr_cap_ ? tbl->arr_cap_ * 2 : 4; - CVar *na = (CVar *) FlFastAlloc(_S, (size_t) cap * sizeof(CVar), FL_IN_CONST_INIT); - if (tbl->arr_size_ > 0) { - memcpy(na, tbl->arr_, (size_t) tbl->arr_size_ * sizeof(CVar)); - } - tbl->arr_ = na; - tbl->arr_cap_ = cap; -} - -/* 数组末尾追加后,把哈希部分中紧随其后的整数键迁入数组,维持「哈希不含 [1, arr_size_+1]」。*/ -__attribute__((noinline)) static void FlArrMigrate(VarTable *tbl) { - while (tbl->count_ != 0) { - int64_t nk = (int64_t) tbl->arr_size_ + 1; - CVar hv = FlHashGetInt(tbl, nk); - if (hv.type_ == VAR_NIL) { - break; - } - CVar key; - key.type_ = VAR_INT; - key.flag_ = 0; - key.data_.i = nk; - FlHashSet(tbl, key, (CVar) {VAR_NIL}); - if (UNLIKELY(tbl->arr_size_ == tbl->arr_cap_)) { - FlArrGrow(tbl); - } - tbl->arr_[tbl->arr_size_++] = hv; - } -} - -/* 整数键写入的数组部分快路径。返回 true 表示已处理。*/ -static inline bool FlArrSetInt(VarTable *tbl, int64_t k, CVar v) { - uint64_t idx = (uint64_t) k - 1; - if (LIKELY(idx < (uint64_t) tbl->arr_size_)) { - tbl->arr_[idx] = v; - return true; - } - if (idx == (uint64_t) tbl->arr_size_ && v.type_ != VAR_NIL && !tbl->spec_set) { - if (UNLIKELY(tbl->arr_size_ == tbl->arr_cap_)) { - FlArrGrow(tbl); - } - tbl->arr_[tbl->arr_size_++] = v; - if (UNLIKELY(tbl->count_ != 0)) { - FlArrMigrate(tbl); - } - return true; - } - return false; -} - -/* 哈希部分写满、即将为整数键 nk 扩容前,按 Lua 的 computesizes 规则判断是否应把整数键迁入数组: - 选取最大的 2^i,使 1..2^i 内的整数键数量超过 2^i 的一半。返回 true 表示数组已扩大。 */ -__attribute__((noinline)) static bool FlArrTryResize(VarTable *tbl, int64_t nk) { - uint32_t nums[32]; - uint64_t total = 0; - uint32_t i; - memset(nums, 0, sizeof(nums)); -#define FL_COUNT_INT_KEY(kk) \ - do { \ - int64_t __ck = (kk); \ - if (__ck >= 1 && __ck <= (1LL << 30)) { \ - nums[__ck == 1 ? 0 : 64 - __builtin_clzll((uint64_t) __ck - 1)]++; \ - total++; \ - } \ - } while (0) - for (i = 0; i < tbl->arr_size_; ++i) { - if (tbl->arr_[i].type_ != VAR_NIL) { - FL_COUNT_INT_KEY((int64_t) i + 1); - } - } - if (tbl->bucket_count_ == 0) { - for (i = 0; i < tbl->count_; ++i) { - if (tbl->quick_data_[i].key.type_ == VAR_INT) { - FL_COUNT_INT_KEY(tbl->quick_data_[i].key.data_.i); - } - } - } else { - for (i = 0; i < tbl->count_; ++i) { - VarEntry *e = &tbl->nodes_[tbl->active_list_[i]].entry; - if (e->key.type_ == VAR_INT) { - FL_COUNT_INT_KEY(e->key.data_.i); - } - } - } - FL_COUNT_INT_KEY(nk); -#undef FL_COUNT_INT_KEY - uint64_t a = 0, optimal = 0, two = 1; - for (i = 0; i < 31 && total > two / 2; ++i, two <<= 1) { - a += nums[i]; - if (a > two / 2) { - optimal = two; - } - } - if (optimal <= (uint64_t) tbl->arr_size_) { - return false; - } - { - uint32_t old_size = tbl->arr_size_; - CVar *na = (CVar *) FlFastAlloc(_S, (size_t) optimal * sizeof(CVar), FL_IN_CONST_INIT); - if (old_size > 0) { - memcpy(na, tbl->arr_, (size_t) old_size * sizeof(CVar)); - } - memset(na + old_size, 0, (size_t) (optimal - old_size) * sizeof(CVar)); - tbl->arr_ = na; - tbl->arr_cap_ = (uint32_t) optimal; - tbl->arr_size_ = (uint32_t) optimal; - if (tbl->count_ != 0) { - uint64_t j; - for (j = old_size + 1; j <= optimal && tbl->count_ != 0; ++j) { - CVar hv = FlHashGetInt(tbl, (int64_t) j); - if (hv.type_ == VAR_NIL) { - continue; - } - CVar key; - key.type_ = VAR_INT; - key.flag_ = 0; - key.data_.i = (int64_t) j; - FlHashSet(tbl, key, (CVar) {VAR_NIL}); - tbl->arr_[j - 1] = hv; - } - FlArrMigrate(tbl); - } - } - return true; -} - -/* 整数键 k 要插入已写满的哈希部分前调用;返回 true 表示 k 已写入数组或哈希空位,调用方直接返回。*/ -static inline bool FlArrResizeInsert(VarTable *tbl, CVar k, CVar v) { - if (k.type_ != VAR_INT || tbl->spec_set) { - return false; - } - if (!FlArrTryResize(tbl, k.data_.i)) { - return false; - } - if (FlArrSetInt(tbl, k.data_.i, v)) { - return true; - } - uint32_t h = (uint32_t) (k.data_.i ^ (k.data_.i >> 32)); - if (tbl->bucket_count_ == 0) { - if (tbl->count_ < 8) { - tbl->quick_data_[tbl->count_].key = k; - tbl->quick_data_[tbl->count_].val = v; - tbl->quick_data_[tbl->count_].hash = h; - tbl->count_++; - return true; - } - } else { - if (tbl->count_ < tbl->bucket_count_ && tbl->free_list_idx_ != 0xFFFFFFFF) { - FlTableInsertRaw(tbl, k, v, h); - return true; - } - } - return false; -} - -static inline void FlSetTableImpl(CVar t, CVar k, CVar v FL_HINT_PARAMS) { - k = NORMALIZE_TABLE_KEY(k); - if (UNLIKELY(t.type_ != VAR_TABLE)) { - FL_RAISE_HINT(_S, "attempt to index a non-table value"); - } - FL_CHECK_CONST_TABLE_HINT(t); - if (UNLIKELY(k.type_ == VAR_NIL)) { - FL_RAISE_HINT(_S, "table index is nil"); - } - if (UNLIKELY(k.type_ == VAR_FLOAT && isnan(k.data_.f))) { - FL_RAISE_HINT(_S, "table index is NaN"); - } - VarTable *tbl = t.data_.t; - if (UNLIKELY(tbl->spec_set)) { - bool __finish = false; - tbl->spec_set(tbl, k, v, &__finish); - if (__finish) return; - } - if (k.type_ == VAR_INT && FlArrSetInt(tbl, k.data_.i, v)) { - return; - } - FlHashSet(tbl, k, v); -} - -static inline void FlSetTableFn(CVar t, CVar k, CVar v FL_HINT_PARAMS) { - k = NORMALIZE_TABLE_KEY(k); - FlSetTableImpl(t, k, v FL_HINT_PASS); - if (LIKELY(t.type_ == VAR_TABLE)) { - if (k.type_ == VAR_INT) { - FlSeqNoteIntSet(t.data_.t, k.data_.i, v.type_ == VAR_NIL); - } - } -} -#define FlSetTable(...) FlSetTableFn(__VA_ARGS__ FL_HINT_ARGS) - -static inline CVar FlGetTableIntFn(CVar t, int64_t k FL_HINT_PARAMS) { - if (UNLIKELY(t.type_ != VAR_TABLE)) { - FL_RAISE_HINT(_S, "attempt to index a non-table value"); - } - VarTable *tbl = t.data_.t; - if (LIKELY((uint64_t) k - 1 < (uint64_t) tbl->arr_size_)) { - return tbl->arr_[k - 1]; - } - if (UNLIKELY(tbl->spec_get)) { - CVar key_cvar; - key_cvar.type_ = VAR_INT; - key_cvar.flag_ = 0; - key_cvar.data_.i = k; - bool __finish = false; - CVar __r = tbl->spec_get(tbl, key_cvar, &__finish); - if (__finish) return __r; - } - return FlHashGetInt(tbl, k); -} -#define FlGetTableInt(...) FlGetTableIntFn(__VA_ARGS__ FL_HINT_ARGS) - -static inline void FlSetTableIntImpl(CVar t, int64_t k, CVar v FL_HINT_PARAMS) { - if (UNLIKELY(t.type_ != VAR_TABLE)) { - FL_RAISE_HINT(_S, "attempt to index a non-table value"); - } - FL_CHECK_CONST_TABLE_HINT(t); - VarTable *tbl = t.data_.t; - if (UNLIKELY(tbl->spec_set)) { - CVar key_cvar; - key_cvar.type_ = VAR_INT; - key_cvar.flag_ = 0; - key_cvar.data_.i = k; - bool __finish = false; - tbl->spec_set(tbl, key_cvar, v, &__finish); - if (__finish) return; - } - if (FlArrSetInt(tbl, k, v)) { - return; - } - uint32_t h = (uint32_t) (k ^ (k >> 32)); - CVar key_cvar; - key_cvar.type_ = VAR_INT; - key_cvar.flag_ = 0; - key_cvar.data_.i = k; - if (UNLIKELY(v.type_ == VAR_NIL)) { - FlHashSet(tbl, key_cvar, v); - return; - } - if (LIKELY(tbl->bucket_count_ == 0)) { - for (uint32_t i = 0; i < tbl->count_; ++i) { - if (tbl->quick_data_[i].hash == h && tbl->quick_data_[i].key.type_ == VAR_INT && tbl->quick_data_[i].key.data_.i == k) { - tbl->quick_data_[i].val = v; - return; - } - } - if (tbl->count_ < 8) { - tbl->quick_data_[tbl->count_].key = key_cvar; - tbl->quick_data_[tbl->count_].val = v; - tbl->quick_data_[tbl->count_].hash = h; - tbl->count_++; - return; - } - if (FlArrResizeInsert(tbl, key_cvar, v)) { - return; - } - FlTableRehash(tbl); - } - if (UNLIKELY(tbl->count_ >= tbl->bucket_count_ || tbl->free_list_idx_ == 0xFFFFFFFF)) { - if (FlArrResizeInsert(tbl, key_cvar, v)) { - return; - } - FlTableRehash(tbl); - } - FlTableInsertRaw(tbl, key_cvar, v, h); -} - -static inline void FlSetTableIntFn(CVar t, int64_t k, CVar v FL_HINT_PARAMS) { - if (LIKELY(t.type_ == VAR_TABLE && (!(t.flag_ & CONST_FLAG) || FL_IN_CONST_INIT))) { - VarTable *tbl = t.data_.t; - uint64_t idx = (uint64_t) k - 1; - /* 新旧值都非 nil 时序列长度不变,可跳过缓存维护。*/ - if (LIKELY(idx < (uint64_t) tbl->arr_size_ && v.type_ != VAR_NIL && tbl->arr_[idx].type_ != VAR_NIL)) { - tbl->arr_[idx] = v; - return; - } - } - FlSetTableIntImpl(t, k, v FL_HINT_PASS); - if (LIKELY(t.type_ == VAR_TABLE)) { - FlSeqNoteIntSet(t.data_.t, k, v.type_ == VAR_NIL); - } -} -#define FlSetTableInt(...) FlSetTableIntFn(__VA_ARGS__ FL_HINT_ARGS) - -static inline void FlSetTableInt_IntFn(CVar t, int64_t k, int64_t v FL_HINT_PARAMS) { - if (LIKELY(t.type_ == VAR_TABLE && (!(t.flag_ & CONST_FLAG) || FL_IN_CONST_INIT))) { - VarTable *tbl = t.data_.t; - uint64_t idx = (uint64_t) k - 1; - if (LIKELY(idx < (uint64_t) tbl->arr_size_ && tbl->arr_[idx].type_ != VAR_NIL)) { - tbl->arr_[idx].type_ = VAR_INT; - tbl->arr_[idx].flag_ = 0; - tbl->arr_[idx].data_.i = v; - return; - } - } - CVar cv; - cv.type_ = VAR_INT; - cv.flag_ = 0; - cv.data_.i = v; - FlSetTableIntImpl(t, k, cv FL_HINT_PASS); - if (LIKELY(t.type_ == VAR_TABLE)) { - FlSeqNoteIntSet(t.data_.t, k, false); - } -} -#define FlSetTableInt_Int(...) FlSetTableInt_IntFn(__VA_ARGS__ FL_HINT_ARGS) - -static inline void FlSetTableInt_FloatFn(CVar t, int64_t k, double v FL_HINT_PARAMS) { - if (LIKELY(t.type_ == VAR_TABLE && (!(t.flag_ & CONST_FLAG) || FL_IN_CONST_INIT))) { - VarTable *tbl = t.data_.t; - uint64_t idx = (uint64_t) k - 1; - if (LIKELY(idx < (uint64_t) tbl->arr_size_ && tbl->arr_[idx].type_ != VAR_NIL)) { - tbl->arr_[idx].type_ = VAR_FLOAT; - tbl->arr_[idx].flag_ = 0; - tbl->arr_[idx].data_.f = v; - return; - } - } - CVar cv; - cv.type_ = VAR_FLOAT; - cv.flag_ = 0; - cv.data_.f = v; - FlSetTableIntImpl(t, k, cv FL_HINT_PASS); - if (LIKELY(t.type_ == VAR_TABLE)) { - FlSeqNoteIntSet(t.data_.t, k, false); - } -} -#define FlSetTableInt_Float(...) FlSetTableInt_FloatFn(__VA_ARGS__ FL_HINT_ARGS) - -static inline void FlSetTableInt_BoolFn(CVar t, int64_t k, bool b FL_HINT_PARAMS) { - if (LIKELY(t.type_ == VAR_TABLE && (!(t.flag_ & CONST_FLAG) || FL_IN_CONST_INIT))) { - VarTable *tbl = t.data_.t; - uint64_t idx = (uint64_t) k - 1; - if (LIKELY(idx < (uint64_t) tbl->arr_size_ && tbl->arr_[idx].type_ != VAR_NIL)) { - tbl->arr_[idx].type_ = VAR_BOOL; - tbl->arr_[idx].flag_ = 0; - tbl->arr_[idx].data_.b = b; - return; - } - } - CVar cv; - cv.type_ = VAR_BOOL; - cv.flag_ = 0; - cv.data_.b = b; - FlSetTableIntImpl(t, k, cv FL_HINT_PASS); - if (LIKELY(t.type_ == VAR_TABLE)) { - FlSeqNoteIntSet(t.data_.t, k, false); - } -} -#define FlSetTableInt_Bool(...) FlSetTableInt_BoolFn(__VA_ARGS__ FL_HINT_ARGS) - -static inline CVar FlGetTableStrIdFn(CVar t, int64_t str_id FL_HINT_PARAMS) { - if (UNLIKELY(t.type_ != VAR_TABLE)) { - FL_RAISE_HINT(_S, "attempt to index a non-table value"); - } - VarTable *tbl = t.data_.t; - if (UNLIKELY(tbl->spec_get)) { - CVar k; - k.type_ = VAR_STRINGID; - k.flag_ = 0; - k.data_.i = str_id; - bool __finish = false; - CVar __r = tbl->spec_get(tbl, k, &__finish); - if (__finish) return __r; - } - // hash fallback - if (UNLIKELY(tbl->count_ == 0)) { - return (CVar) {VAR_NIL}; - } - VarString *vs = (VarString *) str_id; - uint32_t h = LIKELY(vs->hash_ != 0) ? vs->hash_ : (vs->hash_ = FlHashString(vs->data_, vs->size_)); - if (LIKELY(tbl->bucket_count_ == 0)) { - for (uint32_t i = 0; i < tbl->count_; ++i) { - if (tbl->quick_data_[i].hash == h) { - CVar ek = tbl->quick_data_[i].key; - if (LIKELY(ek.type_ == VAR_STRINGID)) { - if (ek.data_.i == str_id) { - return tbl->quick_data_[i].val; - } - VarString *evs = (VarString *) ek.data_.i; - if (evs->size_ == vs->size_ && memcmp(evs->data_, vs->data_, vs->size_) == 0) { - return tbl->quick_data_[i].val; - } - } else if (UNLIKELY(ek.type_ == VAR_STRING)) { - if (ek.data_.s->size_ == vs->size_ && memcmp(ek.data_.s->data_, vs->data_, vs->size_) == 0) { - return tbl->quick_data_[i].val; - } - } - } - } - } else { - uint32_t mask = tbl->bucket_count_ - 1; - uint32_t idx = h & mask; - TableNode *curr = &tbl->nodes_[idx]; - if (UNLIKELY(curr->entry.key.type_ == VAR_NIL)) { - return (CVar) {VAR_NIL}; - } - while (1) { - if (curr->entry.hash == h) { - CVar ek = curr->entry.key; - if (LIKELY(ek.type_ == VAR_STRINGID)) { - if (ek.data_.i == str_id) { - return curr->entry.val; - } - VarString *evs = (VarString *) ek.data_.i; - if (evs->size_ == vs->size_ && memcmp(evs->data_, vs->data_, vs->size_) == 0) { - return curr->entry.val; - } - } else if (UNLIKELY(ek.type_ == VAR_STRING)) { - if (ek.data_.s->size_ == vs->size_ && memcmp(ek.data_.s->data_, vs->data_, vs->size_) == 0) { - return curr->entry.val; - } - } - } - uint32_t next = curr->next; - if (next == 0xFFFFFFFF) { - break; - } - curr = &tbl->nodes_[next]; - } - } - return (CVar) {VAR_NIL}; -} -#define FlGetTableStrId(...) FlGetTableStrIdFn(__VA_ARGS__ FL_HINT_ARGS) - -static inline void FlSetTableStrIdFn(CVar t, int64_t str_id, CVar v FL_HINT_PARAMS) { - if (UNLIKELY(t.type_ != VAR_TABLE)) { - FL_RAISE_HINT(_S, "attempt to index a non-table value"); - } - FL_CHECK_CONST_TABLE_HINT(t); - VarTable *tbl = t.data_.t; - if (UNLIKELY(tbl->spec_set)) { - CVar k; - k.type_ = VAR_STRINGID; - k.flag_ = 0; - k.data_.i = str_id; - bool __finish = false; - tbl->spec_set(tbl, k, v, &__finish); - if (__finish) return; - } - // hash fallback - VarString *vs = (VarString *) str_id; - uint32_t h = LIKELY(vs->hash_ != 0) ? vs->hash_ : (vs->hash_ = FlHashString(vs->data_, vs->size_)); - CVar key_cvar; - key_cvar.type_ = VAR_STRINGID; - key_cvar.flag_ = 0; - key_cvar.data_.i = str_id; - if (UNLIKELY(v.type_ == VAR_NIL)) { - FlSetTableFn((CVar) {.type_ = VAR_TABLE, .data_.t = tbl}, key_cvar, v FL_HINT_PASS); - return; - } - if (LIKELY(tbl->bucket_count_ == 0)) { - for (uint32_t i = 0; i < tbl->count_; ++i) { - if (tbl->quick_data_[i].hash == h) { - CVar ek = tbl->quick_data_[i].key; - if (LIKELY(ek.type_ == VAR_STRINGID)) { - if (ek.data_.i == str_id) { - tbl->quick_data_[i].val = v; - return; - } - VarString *evs = (VarString *) ek.data_.i; - if (evs->size_ == vs->size_ && memcmp(evs->data_, vs->data_, vs->size_) == 0) { - tbl->quick_data_[i].val = v; - return; - } - } else if (UNLIKELY(ek.type_ == VAR_STRING)) { - if (ek.data_.s->size_ == vs->size_ && memcmp(ek.data_.s->data_, vs->data_, vs->size_) == 0) { - tbl->quick_data_[i].val = v; - return; - } - } - } - } - if (tbl->count_ < 8) { - tbl->quick_data_[tbl->count_].key = key_cvar; - tbl->quick_data_[tbl->count_].val = v; - tbl->quick_data_[tbl->count_].hash = h; - tbl->count_++; - return; - } - FlTableRehash(tbl); - } - if (UNLIKELY(tbl->count_ >= tbl->bucket_count_ || tbl->free_list_idx_ == 0xFFFFFFFF)) { - FlTableRehash(tbl); - } - FlTableInsertRaw(tbl, key_cvar, v, h); -} -#define FlSetTableStrId(...) FlSetTableStrIdFn(__VA_ARGS__ FL_HINT_ARGS) - -static inline void FlTableExpandMultiFn(CVar t, int64_t start_idx, CVar v FL_HINT_PARAMS) { - if (LIKELY(v.type_ != VAR_MULTI)) { - FlSetTableIntFn(t, start_idx, v FL_HINT_PASS); - } else { - VarMulti *m = v.data_.m; - for (uint32_t i = 0; i < m->count; ++i) { - FlSetTableIntFn(t, start_idx + i, m->vars[i] FL_HINT_PASS); - } - } -} -#define FlTableExpandMulti(...) FlTableExpandMultiFn(__VA_ARGS__ FL_HINT_ARGS) - -#define CheckNum(v) \ - do { \ - if (UNLIKELY((v).type_ != VAR_INT && (v).type_ != VAR_FLOAT)) { \ - FL_RAISE(_S, "attempt to perform arithmetic on non-numeric value"); \ - } \ - } while (0) -#define CheckNumHint(v) \ - do { \ - if (UNLIKELY((v).type_ != VAR_INT && (v).type_ != VAR_FLOAT)) { \ - FL_RAISE_HINT(_S, "attempt to perform arithmetic on non-numeric value"); \ - } \ - } while (0) - -#define CheckInt(v, result) \ - do { \ - if (LIKELY((v).type_ == VAR_INT)) { \ - (result) = (v).data_.i; \ - } else if (UNLIKELY((v).type_ == VAR_FLOAT)) { \ - if (!FlDoubleFitsInt64((v).data_.f, &(result))) { \ - FL_RAISE(_S, "number has no integer representation"); \ - } \ - } else { \ - FL_RAISE(_S, "attempt to perform bitwise operation on non-numeric value"); \ - (result) = 0; \ - } \ - } while (0) - -#define CVAR_TO_DOUBLE(v) ((v).type_ == VAR_INT ? (double) (v).data_.i : (v).data_.f) - -#define FL_INT_ADD(a, b) ((int64_t) ((uint64_t) (int64_t) (a) + (uint64_t) (int64_t) (b))) -#define FL_INT_SUB(a, b) ((int64_t) ((uint64_t) (int64_t) (a) - (uint64_t) (int64_t) (b))) -#define FL_INT_MUL(a, b) ((int64_t) ((uint64_t) (int64_t) (a) * (uint64_t) (int64_t) (b))) - -#define OP_ARITH_IMPL(a, b, res, op) \ - do { \ - CVar _ra = (a); \ - CVar _rb = (b); \ - if (LIKELY(_ra.type_ == VAR_INT && _rb.type_ == VAR_INT)) { \ - SET_INT(res, _ra.data_.i op _rb.data_.i); \ - } else { \ - CheckNum(_ra); \ - CheckNum(_rb); \ - SET_FLOAT(res, CVAR_TO_DOUBLE(_ra) op CVAR_TO_DOUBLE(_rb)); \ - } \ - } while (0) - -#define OpAdd(a, b, res) \ - do { \ - CVar _ra = (a); \ - CVar _rb = (b); \ - if (LIKELY(_ra.type_ == VAR_INT && _rb.type_ == VAR_INT)) { \ - SET_INT(res, FL_INT_ADD(_ra.data_.i, _rb.data_.i)); \ - } else { \ - CheckNum(_ra); \ - CheckNum(_rb); \ - SET_FLOAT(res, CVAR_TO_DOUBLE(_ra) + CVAR_TO_DOUBLE(_rb)); \ - } \ - } while (0) -#define OpSub(a, b, res) \ - do { \ - CVar _ra = (a); \ - CVar _rb = (b); \ - if (LIKELY(_ra.type_ == VAR_INT && _rb.type_ == VAR_INT)) { \ - SET_INT(res, FL_INT_SUB(_ra.data_.i, _rb.data_.i)); \ - } else { \ - CheckNum(_ra); \ - CheckNum(_rb); \ - SET_FLOAT(res, CVAR_TO_DOUBLE(_ra) - CVAR_TO_DOUBLE(_rb)); \ - } \ - } while (0) -#define OpMul(a, b, res) \ - do { \ - CVar _ra = (a); \ - CVar _rb = (b); \ - if (LIKELY(_ra.type_ == VAR_INT && _rb.type_ == VAR_INT)) { \ - SET_INT(res, FL_INT_MUL(_ra.data_.i, _rb.data_.i)); \ - } else { \ - CheckNum(_ra); \ - CheckNum(_rb); \ - SET_FLOAT(res, CVAR_TO_DOUBLE(_ra) * CVAR_TO_DOUBLE(_rb)); \ - } \ - } while (0) - -#define OpDiv(a, b, res) \ - do { \ - CVar _ra = (a); \ - CVar _rb = (b); \ - CheckNum(_ra); \ - CheckNum(_rb); \ - SET_FLOAT(res, CVAR_TO_DOUBLE(_ra) / CVAR_TO_DOUBLE(_rb)); \ - } while (0) - -#define OpFloorDiv(a, b, res) \ - do { \ - CVar _ra = (a); \ - CVar _rb = (b); \ - if (LIKELY(_ra.type_ == VAR_INT && _rb.type_ == VAR_INT)) { \ - int64_t _q; \ - FlFloorDivInt(_ra.data_.i, _rb.data_.i, _q); \ - SET_INT(res, _q); \ - } else { \ - CheckNum(_ra); \ - CheckNum(_rb); \ - SET_FLOAT(res, floor(CVAR_TO_DOUBLE(_ra) / CVAR_TO_DOUBLE(_rb))); \ - } \ - } while (0) - -#define OpPow(a, b, res) \ - do { \ - CVar _ra = (a); \ - CVar _rb = (b); \ - CheckNum(_ra); \ - CheckNum(_rb); \ - SET_FLOAT(res, pow(CVAR_TO_DOUBLE(_ra), CVAR_TO_DOUBLE(_rb))); \ - } while (0) - -#define OpMod(a, b, res) \ - do { \ - CVar _ra = (a); \ - CVar _rb = (b); \ - if (LIKELY(_ra.type_ == VAR_INT && _rb.type_ == VAR_INT)) { \ - int64_t _r; \ - FlModInt(_ra.data_.i, _rb.data_.i, _r); \ - SET_INT(res, _r); \ - } else { \ - CheckNum(_ra); \ - CheckNum(_rb); \ - double _fa = CVAR_TO_DOUBLE(_ra); \ - double _fb = CVAR_TO_DOUBLE(_rb); \ - SET_FLOAT(res, _fa - _fb * floor(_fa / _fb)); \ - } \ - } while (0) - -#define OP_BIT_IMPL(a, b, res, op) \ - do { \ - int64_t _ai; \ - int64_t _bi; \ - CheckInt(a, _ai); \ - CheckInt(b, _bi); \ - SET_INT(res, _ai op _bi); \ - } while (0) - -#define OpBitAnd(a, b, res) OP_BIT_IMPL(a, b, res, &) -#define OpBitXor(a, b, res) OP_BIT_IMPL(a, b, res, ^) -#define OpBitOr(a, b, res) OP_BIT_IMPL(a, b, res, |) - -#define OP_SHIFT_IMPL(a, b, res, _right) \ - do { \ - int64_t _ai; \ - int64_t _bi; \ - CheckInt(a, _ai); \ - CheckInt(b, _bi); \ - if (_bi >= 64 || _bi <= -64) { \ - SET_INT(res, 0); \ - } else if (_bi >= 0) { \ - SET_INT(res, (int64_t) ((_right) ? ((uint64_t) _ai >> _bi) : ((uint64_t) _ai << _bi))); \ - } else { \ - SET_INT(res, (int64_t) ((_right) ? ((uint64_t) _ai << (-_bi)) : ((uint64_t) _ai >> (-_bi)))); \ - } \ - } while (0) - -#define OpRightShift(a, b, res) OP_SHIFT_IMPL(a, b, res, 1) -#define OpLeftShift(a, b, res) OP_SHIFT_IMPL(a, b, res, 0) - -static inline bool FlCmpLtFn(CVar a, CVar b FL_HINT_PARAMS) { - if (LIKELY(a.type_ == VAR_INT && b.type_ == VAR_INT)) { - return a.data_.i < b.data_.i; - } - CheckNumHint(a); - CheckNumHint(b); - if (a.type_ == VAR_INT) { - return !isnan(b.data_.f) && FlCompareIntDouble(a.data_.i, b.data_.f) < 0; - } else if (b.type_ == VAR_INT) { - return !isnan(a.data_.f) && 0 < FlCompareIntDouble(b.data_.i, a.data_.f); - } else { - return a.data_.f < b.data_.f; - } -} -#define FlCmpLt(...) FlCmpLtFn(__VA_ARGS__ FL_HINT_ARGS) - -static inline bool FlCmpGtFn(CVar a, CVar b FL_HINT_PARAMS) { - if (LIKELY(a.type_ == VAR_INT && b.type_ == VAR_INT)) { - return a.data_.i > b.data_.i; - } - CheckNumHint(a); - CheckNumHint(b); - if (a.type_ == VAR_INT) { - return !isnan(b.data_.f) && FlCompareIntDouble(a.data_.i, b.data_.f) > 0; - } else if (b.type_ == VAR_INT) { - return !isnan(a.data_.f) && 0 > FlCompareIntDouble(b.data_.i, a.data_.f); - } else { - return a.data_.f > b.data_.f; - } -} -#define FlCmpGt(...) FlCmpGtFn(__VA_ARGS__ FL_HINT_ARGS) - -static inline bool FlCmpLeFn(CVar a, CVar b FL_HINT_PARAMS) { - if (LIKELY(a.type_ == VAR_INT && b.type_ == VAR_INT)) { - return a.data_.i <= b.data_.i; - } - CheckNumHint(a); - CheckNumHint(b); - if (a.type_ == VAR_INT) { - return !isnan(b.data_.f) && FlCompareIntDouble(a.data_.i, b.data_.f) <= 0; - } else if (b.type_ == VAR_INT) { - return !isnan(a.data_.f) && 0 <= FlCompareIntDouble(b.data_.i, a.data_.f); - } else { - return a.data_.f <= b.data_.f; - } -} -#define FlCmpLe(...) FlCmpLeFn(__VA_ARGS__ FL_HINT_ARGS) - -static inline bool FlCmpGeFn(CVar a, CVar b FL_HINT_PARAMS) { - if (LIKELY(a.type_ == VAR_INT && b.type_ == VAR_INT)) { - return a.data_.i >= b.data_.i; - } - CheckNumHint(a); - CheckNumHint(b); - if (a.type_ == VAR_INT) { - return !isnan(b.data_.f) && FlCompareIntDouble(a.data_.i, b.data_.f) >= 0; - } else if (b.type_ == VAR_INT) { - return !isnan(a.data_.f) && 0 >= FlCompareIntDouble(b.data_.i, a.data_.f); - } else { - return a.data_.f >= b.data_.f; - } -} -#define FlCmpGe(...) FlCmpGeFn(__VA_ARGS__ FL_HINT_ARGS) - -static inline bool FlCmpEq(CVar a, CVar b) { - bool _eq; - VarEqual(a, b, _eq); - return _eq; -} - -static inline bool FlCmpNe(CVar a, CVar b) { - bool _eq; - VarEqual(a, b, _eq); - return !_eq; -} - -#define OpLt(a, b, res) SET_BOOL(res, FlCmpLt(a, b)) -#define OpGt(a, b, res) SET_BOOL(res, FlCmpGt(a, b)) -#define OpLe(a, b, res) SET_BOOL(res, FlCmpLe(a, b)) -#define OpGe(a, b, res) SET_BOOL(res, FlCmpGe(a, b)) - -#define OpEq(a, b, res) SET_BOOL(res, FlCmpEq(a, b)) - -#define OpNe(a, b, res) SET_BOOL(res, FlCmpNe(a, b)) - -#define OpNot(a, res) \ - do { \ - bool _bt; \ - IsTrue(a, _bt); \ - SET_BOOL(res, !_bt); \ - } while (0) - -#define OpUnaryMinus(a, res) \ - do { \ - CVar _ra = (a); \ - if (LIKELY(_ra.type_ == VAR_INT)) { \ - SET_INT(res, FL_INT_SUB(0, _ra.data_.i)); \ - } else { \ - CheckNum(_ra); \ - SET_FLOAT(res, -_ra.data_.f); \ - } \ - } while (0) - -#define OpBitNot(a, res) \ - do { \ - int64_t _ai; \ - CheckInt(a, _ai); \ - SET_INT(res, ~_ai); \ - } while (0) - -#define OpLen(a, res) \ - do { \ - CVar _lv = (a); \ - if (LIKELY(_lv.type_ == VAR_STRING)) { \ - SET_INT(res, STR_SIZE(_lv.data_.s)); \ - } else if (_lv.type_ == VAR_STRINGID) { \ - SET_INT(res, STR_SIZE((VarString *) _lv.data_.i)); \ - } else if (_lv.type_ == VAR_TABLE) { \ - SET_INT(res, FlGetTableSeqLen(_lv.data_.t)); \ - } else { \ - FL_RAISE(_S, "attempt to get length of a non-string/table value"); \ - } \ - } while (0) - -#define OpConcat(a, b, res) \ - do { \ - res = FlConcat(a, b); \ - } while (0) - -static inline int FlFastItoA(int64_t v, char *buf) { - if (v == 0) { - buf[0] = '0'; - return 1; - } - char tmp[24]; - int i = 0; - bool neg = false; - uint64_t u; - if (v < 0) { - neg = true; - u = 0ULL - (uint64_t) v; - } else { - u = (uint64_t) v; - } - while (u > 0) { - tmp[i++] = (char) ('0' + (u % 10)); - u /= 10; - } - int len = 0; - if (neg) { - buf[len++] = '-'; - } - while (i > 0) { - buf[len++] = tmp[--i]; - } - return len; -} - -static inline int FlVarToStr(CVar v, char *buf, int buf_size FL_HINT_PARAMS) { - switch (v.type_) { - case VAR_NIL: - memcpy(buf, "nil", 3); - buf[3] = '\0'; - return 3; - case VAR_BOOL: - if (v.data_.b) { - memcpy(buf, "true", 4); - buf[4] = '\0'; - return 4; - } else { - memcpy(buf, "false", 5); - buf[5] = '\0'; - return 5; - } - case VAR_INT: - return FlFastItoA(v.data_.i, buf); - case VAR_FLOAT: { - /* Lua 5.4 tostringbuff: %.14g,结果只含数字和符号时补 .0 */ - int flen = snprintf(buf, buf_size, "%.14g", v.data_.f); - int fi = 0; - if (flen > 0 && flen < buf_size) { - if (buf[fi] == '-') fi++; - for (; fi < flen; fi++) { - if (buf[fi] < '0' || buf[fi] > '9') break; - } - if (fi == flen && flen + 2 < buf_size) { - buf[flen] = '.'; - buf[flen + 1] = '0'; - flen += 2; - } - } - return flen; - } - case VAR_STRING: - case VAR_STRINGID: - FL_RAISE_HINT(_S, "FlVarToStr: string type should be handled by caller"); - return 0; - case VAR_TABLE: { - int __n = snprintf(buf, buf_size - 1, "table(0x%llx", (unsigned long long) (uintptr_t) v.data_.t); - if (__n > 0 && __n < buf_size - 1) { - buf[__n] = ')'; - buf[__n + 1] = '\0'; - return __n + 1; - } - return __n; - } - case VAR_CLOSURE: { - int __n = snprintf(buf, buf_size - 1, "function(0x%llx", (unsigned long long) (uintptr_t) v.data_.cl); - if (__n > 0 && __n < buf_size - 1) { - buf[__n] = ')'; - buf[__n + 1] = '\0'; - return __n + 1; - } - return __n; - } - default: - return 0; - } -} - -// Lua 的 .. 只接受字符串和数字,nil / bool / table / function 一律报错。 -// 检查放在非字符串分支里,字符串拼接的快路径不受影响。 -#define CONCAT_CHECK_NUMERIC(v) \ - if (UNLIKELY((v).type_ != VAR_INT && (v).type_ != VAR_FLOAT)) { \ - FL_RAISE(_S, "attempt to concatenate a non-string value"); \ - } - -static inline CVar FlConcatFn(CVar a, CVar b FL_HINT_PARAMS) { - char buf_a[64], buf_b[64]; - const char *sa = buf_a; - int la; - const char *sb = buf_b; - int lb; - if (LIKELY(a.type_ == VAR_STRING)) { - sa = STR_DATA(a.data_.s); - la = STR_SIZE(a.data_.s); - } else if (a.type_ == VAR_STRINGID) { - VarString *vs = (VarString *) a.data_.i; - sa = STR_DATA(vs); - la = STR_SIZE(vs); - } else if (a.type_ == VAR_INT) { - la = FlFastItoA(a.data_.i, buf_a); - } else if (a.type_ == VAR_FLOAT) { - la = FlVarToStr(a, buf_a, sizeof(buf_a) FL_HINT_PASS); - } else { - FL_RAISE_HINT(_S, "attempt to concatenate a non-string value"); - return (CVar) {VAR_NIL}; - } - - if (LIKELY(b.type_ == VAR_STRING)) { - sb = STR_DATA(b.data_.s); - lb = STR_SIZE(b.data_.s); - } else if (b.type_ == VAR_STRINGID) { - VarString *vs = (VarString *) b.data_.i; - sb = STR_DATA(vs); - lb = STR_SIZE(vs); - } else if (b.type_ == VAR_INT) { - lb = FlFastItoA(b.data_.i, buf_b); - } else if (b.type_ == VAR_FLOAT) { - lb = FlVarToStr(b, buf_b, sizeof(buf_b) FL_HINT_PASS); - } else { - FL_RAISE_HINT(_S, "attempt to concatenate a non-string value"); - return (CVar) {VAR_NIL}; - } - - int total = la + lb; - if (UNLIKELY(total < la || total < lb)) { - FL_RAISE_HINT(_S, "string concatenation result too long"); - } - VarString *vs = (VarString *) FlFastAlloc(_S, sizeof(VarString) + total, FL_IN_CONST_INIT); - vs->size_ = total; - vs->hash_ = 0; - memcpy(vs->data_, sa, la); - memcpy(vs->data_ + la, sb, lb); - CVar result; - result.type_ = VAR_STRING; - result.flag_ = 0; - result.data_.s = vs; - return result; -} -#define FlConcat(...) FlConcatFn(__VA_ARGS__ FL_HINT_ARGS) - -// string.char(c) 单字节快路径 -static inline CVar FlStringChar1Fn(int64_t c FL_HINT_PARAMS) { - if (UNLIKELY(c < 0 || c > 255)) { - FL_RAISE_HINT(_S, "bad argument to string.char: value out of range"); - } - VarString *vs = (VarString *) FlFastAlloc(_S, sizeof(VarString) + 1, FL_IN_CONST_INIT); - vs->size_ = 1; - vs->hash_ = 0; - vs->data_[0] = (char) c; - CVar r; - r.type_ = VAR_STRING; - r.flag_ = 0; - r.data_.s = vs; - return r; -} -#define FlStringChar1(...) FlStringChar1Fn(__VA_ARGS__ FL_HINT_ARGS) - -// string.lower / upper:ASCII 单遍 + 一次 arena 分配 -static inline CVar FlStringLower(CVar s) { - const char *src = 0; - int len = 0; - if (LIKELY(s.type_ == VAR_STRING)) { - src = STR_DATA(s.data_.s); - len = STR_SIZE(s.data_.s); - } else if (s.type_ == VAR_STRINGID) { - VarString *vs = (VarString *) s.data_.i; - src = STR_DATA(vs); - len = STR_SIZE(vs); - } else { - return FakeluaCallByName(_S, FAKELUA_JIT_TYPE, "string.lower", 1, s); - } - VarString *out = (VarString *) FlFastAlloc(_S, sizeof(VarString) + len, FL_IN_CONST_INIT); - out->size_ = len; - out->hash_ = 0; - for (int i = 0; i < len; ++i) { - unsigned char c = (unsigned char) src[i]; - out->data_[i] = (c >= 'A' && c <= 'Z') ? (char) (c + 32) : (char) c; - } - CVar r; - r.type_ = VAR_STRING; - r.flag_ = 0; - r.data_.s = out; - return r; -} - -static inline CVar FlStringUpper(CVar s) { - const char *src = 0; - int len = 0; - if (LIKELY(s.type_ == VAR_STRING)) { - src = STR_DATA(s.data_.s); - len = STR_SIZE(s.data_.s); - } else if (s.type_ == VAR_STRINGID) { - VarString *vs = (VarString *) s.data_.i; - src = STR_DATA(vs); - len = STR_SIZE(vs); - } else { - return FakeluaCallByName(_S, FAKELUA_JIT_TYPE, "string.upper", 1, s); - } - VarString *out = (VarString *) FlFastAlloc(_S, sizeof(VarString) + len, FL_IN_CONST_INIT); - out->size_ = len; - out->hash_ = 0; - for (int i = 0; i < len; ++i) { - unsigned char c = (unsigned char) src[i]; - out->data_[i] = (c >= 'a' && c <= 'z') ? (char) (c - 32) : (char) c; - } - CVar r; - r.type_ = VAR_STRING; - r.flag_ = 0; - r.data_.s = out; - return r; -} - -// string.byte(s, i) 单位置;越界返回 nil -static inline CVar FlStringByte1(CVar s, int64_t i) { - const char *src = 0; - int len = 0; - if (LIKELY(s.type_ == VAR_STRING)) { - src = STR_DATA(s.data_.s); - len = STR_SIZE(s.data_.s); - } else if (s.type_ == VAR_STRINGID) { - VarString *vs = (VarString *) s.data_.i; - src = STR_DATA(vs); - len = STR_SIZE(vs); - } else { - return FakeluaCallByName(_S, FAKELUA_JIT_TYPE, "string.byte", 2, s, (CVar) {.type_ = VAR_INT, .data_.i = i}); - } - if (i < 0) i = len + i + 1; - if (UNLIKELY(i < 1 || i > len)) return FlAllocMulti(_S, 0); - return (CVar) {.type_ = VAR_INT, .data_.i = (unsigned char) src[i - 1]}; -} - -// string.rep(s, n):s 重复 n 次(无分隔符,最常见形式) -static inline CVar FlStringRep(CVar s, int64_t n) { - const char *src = 0; - int len = 0; - if (LIKELY(s.type_ == VAR_STRING)) { - src = STR_DATA(s.data_.s); - len = STR_SIZE(s.data_.s); - } else if (s.type_ == VAR_STRINGID) { - VarString *vs = (VarString *) s.data_.i; - src = STR_DATA(vs); - len = STR_SIZE(vs); - } else { - return FakeluaCallByName(_S, FAKELUA_JIT_TYPE, "string.rep", 2, s, (CVar) {.type_ = VAR_INT, .data_.i = n}); - } - if (n <= 0 || len == 0) { - VarString *vs = (VarString *) FlFastAlloc(_S, sizeof(VarString) + 1, FL_IN_CONST_INIT); - vs->size_ = 0; - vs->hash_ = 0; - vs->data_[0] = '\0'; - CVar r; - r.type_ = VAR_STRING; - r.flag_ = 0; - r.data_.s = vs; - return r; - } - // 防溢出:总长度超过 1G 回退 native。(n, len) 均转为 uint64_t 避免截断 - if ((uint64_t) n > (1073741824ULL / (uint64_t) len)) { - return FakeluaCallByName(_S, FAKELUA_JIT_TYPE, "string.rep", 2, s, (CVar) {.type_ = VAR_INT, .data_.i = n}); - } - int64_t total = (int64_t) len * n; - VarString *out = (VarString *) FlFastAlloc(_S, sizeof(VarString) + total, FL_IN_CONST_INIT); - out->size_ = total; - out->hash_ = 0; - char *dst = out->data_; - for (int64_t i = 0; i < n; ++i) { - memcpy(dst, src, len); - dst += len; - } - CVar r; - r.type_ = VAR_STRING; - r.flag_ = 0; - r.data_.s = out; - return r; -} - -// string.reverse(s):字符串反转 -static inline CVar FlStringReverse(CVar s) { - const char *src = 0; - int len = 0; - if (LIKELY(s.type_ == VAR_STRING)) { - src = STR_DATA(s.data_.s); - len = STR_SIZE(s.data_.s); - } else if (s.type_ == VAR_STRINGID) { - VarString *vs = (VarString *) s.data_.i; - src = STR_DATA(vs); - len = STR_SIZE(vs); - } else { - return FakeluaCallByName(_S, FAKELUA_JIT_TYPE, "string.reverse", 1, s); - } - VarString *out = (VarString *) FlFastAlloc(_S, sizeof(VarString) + len, FL_IN_CONST_INIT); - out->size_ = len; - out->hash_ = 0; - for (int i = 0; i < len; ++i) { - out->data_[i] = src[len - 1 - i]; - } - CVar r; - r.type_ = VAR_STRING; - r.flag_ = 0; - r.data_.s = out; - return r; -} - -// string.find(s, pattern, 1, true) 纯文本子串搜索(plain=true,从位置 1 开始) -static inline CVar FlStringFindPlain(CVar s, CVar pat) { - const char *src = 0; - int slen = 0; - if (LIKELY(s.type_ == VAR_STRING)) { - src = STR_DATA(s.data_.s); - slen = STR_SIZE(s.data_.s); - } else if (s.type_ == VAR_STRINGID) { - VarString *vs = (VarString *) s.data_.i; - src = STR_DATA(vs); - slen = STR_SIZE(vs); - } else { - return FakeluaCallByName(_S, FAKELUA_JIT_TYPE, "string.find", 4, s, pat, (CVar) {.type_ = VAR_INT, .data_.i = 1}, (CVar) {.type_ = VAR_BOOL, .data_.b = true}); - } - const char *psrc = 0; - int plen = 0; - if (LIKELY(pat.type_ == VAR_STRING)) { - psrc = STR_DATA(pat.data_.s); - plen = STR_SIZE(pat.data_.s); - } else if (pat.type_ == VAR_STRINGID) { - VarString *vs = (VarString *) pat.data_.i; - psrc = STR_DATA(vs); - plen = STR_SIZE(vs); - } else { - return FakeluaCallByName(_S, FAKELUA_JIT_TYPE, "string.find", 4, s, pat, (CVar) {.type_ = VAR_INT, .data_.i = 1}, (CVar) {.type_ = VAR_BOOL, .data_.b = true}); - } - if (plen == 0) return (CVar) {.type_ = VAR_INT, .data_.i = 1}; - if (plen > slen) return (CVar) {VAR_NIL}; - int limit = slen - plen; - for (int i = 0; i <= limit; ++i) { - if (src[i] == psrc[0] && memcmp(src + i, psrc, plen) == 0) { - return (CVar) {.type_ = VAR_INT, .data_.i = i + 1}; - } - } - return (CVar) {VAR_NIL}; -} - -// string.format("%d", int) -static inline CVar FlFormatInt(int64_t v) { - char buf[32]; - int n = FlFastItoA(v, buf); - VarString *vs = (VarString *) FlFastAlloc(_S, sizeof(VarString) + n, FL_IN_CONST_INIT); - vs->size_ = n; - vs->hash_ = 0; - memcpy(vs->data_, buf, (size_t) n); - CVar r; - r.type_ = VAR_STRING; - r.flag_ = 0; - r.data_.s = vs; - return r; -} - -// select("#", multi) / select(i, multi) 的内联辅助。 -// 空 Multi(含空指针)是 0 个值。编译器把没有实参的 ... 传成 nil,所以 nil 也是 0。 -// 其它单个值是 1 个值。 -static inline CVar FlSelectHash(CVar vararg) { - if (vararg.type_ == VAR_MULTI) { - if (vararg.data_.m == NULL) return (CVar) {.type_ = VAR_INT, .data_.i = 0}; - return (CVar) {.type_ = VAR_INT, .data_.i = (int64_t) vararg.data_.m->count}; - } - if (vararg.type_ == VAR_NIL) return (CVar) {.type_ = VAR_INT, .data_.i = 0}; - return (CVar) {.type_ = VAR_INT, .data_.i = 1}; -} - -static inline CVar FlSelectIndexFn(int64_t idx, CVar vararg FL_HINT_PARAMS) { - if (vararg.type_ == VAR_MULTI) { - VarMulti *m = vararg.data_.m; - int64_t count = (m != NULL) ? (int64_t) m->count : 0; - if (idx < 0) { - if (idx < -count - 1) FL_RAISE_HINT(_S, "bad argument #1 to 'select': index out of range"); - idx = count + idx + 1; - } - if (idx < 1) FL_RAISE_HINT(_S, "bad argument #1 to 'select': index out of range"); - if (idx > count || m == NULL) return FlAllocMulti(_S, 0); - uint32_t start = (uint32_t) (idx - 1); - uint32_t nret = m->count - start; - if (nret == 1) return m->vars[start]; - return FlSliceMulti(_S, vararg, start); - } - // 没有实参的 ... 传进来是 nil,和空 Multi 一样是 0 个值。 - // 正下标返回空,0 和负数都越界。Multi 在上面已经返回,这里碰不到热路径。 - if (vararg.type_ == VAR_NIL) { - if (idx < 1) FL_RAISE_HINT(_S, "bad argument #1 to 'select': index out of range"); - return FlAllocMulti(_S, 0); - } - /* 其它单个实参是 1 个值。越界下标报错;超过末尾返回 0 个值。 */ - if (idx < 0) { - if (idx < -2) FL_RAISE_HINT(_S, "bad argument #1 to 'select': index out of range"); - idx = idx + 2; - } - if (idx < 1) FL_RAISE_HINT(_S, "bad argument #1 to 'select': index out of range"); - if (idx > 1) return FlAllocMulti(_S, 0); - return vararg; -} -#define FlSelectIndex(...) FlSelectIndexFn(__VA_ARGS__ FL_HINT_ARGS) - -// tonumber 热路径:数字直通;纯十进制整数字符串本地解析;其余回退 native -static inline int FlIsSpaceByte(unsigned char c) { - return c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '\f' || c == '\v'; -} - -static inline CVar FlTonumber(CVar v) { - if (LIKELY(v.type_ == VAR_INT || v.type_ == VAR_FLOAT)) return v; - const char *p = 0; - int len = 0; - if (LIKELY(v.type_ == VAR_STRING)) { - p = STR_DATA(v.data_.s); - len = STR_SIZE(v.data_.s); - } else if (v.type_ == VAR_STRINGID) { - VarString *vs = (VarString *) v.data_.i; - p = STR_DATA(vs); - len = STR_SIZE(vs); - } else { - return (CVar) {VAR_NIL}; - } - while (len > 0 && FlIsSpaceByte((unsigned char) p[0])) { - ++p; - --len; - } - while (len > 0 && FlIsSpaceByte((unsigned char) p[len - 1])) { - --len; - } - if (len <= 0) return (CVar) {VAR_NIL}; - // 0x 十六进制等复杂形式走 native - if (len >= 2 && p[0] == '0' && (p[1] == 'x' || p[1] == 'X')) { - return FakeluaCallByName(_S, FAKELUA_JIT_TYPE, "tonumber", 1, v); - } - if (len >= 3 && (p[0] == '+' || p[0] == '-') && p[1] == '0' && (p[2] == 'x' || p[2] == 'X')) { - return FakeluaCallByName(_S, FAKELUA_JIT_TYPE, "tonumber", 1, v); - } - int neg = 0; - int i = 0; - if (p[0] == '+') { - i = 1; - } else if (p[0] == '-') { - neg = 1; - i = 1; - } - if (i >= len) return (CVar) {VAR_NIL}; - // 含小数点/指数 → native - { - int j; - for (j = i; j < len; ++j) { - unsigned char c = (unsigned char) p[j]; - if (c == '.' || c == 'e' || c == 'E') { - return FakeluaCallByName(_S, FAKELUA_JIT_TYPE, "tonumber", 1, v); - } - if (c < '0' || c > '9') return (CVar) {VAR_NIL}; - } - } - int64_t val = 0; - for (; i < len; ++i) { - // 溢出检查:val * 10 + digit > INT64_MAX 时回退到 native(返回 float) - // 阈值: (INT64_MAX - digit) / 10,等价于 val > (INT64_MAX - digit) / 10 - int digit = p[i] - '0'; - if (val > (INT64_MAX - digit) / 10) { - return FakeluaCallByName(_S, FAKELUA_JIT_TYPE, "tonumber", 1, v); - } - val = val * 10 + digit; - } - if (neg) val = -val; - return (CVar) {.type_ = VAR_INT, .data_.i = val}; -} - -// 空表预扩到至少 need 个桶(反复 FlTableRehash,空表拷贝成本可忽略) -static inline void FlEnsureTableBuckets(VarTable *tbl, uint32_t need) { - if (need < 1) need = 1; - uint32_t target = need - 1; - target |= target >> 1; - target |= target >> 2; - target |= target >> 4; - target |= target >> 8; - target |= target >> 16; - target += 1; - while (tbl->bucket_count_ < target) { - FlTableRehash(tbl); - if (UNLIKELY(tbl->bucket_count_ == 0)) break; - } -} - -// 空表预扩容的节点数组必须能一次分配出来(与 native_table.cpp 的 MovePresizeFits 一致)。 -static inline int FlMovePresizeFits(uint64_t count) { - if (count == 0 || count > 0xFFFFFFFFULL) return 0; - uint32_t min_buckets = (uint32_t) count; - uint32_t buckets = min_buckets - 1; - buckets |= buckets >> 1; - buckets |= buckets >> 2; - buckets |= buckets >> 4; - buckets |= buckets >> 8; - buckets |= buckets >> 16; - buckets += 1; - if (buckets < min_buckets) return 0; - uint64_t total = (uint64_t) buckets + (uint64_t) (buckets / 2); - uint64_t bytes = total * sizeof(TableNode) + total * sizeof(uint32_t); - return bytes <= (1024ULL * 1024ULL); -} - -// table.move 内联实现(完整对齐 Lua 5.4 边界/回绕检查与同表重叠后向拷贝) -static inline CVar FlTableMoveFn(CVar src, int64_t f, int64_t e, int64_t t, CVar dst FL_HINT_PARAMS) { - if (dst.type_ == VAR_NIL) dst = src; - if (UNLIKELY(src.type_ != VAR_TABLE || !src.data_.t || dst.type_ != VAR_TABLE || !dst.data_.t)) { - FL_RAISE_HINT(_S, "bad argument to 'table.move' (table expected)"); - } - FL_CHECK_CONST_TABLE_HINT(dst); - if (e >= f) { - uint64_t count = (uint64_t) e - (uint64_t) f + 1; - if (UNLIKELY(count == 0 || count > (uint64_t) INT64_MAX)) { - FL_RAISE_HINT(_S, "bad argument #3 to 'table.move' (too many elements to move)"); - } - uint64_t last_off = count - 1; - if (UNLIKELY(t > 0 && (uint64_t) t > (uint64_t) INT64_MAX - last_off)) { - FL_RAISE_HINT(_S, "bad argument #4 to 'table.move' (destination wrap around)"); - } - int64_t icount = (int64_t) count; - int same = (src.data_.t == dst.data_.t); - if (!same && dst.data_.t->count_ == 0 && icount > 8 && t != (int64_t) dst.data_.t->arr_size_ + 1 && FlMovePresizeFits(count)) { - FlEnsureTableBuckets(dst.data_.t, (uint32_t) icount); - } - if (!same || t <= f || t > e) { - int64_t i; - for (i = 0; i < icount; ++i) { - CVar val = FlGetTableIntFn(src, (int64_t) ((uint64_t) f + (uint64_t) i) FL_HINT_PASS); - FlSetTableIntFn(dst, (int64_t) ((uint64_t) t + (uint64_t) i), val FL_HINT_PASS); - } - } else { - int64_t i; - for (i = icount - 1; i >= 0; --i) { - CVar val = FlGetTableIntFn(src, (int64_t) ((uint64_t) f + (uint64_t) i) FL_HINT_PASS); - FlSetTableIntFn(dst, (int64_t) ((uint64_t) t + (uint64_t) i), val FL_HINT_PASS); - } - } - } - return dst; -} -#define FlTableMove(...) FlTableMoveFn(__VA_ARGS__ FL_HINT_ARGS) - -// table.unpack 内联实现(完整对齐 Lua 5.4 区间回绕与单块分配上限检查) -static inline CVar FlTableUnpackFn(CVar tbl, int64_t start_i, int64_t end_j FL_HINT_PARAMS) { - if (start_i > end_j) return FlAllocMulti(_S, 0); - uint64_t count64 = (uint64_t) end_j - (uint64_t) start_i + 1; - uint64_t max_unpack = (1024ULL * 1024ULL - sizeof(VarMulti)) / sizeof(CVar); - if (UNLIKELY(count64 == 0 || count64 > 2147483647ULL || count64 > max_unpack)) { - FL_RAISE_HINT(_S, "too many results to unpack"); - } - uint32_t count = (uint32_t) count64; - CVar multi = FlAllocMulti(_S, count); - uint32_t i; - for (i = 0; i < count; ++i) { - multi.data_.m->vars[i] = FlGetTableIntFn(tbl, (int64_t) ((uint64_t) start_i + (uint64_t) i) FL_HINT_PASS); - } - return multi; -} -#define FlTableUnpack(...) FlTableUnpackFn(__VA_ARGS__ FL_HINT_ARGS) - -#define GET_TABLE_ENTRY(tbl, idx, k, v) \ - do { \ - VarTable *__gt = (tbl).data_.t; \ - uint32_t __gi = (idx); \ - uint32_t __spec_cnt = __gt->spec_count; \ - if (LIKELY(__gi < __spec_cnt)) { \ - (k) = __gt->spec_keys[__gi]; \ - (v) = __gt->spec_vals[__gi]; \ - } else { \ - __gi -= __spec_cnt; \ - if (__gi < __gt->arr_size_) { \ - (k).type_ = VAR_INT; \ - (k).flag_ = 0; \ - (k).data_.i = (int64_t) __gi + 1; \ - (v) = __gt->arr_[__gi]; \ - } else { \ - __gi -= __gt->arr_size_; \ - if (UNLIKELY(__gi >= __gt->count_)) { \ - (k).type_ = VAR_NIL; \ - (v).type_ = VAR_NIL; \ - } else if (LIKELY(__gt->bucket_count_ == 0)) { \ - (k) = __gt->quick_data_[__gi].key; \ - (v) = __gt->quick_data_[__gi].val; \ - } else { \ - uint32_t __gti_node_idx = __gt->active_list_[__gi]; \ - (k) = __gt->nodes_[__gti_node_idx].entry.key; \ - (v) = __gt->nodes_[__gti_node_idx].entry.val; \ - } \ - } \ - } \ - } while (0) - -#define FlFloorDivQuotient(a, b, q) \ - do { \ - if (UNLIKELY((b) == (int64_t) -1)) { \ - (q) = (int64_t) ((uint64_t) 0 - (uint64_t) (int64_t) (a)); \ - } else { \ - (q) = (a) / (b); \ - if (((a) ^ (b)) < 0 && (a) % (b) != 0) { \ - (q) -= 1; \ - } \ - } \ - } while (0) - -#define FlFloorDivInt(a, b, result) \ - do { \ - int64_t __fl_a = (a); \ - int64_t __fl_b = (b); \ - if (UNLIKELY(__fl_b == 0)) { \ - FL_RAISE(_S, "floor division by zero"); \ - } \ - int64_t __fl_q; \ - FlFloorDivQuotient(__fl_a, __fl_b, __fl_q); \ - (result) = __fl_q; \ - } while (0) - -#define FlModInt(a, b, result) \ - do { \ - int64_t __fm_a = (a); \ - int64_t __fm_b = (b); \ - if (UNLIKELY(__fm_b == 0)) { \ - FL_RAISE(_S, "modulo by zero"); \ - } \ - int64_t __fm_r = UNLIKELY(__fm_b == (int64_t) -1) ? 0 : (__fm_a % __fm_b); \ - if (__fm_r != 0 && ((__fm_r < 0) != (__fm_b < 0))) { \ - __fm_r += __fm_b; \ - } \ - (result) = __fm_r; \ - } while (0) - -#define FlModFloat(a, b, result) \ - do { \ - double __fmf_a = (a); \ - double __fmf_b = (b); \ - (result) = __fmf_a - __fmf_b * floor(__fmf_a / __fmf_b); \ - } while (0) - -// Lua 5.4 pushnumint:d 能无损落进 int64 时返回 integer,否则保留 float -// (inf/-inf/nan/超出 [-2^63, 2^63) 均不报错,仍是 float)。 -static inline CVar FlPushNumInt(double d) { - CVar r; - if (isfinite(d) && d >= -9223372036854775808.0 && d < 9223372036854775808.0) { - r.type_ = VAR_INT; - r.data_.i = (int64_t) d; - } else { - r.type_ = VAR_FLOAT; - r.data_.f = d; - } - return r; -} - -static inline CVar FlMathFloor(CVar v) { - if (v.type_ == VAR_INT) return v; - return FlPushNumInt(floor((double) v.data_.f)); -} - -static inline CVar FlMathCeil(CVar v) { - if (v.type_ == VAR_INT) return v; - return FlPushNumInt(ceil((double) v.data_.f)); -} - -// Lua 5.4 math.fmod:两整数走整数取模(0 报错、-1 特判为 0 避开溢出), -// 任一浮点恒返回 float(浮点除零是 NaN,不报错)。 -static inline CVar FlMathFmodFn(CVar a, CVar b FL_HINT_PARAMS) { - CVar r; - if (a.type_ == VAR_INT && b.type_ == VAR_INT) { - int64_t d = b.data_.i; - if (d == 0) { - // 错误串结尾不能紧贴右圆括号:本头文件整体以 R 圆括号原始串内嵌进 c_gen, - // 右圆括号紧跟双引号会提前终止原始串。 - FL_RAISE_HINT(_S, "bad argument #2 to 'fmod': zero divisor"); - } - r.type_ = VAR_INT; - r.data_.i = (d == -1) ? 0 : a.data_.i % d; - return r; - } - r.type_ = VAR_FLOAT; - r.data_.f = fmod(CVAR_TO_DOUBLE(a), CVAR_TO_DOUBLE(b)); - return r; -} -#define FlMathFmod(...) FlMathFmodFn(__VA_ARGS__ FL_HINT_ARGS) - -// Lua 5.4 math.modf:整数部分向零取整并按 pushnumint 决定 int/float, -// 无小数部分(整数值或 inf)时小数部分为 +0.0。 -static inline CVar FlMathModf(CVar v) { - double n; - double ip; - double fp; - CVar r; - r = FlAllocMulti(_S, 2); - if (v.type_ == VAR_INT) { - r.data_.m->vars[0] = v; - r.data_.m->vars[1] = (CVar) {.type_ = VAR_FLOAT, .data_.f = 0.0}; - return r; - } - n = (double) v.data_.f; - ip = (n < 0) ? ceil(n) : floor(n); - fp = (n == ip) ? 0.0 : n - ip; - r.data_.m->vars[0] = FlPushNumInt(ip); - r.data_.m->vars[1] = (CVar) {.type_ = VAR_FLOAT, .data_.f = fp}; - return r; -} - -static inline double FlMathLdexp(double value, int64_t exponent) { - if (exponent > 2147483647LL) exponent = 2147483647LL; - if (exponent < (-2147483647LL - 1)) exponent = (-2147483647LL - 1); - return ldexp(value, (int) exponent); -} - -static inline int64_t FlRandomInt64(void) { - uint64_t value = 0; - for (int i = 0; i < 5; ++i) { - value = (value << 15) | (uint64_t) (rand() & 0x7fff); - } - return (int64_t) value; -} - -// 整数 for 步进:用无符号加法探测有符号溢出。溢出返回 0 且不改 *ctrl -//(避免 maxinteger++ 变成 mininteger 后死循环)。continue 仍走 for 的 incr 子句。 -static inline int FlForIntAdvance(int64_t *ctrl, int64_t step) { - int64_t cur = *ctrl; - int64_t next = (int64_t) ((uint64_t) cur + (uint64_t) step); - if ((cur ^ step) >= 0 && (next ^ cur) < 0) return 0; - *ctrl = next; - return 1; -} - -// Lua 5.4 forlimit:无法落入 int64 时按符号裁成 min/max integer;NaN 与负无穷同样处理。 -// 返回 1 表示整段跳过。 -static inline int FlForLimitToInt(double flim, int64_t step, int64_t *out) { - int64_t p = 0; - if (FlDoubleFitsInt64(flim, &p)) { - *out = p; - return 0; - } - if (!isfinite(flim)) { - if (flim > 0.0) { - if (step < 0) return 1; - *out = INT64_MAX; - return 0; - } - if (step > 0) return 1; - *out = INT64_MIN; - return 0; - } - if (flim >= FL_INT64_FLOAT_EXCL) { - if (step < 0) return 1; - *out = INT64_MAX; - return 0; - } - if (flim < (double) INT64_MIN) { - if (step > 0) return 1; - *out = INT64_MIN; - return 0; - } - *out = step > 0 ? (int64_t) floor(flim) : (int64_t) ceil(flim); - return 0; -} - -#define FlToIntChecked(v, result) \ - do { \ - if (!FlDoubleFitsInt64((double) (v), &(result))) { \ - FL_RAISE(_S, "number has no integer representation"); \ - } \ - } while (0) - -#define FlShiftIntImpl(a, b, result, _right) \ - do { \ - int64_t __sh_a = (a); \ - int64_t __sh_b = (b); \ - if (UNLIKELY(__sh_b >= 64 || __sh_b <= -64)) { \ - (result) = (int64_t) 0; \ - } else if (__sh_b >= 0) { \ - (result) = (int64_t) ((_right) ? ((uint64_t) __sh_a >> __sh_b) : ((uint64_t) __sh_a << __sh_b)); \ - } else { \ - (result) = (int64_t) ((_right) ? ((uint64_t) __sh_a << (-__sh_b)) : ((uint64_t) __sh_a >> (-__sh_b))); \ - } \ - } while (0) - -#define FlLShiftInt(a, b, result) FlShiftIntImpl(a, b, result, 0) - -#define FlRShiftInt(a, b, result) FlShiftIntImpl(a, b, result, 1) - -#define FlLenInt(v, result) \ - do { \ - CVar __fl_v = (v); \ - if (LIKELY(__fl_v.type_ == VAR_STRING)) { \ - (result) = (int64_t) STR_SIZE(__fl_v.data_.s); \ - } else if (UNLIKELY(__fl_v.type_ == VAR_STRINGID)) { \ - (result) = (int64_t) STR_SIZE((VarString *) __fl_v.data_.i); \ - } else if (UNLIKELY(__fl_v.type_ == VAR_TABLE)) { \ - (result) = (int64_t) FlGetTableSeqLen(__fl_v.data_.t); \ - } else { \ - FL_RAISE(_S, "attempt to get length of a non-string/table value"); \ - (result) = 0; \ - } \ - } while (0) - -// 获取本 State 的日志级别(由 C++ 侧初始化和设置) -// 返回值: 0=Trace, 1=Debug, 2=Info, 3=Warn, 4=Error, 5=Critical, 6=Off -// 使用函数而非全局变量,避免 Windows DLL 导入需要 __declspec(dllimport) 的问题 -extern int GetLogLevel(State *s); - -// C++ 日志函数声明(供宏调用) -void FakeluaLogLua(State *s, int level, CVar msg, const char *file, int line, const char *fname); - -// 日志级别常量(与 C++ LogLevel 枚举一致) -enum { - FAKELUA_LOGLEVEL_TRACE = 0, - FAKELUA_LOGLEVEL_DEBUG = 1, - FAKELUA_LOGLEVEL_INFO = 2, - FAKELUA_LOGLEVEL_WARN = 3, - FAKELUA_LOGLEVEL_ERROR = 4, - FAKELUA_LOGLEVEL_CRITICAL = 5, - FAKELUA_LOGLEVEL_OFF = 6, -}; - -// 日志级别检查宏:先检查级别,只有启用时才调用 C++ 函数 -// 这样 log.debug(expensive_func()) 在级别禁用时完全不会执行 expensive_func() -#define FAKELUA_LOG_CHECK(level) ((level) >= GetLogLevel(_S)) - -// 日志宏:级别检查 + 调用 C++ 日志函数 -// 参数 msg 只在级别启用时才会被求值 -#define FAKELUA_LOG_TRACE(msg, file, line, fname) \ - do { \ - if (FAKELUA_LOG_CHECK(FAKELUA_LOGLEVEL_TRACE)) FakeluaLogLua(_S, FAKELUA_LOGLEVEL_TRACE, msg, file, line, fname); \ - } while (0) -#define FAKELUA_LOG_DEBUG(msg, file, line, fname) \ - do { \ - if (FAKELUA_LOG_CHECK(FAKELUA_LOGLEVEL_DEBUG)) FakeluaLogLua(_S, FAKELUA_LOGLEVEL_DEBUG, msg, file, line, fname); \ - } while (0) -#define FAKELUA_LOG_INFO(msg, file, line, fname) \ - do { \ - if (FAKELUA_LOG_CHECK(FAKELUA_LOGLEVEL_INFO)) FakeluaLogLua(_S, FAKELUA_LOGLEVEL_INFO, msg, file, line, fname); \ - } while (0) -#define FAKELUA_LOG_WARN(msg, file, line, fname) \ - do { \ - if (FAKELUA_LOG_CHECK(FAKELUA_LOGLEVEL_WARN)) FakeluaLogLua(_S, FAKELUA_LOGLEVEL_WARN, msg, file, line, fname); \ - } while (0) -#define FAKELUA_LOG_ERROR(msg, file, line, fname) \ - do { \ - if (FAKELUA_LOG_CHECK(FAKELUA_LOGLEVEL_ERROR)) FakeluaLogLua(_S, FAKELUA_LOGLEVEL_ERROR, msg, file, line, fname); \ - } while (0) -#define FAKELUA_LOG_CRITICAL(msg, file, line, fname) \ - do { \ - if (FAKELUA_LOG_CHECK(FAKELUA_LOGLEVEL_CRITICAL)) FakeluaLogLua(_S, FAKELUA_LOGLEVEL_CRITICAL, msg, file, line, fname); \ - } while (0) -)FL_RUNTIME"; +#include "c_runtime_embed.inc" }// namespace fakelua diff --git a/src/interp/codegen.cpp b/src/interp/codegen.cpp index 777450f2..2a8b1f97 100644 --- a/src/interp/codegen.cpp +++ b/src/interp/codegen.cpp @@ -758,6 +758,10 @@ void InterpCodegen::CompileFunction(FuncInfo *func) { Emit(Op::RETURN, r, 1); } + if (!check_only_ && func->proto) { + func->proto->call_cache.assign(func->proto->code.size(), {}); + } + check_ip_ = prev_check_ip; cur_func_ = prev_func; cur_proto_ = prev_proto; diff --git a/src/interp/func_proto.h b/src/interp/func_proto.h index 9c5c16e6..e4babea5 100644 --- a/src/interp/func_proto.h +++ b/src/interp/func_proto.h @@ -41,6 +41,13 @@ struct InterpUnit final : JITHandle { std::string file_name; }; +// 命名调用点缓存。vm_version 与 Vm::VersionPtr 一致时 proto 仍然有效; +// RegisterFunction 改变版本号后下一次调用重新解析。proto 为空表示目标不是解释器原型。 +struct InterpCallCache { + FuncProto *proto = nullptr; + uint64_t vm_version = 0; +}; + struct FuncProto { InterpUnit *unit = nullptr; std::string name; @@ -57,6 +64,8 @@ struct FuncProto { std::vector child_protos; // 与 code 一一对应的 Lua 源码行号,0 表示未知(运行期报错时据此定位)。 std::vector lineinfo; + // 与 code 等长。CALLNAME / TAILNAME 按指令下标缓存解析结果,避免每次调用查哈希表。 + std::vector call_cache; }; inline const char *const kInterpClosureMagic = reinterpret_cast(static_cast(1)); diff --git a/src/interp/interpreter.cpp b/src/interp/interpreter.cpp index 0b569ba8..e8b5a83d 100644 --- a/src/interp/interpreter.cpp +++ b/src/interp/interpreter.cpp @@ -31,6 +31,49 @@ std::string_view ConstToName(const CVar &c) { return {}; } +struct NamedCallTarget { + FuncProto *proto = nullptr; + bool direct = false; +}; + +// 文件级命名调用的调用点缓存。命中且实参个数精确、非变参、末参不是 Multi 时 direct 为真, +// 调用方直接进入解释器帧,不再查 local_funcs / VmFunction。 +NamedCallTarget ResolveNamedCall(State *s, FuncProto *caller, const Inst *ip_after, const Inst &inst, int nargs, const CVar *args) { + NamedCallTarget out; + if (caller == nullptr || ip_after == nullptr || caller->code.empty() || caller->call_cache.empty()) { + return out; + } + const ptrdiff_t off = ip_after - caller->code.data() - 1; + if (off < 0 || static_cast(off) >= caller->call_cache.size()) { + return out; + } + InterpCallCache &slot = caller->call_cache[static_cast(off)]; + const uint64_t ver = *s->GetVM().VersionPtr(); + if (slot.vm_version != ver) { + const std::string_view name = ConstToName(caller->constants[static_cast(inst.sbx)]); + FuncProto *found = nullptr; + if ((inst.c & kCallLocalUnit) != 0 && caller->unit != nullptr) { + if (const auto it = caller->unit->local_funcs.find(name); it != caller->unit->local_funcs.end() && it->second) { + found = it->second; + } + } + if (found == nullptr) { + if (const VmFunction *fn = s->GetVM().FindFunction(name)) { + if (void *addr = fn->GetAddr(JIT_INTERP)) { + found = AsInterpProto(addr); + } + } + } + slot.proto = found; + slot.vm_version = ver; + } + out.proto = slot.proto; + const bool last_multi = nargs > 0 && args != nullptr && args[nargs - 1].type_ == static_cast(VarType::Multi); + out.direct = out.proto != nullptr && !out.proto->is_vararg && !last_multi && nargs == out.proto->param_count && + nargs <= static_cast(kMaxFunctionInputParams); + return out; +} + CVar LookupGlobal(FuncProto *proto, std::string_view name) { if (name == "_VERSION") { CVar v{}; @@ -395,6 +438,9 @@ bool InterpDoTailName(State *s, FuncProto *&proto, VarClosure *&cl, CVar *const for (int i = 0; i < nargs; ++i) { raw[i] = interp_rt::VmSlot(stk, boxp, inst.a + i); } + if (const NamedCallTarget hit = ResolveNamedCall(s, proto, ip, inst, nargs, raw); hit.direct) { + return ApplyInterpTail(s, proto, cl, stk, boxp, saved, ip, hit.proto, nullptr, raw, nargs, ret); + } if ((inst.c & kCallLocalUnit) != 0 && proto->unit) { if (const auto it = proto->unit->local_funcs.find(name); it != proto->unit->local_funcs.end() && it->second) { FuncProto *callee = it->second; @@ -843,26 +889,27 @@ CVar InterpreterExecuteT(State *s, FuncProto *proto, const CVar *args, int arg_c // std::string;报错走 std::format 的 string_view,仅在 CallByNameImpl 的失败路径 // 才可能分配。 { - const std::string_view name = ConstToName(proto->constants[static_cast(inst.sbx)]); CVar call_args[kMaxFunctionInputParams]; const int nargs = inst.b; if (nargs < 0 || nargs > static_cast(kMaxFunctionInputParams)) { + const std::string_view name = ConstToName(proto->constants[static_cast(inst.sbx)]); ThrowFakeluaException(std::format("interpreter: too many arguments ({}) passed for function '{}', max is {}", nargs, name, kMaxFunctionInputParams)); } for (int i = 0; i < nargs; ++i) { call_args[i] = VM_R(inst.a + i); } CVar ret; - bool handled = false; - if ((inst.c & kCallLocalUnit) != 0 && proto->unit) { - if (const auto it = proto->unit->local_funcs.find(name); it != proto->unit->local_funcs.end() && it->second) { - ret = CallUnitLocalProto(s, name, it->second, call_args, nargs); - handled = true; + const NamedCallTarget hit = ResolveNamedCall(s, proto, ip, inst, nargs, call_args); + if (hit.direct) { + ret = InterpExecuteInContext(s, hit.proto, nargs == 0 ? nullptr : call_args, nargs, nullptr); + } else { + const std::string_view name = ConstToName(proto->constants[static_cast(inst.sbx)]); + if (hit.proto) { + ret = CallUnitLocalProto(s, name, hit.proto, call_args, nargs); + } else if (!TryInterpNamedCall(s, name, call_args, nargs, ret)) { + ret = CallByNameArgs(s, JIT_INTERP, name, nargs, call_args); } } - if (!handled && !TryInterpNamedCall(s, name, call_args, nargs, ret)) { - ret = CallByNameArgs(s, JIT_INTERP, name, nargs, call_args); - } VM_W(inst.a, (inst.c & 1) == 0 ? ret : interp_rt::UnboxMulti(ret, 0)); } VM_DISPATCH_NEXT(); @@ -922,7 +969,18 @@ CVar InterpreterExecuteT(State *s, FuncProto *proto, const CVar *args, int arg_c { FuncProto *child = proto->child_protos[static_cast(inst.sbx)]; const int nup = static_cast(child->upvalues.size()); - std::vector upbufs(static_cast(nup), nullptr); + constexpr int kStackUpvalues = 16; + CVar *stack_up[kStackUpvalues]; + std::vector heap_up; + CVar **upbufs = nullptr; + if (nup > 0) { + if (nup <= kStackUpvalues) { + upbufs = stack_up; + } else { + heap_up.assign(static_cast(nup), nullptr); + upbufs = heap_up.data(); + } + } for (int i = 0; i < nup; ++i) { const auto &uv = child->upvalues[static_cast(i)]; if (uv.in_stack) { @@ -945,7 +1003,7 @@ CVar InterpreterExecuteT(State *s, FuncProto *proto, const CVar *args, int arg_c upbufs[i] = (cl && uv.idx < static_cast(cl->upvalue_count)) ? cl->upvalues[uv.idx] : nullptr; } } - VM_W(inst.a, interp_rt::MakeClosure(s, TagInterpProto(child), nup, child->param_count, child->is_vararg, upbufs.empty() ? nullptr : upbufs.data())); + VM_W(inst.a, interp_rt::MakeClosure(s, TagInterpProto(child), nup, child->param_count, child->is_vararg, nup == 0 ? nullptr : upbufs)); } VM_DISPATCH_NEXT(); }