返回资讯中心
外部精选
软件工程
#开发者实时流

Go 语言在 WASM 上性能不佳,作者认为不适合作为 WASM 开发语言

来源标题:Go Is the Wrong Language for WASM

一篇技术分析指出 Go 不适合编译到 WASM:由于 WASM 缺乏栈切换指令,Go 编译器需把每个函数改写成可在任意调用点退出和恢复的状态机,即使程序不使用 goroutine,导致同一函数生成 172 条 WASM 指令,而 Rust 只需 41 条。文章还对比了产物体积,Swift 的 hello world 达 5.6 MB,其中 81% 是标准库代码。

Hacker News 最新KingOfCoders2026/12/3 · 今天17 分钟阅读

以下正文同步自 Hacker News 最新,版权归原站所有,已转换为易读排版。

, but 0.37 MB of the data are tables the garbage collector needs to walk the stacks, so they stay.Swift ships even more, 5.6 MB, and 81% of its code is the standard library (there’s a function in hello world that prints a vector of 32 numbers for debugging, 36 KB). Swift has a second mode for small devices, Embedded Swift, which gets hello world down to 13 KB, but Go doesn’t have one, unless you count TinyGo.Why is Go slow in WASM?

A goroutine has to be able to stop in the middle of a function, because it waits for a channel for example, and go on later. Natively Go switches to another stack, but WASM has no instruction for that (there’s a proposal), so the Go compiler turns every function into a state machine that can leave at every call and come back in at the same place, whether your program uses goroutines or not. This is a method from the binary trees program:func (n *node) check() int { if n.l == nil { return 1 } return 1 + n.l.check() + n.r.check() }

The Rust program has the same function, and there it’s 41 WASM instructions (the Rust compiler turns the second call into a loop), but the Go compiler needs 172 for the method above. A call of check is two instructions in Rust, one puts the pointer on the stack and the other one is the call. In Go it’s these sixteen, the comments are mine:local.get 1 local.get 2 i64.store ;; the argument goes to Go's own stack, in memory local.get 1 i32.const 8 i32.sub local.tee 1 global.set 0 ;; move the pointer to that stack local.get 1 i64.const 374079493 i64.store ;; remember where to go on i32.const 0 call $main._node.check global.get 0 local.set 1 br_if 5 ;; did check stop in the middle? then stop too

Last year I wrote that goroutines are “surprisingly irrelevant when you write a web app”, a bit like applets in Java (though more useful), the killer feature no one cared about once Java was mainstream. But in WASM you pay for them with every call.If it has to be Go, take TinyGo, which is a second compiler for Go, and switch off the scheduler with tinygo build -target=wasip1 -opt=2 -scheduler=none. Hello world is 79 KB then, the programs run at 1.4 times C, which is faster than Swift, and JSON goes from 1.08 seconds to 0.64. You lose the go keyword for that2.Why is Kotlin smaller than Rust?

Kotlin needs a garbage collector as much as Go does, but its hello world is 12 KB and Rust’s is 39 KB (33 KB when you optimize for size). WASM 3.0 comes with garbage collection, WASM GC, where a module declares its objects as structs and arrays and the engine allocates them and cleans up after them, so there’s no garbage collector in the file. Rust doesn’t use the memory management of the engine, so 11 of its 39 KB are malloc (and 8 KB are the code for formatting strings). TeaVM, which compiles Java to WASM, had its own garbage collector up to version 0.13 and uses WASM GC today, so I could measure both. Hello world went from 87 KB to 13, but only 12 of the 87 were the collector with its exceptions and its stack, the rest is mostly library code and data about classes that’s gone too. With a 64 MB heap the programs got a little faster, from 2.1 times C to 1.8, but with the default heap of Wasmtime they got slower, 2.8.The speed of WASM GC depends on the engine. Wasmtime has it switched on by default since July 20263, the heap starts empty and then the engine collects all the time, so the word count in Java took 2.7 seconds until I gave it 64 MB at the start, and then it took 0.7. V8 has had WASM GC since 2023, and it runs the same Kotlin files about twice as fast as Wasmtime, JSON even 3.3 times (1.39 seconds against 0.42), while C, Rust and Zig gain 12%. In the browser Kotlin is closer to Rust than my table says.Go can’t use WASM GC, because Go allows pointers into the middle of an object and WASM GC doesn’t have them, so every pointer would need to carry two values. The issue has been open since November 2023 and its milestone is Unplanned4. Sad. The .NET team isn’t planning to adopt WASM GC either, for similar reasons5.How does C# get as fast as C?

It depends on the compiler, the same C# source can be as fast as C or 14 times slower, and hello world can be 25 KB or 7.7 MB. Mono from Microsoft interprets the .NET bytecode, that’s the 14, or compiles it ahead of time, then it’s 1.7 times C, and the 7.7 MB of hello world still contain the interpreter for what can’t be compiled. NativeAOT-LLVM, also from Microsoft but experimental, gets hello world to 846 KB and the programs to 1.3 times C, but each start is 23 ms, because its garbage collector touches 50 MB of memory first.NetWasm isn’t from Microsoft and is as fast as C, because it allocates an object by moving a pointer, so the binary trees in C# with a garbage collector need about as long as in C with malloc, and because it uses the exceptions WASM has today, while NativeAOT-LLVM only knows the old ones, which Wasmtime refuses, so in my test it checks after every call if there was an exception. For that you have to set O3 and the garbage collector NetWasm in the project file. With the defaults you get the 25 KB hello world and 2 times C, and the binary trees need eight times as long. F# is at 1.06 times C and Visual Basic at 1.17 (yes, I compiled Visual Basic to WASM), but I had to compile both to a .NET assembly first and call it from one line of C#.I wouldn’t put NetWasm into production yet. The repository is from August 2026, nearly all commits are from one person, the README doesn’t name anyone who runs it in production, your host needs WASI 0.3, and companies with 250 people or $10M revenue pay for the compiler6. The README also says NetWasm is more than three times as fast as Mono AOT (the compiled one), but on my programs it was 1.7 times.What about JavaScript, Python and Ruby?

They bring their interpreter, so the four programs need 30 (Python), 39 (JavaScript) or 96 (Ruby) times as long as in C, and nbody, which is only math, 100 times in Python and JavaScript and 600 times in Ruby. JavaScript is fast in the browser because of the JIT compiler, but a JIT compiler can’t run inside WASM, because a module isn’t able to create new code while it runs. Ruby has an additional problem with floating point numbers. A 64 bit Ruby hides them inside the pointer, but WASM pointers have 32 bits and there’s no space for that, so 200,000 float operations allocate 200,001 objects (natively it’s one). Python needs 29 ms to start and Ruby 204, but JavaScript starts in 0.07 ms, because Javy (the tool that builds the file) starts QuickJS at build time and stores its memory in the file.Which language should the agent use?

<a href=/why-we-chose-go-over-rust-for-our-startup/ target=_blank rel=

正文由 探How 从来源站点 RSS 同步,内容未经改写;遇到排版缺失或需要图片、视频时请以原文为准。

打开原文
去社区讨论 →