Rustgo: Calling Rust from Go with near-zero overhead
An experiment explores calling Rust code from Go to enhance performance in critical applications. It proposes linking Rust directly into Go, avoiding cgo overhead, and details calling conventions for efficient integration.
Read original articleThe article discusses an experiment to call Rust code from Go with minimal overhead, aiming to replace assembly code in performance-critical applications. The author highlights Rust's advantages, such as being more readable than assembly while still allowing for optimizations. The challenge lies in Go's Foreign Function Interface (cgo), which, while enabling calls to C functions, incurs significant performance costs that are unsuitable for small, frequently called functions.
The proposed solution involves linking Rust code directly into Go programs, treating it similarly to assembly. The author outlines the process of compiling Rust into a static library and linking it with Go using the system linker. This method avoids the overhead associated with cgo, allowing for more efficient function calls.
To successfully call Rust functions from Go, the author explains the need to understand calling conventions for both languages. The Go calling convention is mostly undocumented, but the author provides insights into how arguments and return values are managed during function calls. The article concludes with a practical example of invoking a Rust function from Go, demonstrating the potential for high-performance inter-language calls without the complexity and overhead of cgo. The experiment illustrates the feasibility of integrating Rust into Go applications, leveraging Rust's performance benefits while maintaining Go's usability.
Related
Beating the Compiler
The blog post discusses optimizing interpreters in assembly to outperform compilers. By enhancing the Uxn CPU interpreter, a 10-20% speedup was achieved through efficient assembly implementations and techniques inspired by LuaJIT.
Rust for Filesystems
At the 2024 Linux Summit, Wedson Almeida Filho and Kent Overstreet explored Rust for Linux filesystems. Rust's safety features offer benefits for kernel development, despite concerns about compatibility and adoption challenges.
I Hope Rust Does Not Oxidize Everything
The author expresses concerns about Rust's widespread adoption in programming, citing issues with syntax, async features, complexity, and long compile times. They advocate for language diversity to prevent monoculture, contrasting Rust with their language Yao.
Investing in Rust
Investing in Rust programming language can enhance cybersecurity by preventing memory-related vulnerabilities. Challenges in adoption include integration issues and skill set mismatches, suggesting U.S. policy interventions for promotion.
Building static binaries with Go on Linux
The article explains how to build static binaries with Go on Linux, noting that while possible, it requires specific configurations and tools, especially when using C code via cgo.
I wonder if it would have been easier to disassemble the rust binary and turn it into Go assembly and use that.
That would need a fairly complicated program to process the binary back into assembler. Maybe getting the rust compiler to output assembly and processing into Go assembly would be the way.
Using Go assembly would save fighting with the linker, be more likely to survive upgrades and it would be cross platform (well at least on platforms with the same CPU arch).
There had been an issue for having something similar in the language itself - https://github.com/golang/go/issues/42469, but the Golang compiler team rejected it. If you have followed similar discussions around this with the Golang compiler team, you will notice a pattern of interaction that strongly indicates that they are very much opposed to ever accepting this into the compiler.
2017 (282 points, 68 comments) https://news.ycombinator.com/item?id=15017519
2019 (107 points, 37 comments) https://news.ycombinator.com/item?id=20600178
I was under the impression that go isn't that far off from rust on most speed metrics being both compiled. So this adds complexity for what gain?
Related
Beating the Compiler
The blog post discusses optimizing interpreters in assembly to outperform compilers. By enhancing the Uxn CPU interpreter, a 10-20% speedup was achieved through efficient assembly implementations and techniques inspired by LuaJIT.
Rust for Filesystems
At the 2024 Linux Summit, Wedson Almeida Filho and Kent Overstreet explored Rust for Linux filesystems. Rust's safety features offer benefits for kernel development, despite concerns about compatibility and adoption challenges.
I Hope Rust Does Not Oxidize Everything
The author expresses concerns about Rust's widespread adoption in programming, citing issues with syntax, async features, complexity, and long compile times. They advocate for language diversity to prevent monoculture, contrasting Rust with their language Yao.
Investing in Rust
Investing in Rust programming language can enhance cybersecurity by preventing memory-related vulnerabilities. Challenges in adoption include integration issues and skill set mismatches, suggesting U.S. policy interventions for promotion.
Building static binaries with Go on Linux
The article explains how to build static binaries with Go on Linux, noting that while possible, it requires specific configurations and tools, especially when using C code via cgo.