Janet on x32: 32-bit Pointers, 64-bit Speed, 25% Less RAM
本文为原文前 6,000 字符的节选翻译,完整内容请查看原文。
Janet on x32: 32-bit Pointers, 64-bit Speed, 25% Less RAM
Oct 2026 - Alex Alejandre It’s possible to save a dramatic amount of memory (close to half for programs with pointer heavy heaps) and also speed up programs by a modest amount (by way of more data fitting in cache) by using 32 bit pointers on 64 bit systems.
2026年10月 - Alex Alejandre 通过在64位系统上使用32位指针,可以节省大量的内存(对于指针密集型堆的程序,节省量接近一半),并能通过将更多数据放入缓存的方式,适度提升程序运行速度。
The Linux x32 ABI lets you do just this, but it’s tragically underused and overlooked. It’s disabled by default on Debian (though you can enable it with a boot flag) and not even compiled in on Arch Linux. Most software compiles just fine for x32, but there isn’t much packaging for it so you do have to compile nearly everything yourself.
Linux x32 ABI 正是为此而生,但它却被严重低估和忽视了。它在 Debian 上默认是禁用的(尽管可以通过引导标志启用),而在 Arch Linux 上甚至根本没有编译进去。大多数软件都可以很好地编译为 x32,但由于缺乏相关的打包支持,你几乎必须自己编译所有内容。
I experimented with deploying mastodon on x32 and it cut the app’s memory usage from 650mb to 350mb. There’s so much potential here, but the work is mostly of the thankless coordination and communication type and I don’t have the time or motivation to push it forward myself. - Hailey
我尝试在 x32 上部署 Mastodon,结果将该应用的内存占用从 650MB 降低到了 350MB。这其中蕴含着巨大的潜力,但相关工作大多属于吃力不讨好的协调与沟通类事务,我没有时间也没有动力去推动它。 - Hailey
How to Compile for 32 Bits? Janet libraries like Spork supply their own build flags, but we can hijack them with a fake cc which starts the real one with the necessary -m32 -msse2 -mfpmath=sse flags. I modified the beginning of my default Janet build script:
如何编译 32 位版本? 像 Spork 这样的 Janet 库提供了自己的构建标志,但我们可以通过一个伪造的 cc 来劫持它们,该伪造程序会启动真正的编译器并附带必要的 -m32 -msse2 -mfpmath=sse 标志。我修改了默认 Janet 构建脚本的开头:
#!/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 Easy enough! Now let’s try it!
(脚本内容如上)很简单!现在让我们试试吧!
Failing on Arch tl;dr: 20% memory reduction, 50% slower See the Scripts and Results This is on cachyos (arch, btw) with lib32-glibc lib32-gcc-libs. Adding this to a build script forces libraires like spork to build at 32 bit too!
在 Arch 上失败 简而言之:内存减少 20%,速度慢了 50% 查看脚本和结果 这是在 cachyos(顺便提一下,是 Arch)上进行的,安装了 lib32-glibc 和 lib32-gcc-libs。将其添加到构建脚本中,也会强制像 spork 这样的库以 32 位构建!
For mem.janet: (let [live @[]] (for i 0 2_000_000 (array/push live [i (+ i 1)])) (gccollect) (print (length live))) Note, these use Fish and I don’t want to rebuild my site with other language support: sudo pacman -S time # different from just time command time -v ./bin/janet mem.janet # command skips the fish version command time -v ./janet32/bin/janet mem.janet 2000000
针对 mem.janet: (代码如上) 注意,这些使用了 Fish shell,我不想为了其他语言支持而重建我的网站: (命令如上)
As Hailey said, Arch does not come with the x32 ABI compiled, so I had to use -m32 and while we do get a 20% memory savings, we execute 50% slower because half the registers aren’t used! -m32 is the old 32-bit x86 mode using only 8 registers. To see the speed-up, we need to use -mx32 which uses the 64-bit instruction set with shrunken 32-bit pointers. But I don’t want to recompile Linux…Unfortunately, Ubuntu also dropped x32 support.
正如 Hailey 所说,Arch 没有编译 x32 ABI,所以我不得不使用 -m32。虽然我们确实节省了 20% 的内存,但执行速度却慢了 50%,因为有一半的寄存器没有被使用!-m32 是旧的 32 位 x86 模式,仅使用 8 个寄存器。要看到速度提升,我们需要使用 -mx32,它使用 64 位指令集并配合缩减的 32 位指针。但我不想重新编译 Linux……不幸的是,Ubuntu 也放弃了对 x32 的支持。