Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand All @@ -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
Expand Down
3 changes: 1 addition & 2 deletions .github/workflows/build_with_windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion .github/workflows/fuzz.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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-
Expand Down
32 changes: 32 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
60 changes: 39 additions & 21 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)

Expand All @@ -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)
Expand All @@ -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)
Expand Down
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down
18 changes: 9 additions & 9 deletions README.zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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 详细文档

Expand Down
Loading
Loading