Janet on x32:32 位指针,64 位速度,内存占用减少 25%
摘要
Alex Alejandre 探索为 Linux x32 ABI 编译 Janet 语言,以获得 32 位指针和 64 位速度;他报告称在 Arch 上内存节省高达约 20-25%,但该工作负载运行速度却慢了 50%——这表明 x32 对指针密集型程序具有巨大潜力,但目前缺乏打包和生态系统的支持。
<p><a href="https://lobste.rs/s/am1cuw/janet_on_x32_32_bit_pointers_64_bit_speed_25">评论</a></p>
查看缓存全文
缓存时间:
2026/10/06 23:29
# Janet 在 x32 上:32 位指针,64 位速度,节省 25% 内存
来源:https://alexalejandre.com/programming/lisp/janet-for-the-x32-abi/
2026 年 10 月 - Alex Alejandre
---
> 在 64 位系统上使用 32 位指针,可以大幅节省内存(对于指针密集的堆,节省接近一半),同时还能适度提升程序速度(因为更多数据能塞进缓存)。Linux x32 ABI 正好能做到这一点,但它被严重低估和忽视。在 Debian 上它默认是关闭的(不过你可以用启动参数启用它),而在 Arch Linux 上甚至根本没编译进去。大多数软件都能正常编译成 x32 版本,但相关的打包很少,所以几乎所有东西你都得自己编译。我尝试过把 mastodon 部署在 x32 上,结果内存占用从 650mb 降到了 350mb。这里蕴含着巨大的潜力,但相关工作主要属于那种吃力不讨好的协调与沟通性质的活,而我自己没有时间也没有动力去推动它。 -Hailey (https://lobste.rs/s/gns27z/what_are_your_programming_hunches_you#c_tw3vok)
### 如何为 32 位编译?
像 Spork 这样的 Janet 库会提供自己的构建标志,但我们可以通过一个假的 `cc` 来劫持它——这个假 `cc` 会带着必要的 `-m32 -msse2 -mfpmath=sse` 标志启动真正的编译器。我修改了默认 Janet 构建脚本(https://codeberg.org/veqq/install-latest-janet/src/branch/master/install-latest-janet.sh)的开头部分:
```
#!/bin/sh
set -eu
TARGET="$PWD/janet32"
JANET="$TARGET/bin/janet"
mkdir -p "$TARGET/cc32"
printf '#!/bin/sh\nexec gcc -m32 -msse2 -mfpmath=sse "$@"\n' > "$TARGET/cc32/cc"
chmod +x "$TARGET/cc32/cc"
export PATH="$TARGET/cc32:$PATH"
export JANET_TOOLCHAIN=cc
unset JANET_PATH JANET_TREE
mkdir -p "$TARGET"
cd "$TARGET"
rm -rf build/spork janet
git clone https://github.com/janet-lang/janet
cd janet
```
很简单!现在让我们来试一试吧!
### 在 Arch 上失败
先说结论:内存减少 20%,速度慢 50%
详见脚本和测试结果。我在 cachyos(顺便说一句,这也是 Arch)上运行,已安装 lib32-glibc 和 lib32-gcc-libs。在构建脚本中加入这个,就能强制让 spork 之类的库也以 32 位构建!
对于 `mem.janet`:
```
(let [live @[]]
(for i 0 2_000_000
(array/push live [i (+ i 1)]))
(gccollect)
(print (length live)))
```
注意,我用的是 Fish,而且不想用其他语言支持来重新构建我的网站:
```
sudo pacman -S time # 与普通 time 不同
command time -v ./bin/janet mem.janet # command 可以跳过 fish 版本
command time -v ./janet32/bin/janet mem.janet
2000000
Command being timed: "./bin/janet mem.janet"
User time (seconds): 0.24
System time (seconds): 0.05
Percent of CPU this job got: 100%
Elapsed (wall clock) time (h:mm:ss or m:ss): 0:00.30
Average shared text size (kbytes): 0
Average unshared data size (kbytes): 0
Average stack size (kbytes): 0
Average total size (kbytes): 0
Maximum resident set size (kbytes): 145196
Average resident set size (kbytes): 0
Major (requiring I/O) page faults: 0
Minor (reclaiming a frame) page faults: 33513
Voluntary context switches: 1
Involuntary context switches: 9
2000000
Command being timed: "./janet32/bin/janet mem.janet"
User time (seconds): 0.47
System time (seconds): 0.03
Percent of CPU this job got: 99%
Elapsed (wall clock) time (h:mm:ss or m:ss): 0:00.50
Average shared text size (kbytes): 0
Average unshared data size (kbytes): 0
Average stack size (kbytes): 0
Average total size (kbytes): 0
Maximum resident set size (kbytes): 113716
Average resident set size (kbytes): 0
Major (requiring I/O) page faults: 0
Minor (reclaiming a frame) page faults: 25686
Voluntary context switches: 1
Involuntary context switches: 21
```
我在测试中加了个 0:
```
command time -v ./bin/janet mem.janet
command time -v ./janet32/bin/janet mem.janet
20000000
Command being timed: "./bin/janet mem.janet"
User time (seconds): 2.83
System time (seconds): 0.46
Percent of CPU this job got: 99%
Elapsed (wall clock) time (h:mm:ss or m:ss): 0:03.31
Average shared text size (kbytes): 0
Average unshared data size (kbytes): 0
Average stack size (kbytes): 0
Average total size (kbytes): 0
Maximum resident set size (kbytes): 1410668
Average resident set size (kbytes): 0
Major (requiring I/O) page faults: 0
Minor (reclaiming a frame) page faults: 316820
Voluntary context switches: 1
Involuntary context switches: 164
Exit status: 0
20000000
Command being timed: "./janet32/bin/janet mem.janet"
User time (seconds): 4.64
System time (seconds): 0.35
Percent of CPU this job got: 99%
Elapsed (wall clock) time (h:mm:ss or m:ss): 0:05.00
Average shared text size (kbytes): 0
Average unshared data size (kbytes): 0
Average stack size (kbytes): 0
Average total size (kbytes): 0
Maximum resident set size (kbytes): 1098372
Average resident set size (kbytes): 0
Major (requiring I/O) page faults: 0
Minor (reclaiming a frame) page faults: 238633
Voluntary context switches: 1
Involuntary context switches: 193
```
例如 `declarative-dsls/tests.janet`:
```
98 passed
Command being timed: "./bin/janet ./lib/janet/bin/judge declarative-dsls/tests.janet"
User time (seconds): 0.42
System time (seconds): 0.02
Percent of CPU this job got: 100%
Elapsed (wall clock) time (h:mm:ss or m:ss): 0:00.45
Average shared text size (kbytes): 0
Average unshared data size (kbytes): 0
Average stack size (kbytes): 0
Average total size (kbytes): 0
Maximum resident set size (kbytes): 61900
declarative-dsls/tests.janet
98 passed
Command being timed: "./bin/janet ./lib/janet/bin/judge declarative-dsls/tests.janet"
User time (seconds): 0.70
System time (seconds): 0.02
Percent of CPU this job got: 99%
Elapsed (wall clock) time (h:mm:ss or m:ss): 0:00.72
Average shared text size (kbytes): 0
Average unshared data size (kbytes): 0
Average stack size (kbytes): 0
Average total size (kbytes): 0
Maximum resident set size (kbytes): 54108
Average resident set size (kbytes): 0
Major (requiring I/O) page faults: 0
Minor (reclaiming a frame) page faults: 12735
```
正如 Hailey 所说,Arch 并没有编译 x32 ABI(https://en.wikipedia.org/wiki/X32_ABI),所以我不得不使用 `-m32`,虽然我们确实获得了 20% 的内存节省,但执行速度慢了 50%,因为一半的寄存器没被使用!`-m32` 是旧的 32 位 x86 模式,只使用 8 个寄存器。要看到提速,我们需要用 `-mx32`,它使用 64 位指令集配合缩小的 32 位指针。但我不想重新编译 Linux……
不幸的是,Ubuntu 也已经放弃了 x32 支持。
### 在已过时的 Ubuntu 上成功
幸运的是,凭借我的懒惰,我手上有一些已废弃的服务器(*当然*不是生产环境……)和操作系统安装,白白占用着硬盘空间:
> Ubuntu 20.04 LTS Focal Fossa 已于 2025 年 5 月 31 日结束标准支持。
Ubuntu,所以我们又回到 bash 了:
```
grep X86_X32 /boot/config-$(uname -r)
CONFIG_X86_X32=y
```
正是我们需要的,尽管 `gcc version 9.4.0` 有点老。我们已经装好了 `build-essential git gcc-multilib libc6-dev-x32`。我们几乎准备好测试这个想法了。但首先,我们必须修改 `src/include/janet.h`:
```
#if ((defined(__x86_64__) || defined(_M_X64)) \
&& (defined(JANET_POSIX) || defined(JANET_WINDOWS))) \
```
变成:
```
#if ((defined(__x86_64__) || defined(_M_X64)) && !defined(__ILP32__) \
&& (defined(JANET_POSIX) || defined(JANET_WINDOWS))) \
```
否则 Janet 会选择 64 位的值布局。我们的构建脚本还会传递一个构建标志来禁用 Janet 的 FFI。以下是完整的 `32janet.sh`:
```
#!/bin/bash
set -eu
TARGET="$PWD/janetx32"
JANET="$TARGET/bin/janet"
mkdir -p "$TARGET/cc32"
# 这会强制 Janet 的 C 库(如 Spork)也以 32 位构建
# FFI 构建测试在 -mx32 构建下失败,所以加 -DJANET_NO_FFI
printf '#!/bin/sh\nexec gcc -mx32 -DJANET_NO_FFI "$@"\n' > "$TARGET/cc32/cc"
chmod +x "$TARGET/cc32/cc"
export PATH="$TARGET/cc32:$PATH"
export JANET_TOOLCHAIN=cc
unset JANET_PATH JANET_TREE
mkdir -p "$TARGET"
cd "$TARGET"
git clone https://github.com/janet-lang/janet
cd janet
# 修改 Janet 源码
git checkout -q 0e5fdd53 # 否则这个 sed 命令会失效
sed -i 's/^#if ((defined(__x86_64__) || defined(_M_X64)) \\$/#if ((defined(__x86_64__) || defined(_M_X64)) \&\& !defined(__ILP32__) \\/' src/include/janet.h
export CFLAGS='-fPIC -O3 -flto -fno-semantic-interposition -march=native' # 加速优化
PREFIX="$TARGET" make clean
PREFIX="$TARGET" make
PREFIX="$TARGET" make test
PREFIX="$TARGET" make install
cd ..
git clone --depth=1 https://github.com/janet-lang/spork build/spork
"$JANET" -e '(if (bundle/installed? "spork") (bundle/replace "spork" "build/spork") (bundle/install "build/spork"))'
# ### 软件包
PM="$TARGET/lib/janet/bin/janet-pm"
git clone https://codeberg.org/veqq/declarative-dsls
"$JANET" "$PM" install file::declarative-dsls # 同时安装了 https://codeberg.org/veqq/varray
git clone https://github.com/ianthehenry/judge # 同时安装了 https://github.com/ianthehenry/cmd
"$JANET" "$PM" install file::judge
time "$JANET" "$TARGET/lib/janet/bin/judge" declarative-dsls/tests.janet # 用来证明它能工作
```
以及一个用于对比的 `64janet.sh`:
```
#!/bin/bash
set -eu
TARGET="$PWD/janet64"
JANET="$TARGET/bin/janet"
unset JANET_PATH JANET_TREE
mkdir -p "$TARGET"
cd "$TARGET"
git clone https://github.com/janet-lang/janet
cd janet
git checkout -q 0e5fdd53 # 与 x32 构建使用相同的提交
export CFLAGS='-fPIC -O3 -flto -fno-semantic-interposition -march=native' # 加速优化
PREFIX="$TARGET" make clean
PREFIX="$TARGET" make
PREFIX="$TARGET" make test
PREFIX="$TARGET" make install
cd ..
git clone --depth=1 https://github.com/janet-lang/spork build/spork
"$JANET" -e '(if (bundle/installed? "spork") (bundle/replace "spork" "build/spork") (bundle/install "build/spork"))'
# ### 软件包
PM="$TARGET/lib/janet/bin/janet-pm"
git clone https://codeberg.org/veqq/declarative-dsls
"$JANET" "$PM" install file::declarative-dsls # 同时安装了 https://codeberg.org/veqq/varray
git clone https://github.com/ianthehenry/judge # 同时安装了 https://github.com/ianthehenry/cmd
"$JANET" "$PM" install file::judge
time "$JANET" "$TARGET/lib/janet/bin/judge" declarative-dsls/tests.janet # 用来证明它能工作
```
现在我们来对比一下:
```
sudo apt install time # 与普通 time 不同
/usr/bin/time -f '%e s %M KB' janet64/bin/janet janet64/lib/janet/bin/judge janet64/declarative-dsls/tests.janet
/usr/bin/time -f '%e s %M KB' janetx32/bin/janet janetx32/lib/janet/bin/judge janetx32/declarative-dsls/tests.janet
2.42 s 61668 KB # 64 位
2.36 s 53472 KB # 32 位
```
但 declarative-dsls(https://codeberg.org/veqq/declarative-dsls)属于最坏情况,它使用了带类型的 C 数组(https://codeberg.org/veqq/varray)来大幅提升速度,而这并不怎么受益于此。让我们来制作一些最小化的基准测试,真正检验两者的差异:
参见基准测试脚本。`words.janet` 生成一段文本,将其拆分为单词,统计词频并排序:
```
(let [start (os/clock :monotonic)
rng (math/rng 42)
vocab (seq [i :range [0 5000]] (string "w" (math/rng-int rng 100000)))
text @""
counts @{}]
(repeat 600000 (buffer/push text (in vocab (math/rng-int rng 5000)) " "))
(each word (string/split " " text)
(put counts word (+ 1 (get counts word 0))))
(pp (take 3 (sort-by |(- (in $ 1)) (pairs counts))))
(print (length counts) " 个不同的单词")
(print (- (os/clock :monotonic) start) " 秒"))
```
`records.janet` 按部门对 struct 进行分组,并汇总各部门的信息,其中包含类似真实代码中常见的未使用额外字段:
```
(let [start (os/clock :monotonic)
rng (math/rng 7)
depts [:eng :ops :sales :legal :support]
people (seq [i :range [0 300000]]
{:id i
# :name 和 :tags 未被使用
:name (string "person-" i)
:dept (in depts (math/rng-int rng 5))
:salary (+ 30000 (math/rng-int rng 90000))
:tags [(math/rng-int rng 10) (math/rng-int rng 10)]})
by-dept (group-by |(in $ :dept) people)]
(each dept (sorted (keys by-dept))
(let [staff (in by-dept dept)
salaries (map |(in $ :salary) staff)]
(pp [dept
(length staff)
(math/round (/ (sum salaries) (length staff)))
(length (filter |(> $ 100000) salaries))])))
(print (- (os/clock :monotonic) start) " 秒"))
```
`parse.janet` 使用 PEG 解析行并汇总数据:
```
(let [start (os/clock :monotonic)
rng (math/rng 3)
lines (seq [i :range [0 200000]]
(string i "," (math/rng-int rng 1000) ",item-" (math/rng-int rng 500) "," (/ (math/rng-int rng 10000) 100)))
row (peg/compile
~{:num (number (some (set "0123456789.")))
:txt (capture (some (if-not "," 1)))
:main (* :num "," :num "," :txt "," :num -1)})
rows (map |(peg/match row $) lines)
totals @{}]
(each [id qty item price] rows
(put totals item (+ (get totals item 0) (* qty price))))
(print (length rows) " 行, " (length totals) " 个商品, 总计 " (math/round (sum (values totals))))
(print (- (os/clock :monotonic) start) " 秒"))
```
`tree.janet` 构建并遍历二叉树,这是我们指针最密集的示例:
```
(let [start (os/clock :monotonic)
make (fn make [depth]
(if (= depth 0)
[nil nil]
[(make (- depth 1)) (make (- depth 1))]))
check (fn check [node]
(if (in node 0)
(+ 1 (check (in node 0)) (check (in node 1)))
1))
long-lived (make 19)]
(var total 0)
(repeat 40 (+= total (check (make 14))))
(print total " " (check long-lived))
(print (- (os/clock :monotonic) start) " 秒"))
```
我们进行基准测试:
```
for f in words records tree parse; do
> /usr/bin/time -f "$f 64: %e s %M KB" janet64/bin/janet $f.janet > /dev/null
> /usr/bin/time -f "$f x32: %e s %M KB" janetx32/bin/janet $f.janet > /dev/null
> done
>
words 64: 0.58 s 41904 KB
words x32: 0.67 s 31856 KB
records 64: 6.40 s 138164 KB
records x32: 5.80 s 126916 KB
tree 64: 1.39 s 50504 KB
tree x32: 1.50 s 35532 KB
parse 64: 3.20 s 58504 KB
parse x32: 3.19 s 44428 KB
words 64: 0.63 s 41984 KB
words x32: 0.70 s 31976 KB
records 64: 6.71 s 138240 KB
records x32: 5.77 s 126844 KB
tree 64: 1.40 s 50392 KB
tree x32: 1.48 s 35304 KB
parse 64: 3.37 s 58592 KB
parse x32: 3.09 s 44496 KB
words 64: 0.59 s 41756 KB
words x32: 0.67 s 31788 KB
records 64: 6.50 s 138384 KB
records x32: 5.80 s 126960 KB
tree 64: 1.39 s 50288 KB
tree x32: 1.43 s 35560 KB
parse 64: 3.35 s 58520 KB
parse x32: 3.24 s 44572 KB
words 64: 0.60 s 42012 KB
words x32: 0.67 s 31896 KB
records 64: 6.29 s 138448 KB
records x32: 6.00 s 126832 KB
tree 64: 1.38 s 50176 KB
tree x32: 1.43 s 35392 KB
parse 64: 3.36 s 58384 KB
parse x32: 3.13 s 44364 KB
```
### 结论
我们看到,对于小对象,内存节省最高,平均约 25%,而速度方面的提升并不稳定(有时快 8%,有时慢 16%)。也许其他项目比 Janet 更能从中获益,因为 Janet 已经通过 nanboxing 将值压缩到 8 字节,更小的头部结构能带来主要收益。在研究过程中,我曾期待由于更多数据留在 L1 缓存等原因而获得性能提升。在另一个 x32 支持没有被放弃的世界里,我们本可以将它作为脚本语言的默认选择。面对这种
相似文章
Lobsters Hottest
Lemire展示了如何通过64位/33位乘数技巧,在有符号32位整数常量除法上击败编译器,避免GCC/LLVM生成的修正项,并给出了现代CPU上的基准测试延迟数据。
The Old New Thing (Raymond Chen)
Raymond Chen跟进了他之前关于ARM64堆栈限制检查的文章,指出了堆栈探测函数中x15寄存器的非常规使用细节,并比较了多个架构的寄存器使用。
Hacker News Top
本文通过在Snapdragon X2 Elite硬件上运行Geekbench 7测试,研究了Windows 11 Prism二进制翻译器的性能影响,该翻译器将x86-64指令转换为aarch64指令,并探讨了对Arm在PC市场竞争力的影响。
X AI KOLs Timeline
一位开发者从零开始用纯 x86-64 汇编写了一个 24 KB 的操作系统,完全不依赖 Linux 或 C 工具链,直接从内存中运行 Qwen3-0.6B,在同一硬件上比原生 Linux 上的 llama.cpp 快最多 54%。该项目包含 ISO 构建器、Proxmox 配置指南,输出与 PyTorch FP32 位精确一致。
Lobsters Hottest
一位开发者借助 Claude Code,用纯 x86_64 汇编重建了完整的 Linux 桌面栈——从 shell、终端、窗口管理器到各种工具,实现微秒级启动,并延长数小时续航。