--disable-wasm-trap-handler


Node.js 在 64 位平台上启用了 V8 基于陷阱处理器的 WebAssembly 边界检查,这显著提高了 WebAssembly 的性能,因为无需再进行内联边界检查。此优化需要为每个 WebAssembly 内存实例分配一个大型虚拟内存区域(当前对于 32 位 WebAssembly 内存通常为 8GB,对于 64 位 WebAssembly 内存为 16GB)以捕获越界访问。在大多数 64 位平台上,虚拟内存地址空间通常足够大(约 128TB)以适应典型的 WebAssembly 使用,但如果机器对虚拟内存有手动限制(例如通过 ulimit -v),WebAssembly 内存分配更可能因 WebAssembly.Memory(): could not allocate memory 而失败。

🌐 Node.js enables V8's trap-handler-based WebAssembly bound checks on 64-bit platforms, which significantly improves WebAssembly performance by eliminating the need for inline bound checks. This optimization requires allocating a large virtual memory cage per WebAssembly memory instance (currently typically 8GB for 32-bit WebAssembly memory, 16GB for 64-bit WebAssembly memory) to trap out-of-bound accesses. On most 64-bit platforms, the virtual memory address space is usually large enough (around 128TB) to accommodate typical WebAssembly usages, but if the machine has manual limits on virtual memory (e.g. through ulimit -v), WebAssembly memory allocation is more likely to fail with WebAssembly.Memory(): could not allocate memory.

在启动时,Node.js 会自动检查是否有足够的虚拟内存可用于分配至少一个内存笼,如果没有,陷阱处理器优化会自动被禁用,以便 WebAssembly 仍然可以使用内联边界检查运行(但性能较低)。但是,如果应用需要创建许多 WebAssembly 内存实例,而机器仍然配置了相对较高的虚拟内存限制,由于虚拟内存使用量增加,WebAssembly 内存实例的分配可能仍会比预期更快失败。

🌐 At startup, Node.js automatically checks whether there is enough virtual memory available to allocate at least one cage, and if not, the trap-handler optimization is automatically disabled so that WebAssembly can still run using inline bound checks (with less optimal performance). But if the application needs to create many WebAssembly memory instances and the machine still configures a relatively high limit on virtual memory, allocation of WebAssembly memory instances may still fail more quickly than expected due to the raised virtual memory usage.

--disable-wasm-trap-handler 完全禁用此优化,以便 WebAssembly 内存实例始终使用内联边界检查,而不是保留大型虚拟内存区。这允许在 Node.js 进程可用的虚拟内存地址空间有限时创建更多实例。