This article also has an Chinese version.
A year ago, I released an open-source project called Rust2Go(related blog: Design and Implementation of a Rust-Go FFI Framework), which provides high-performance asynchronous and synchronous call support from Rust to Go. This project serves as the foundation for several community projects and multiple internal projects within my company. I’ve continued optimizing its performance and developing new features.
I’ll be speaking on this topic at Rust Asia Conf 2025—everyone interested is warmly welcome to join!
Recently, I explored CGO-related topics and, based on a newly developed high-performance CGO mechanism, added support in Rust2Go for actively invoking Rust from Go. This article focuses primarily on the former.
Note: This article is not limited to Rust. It is applicable to all Go cross-language projects. A corresponding repository and example are also provided. Users with such needs are welcome to adopt and use it.
This article will proceed in the following order:
- Introduce the principles of CGO calls and their performance issues, which are the main targets of optimization;
- Show how to optimize CGO calls using the simplest assembly techniques;
- Highlight stack space issues and explain how switching to the G0 stack can resolve them;
- Introduce Async Preemption and how to block it to ensure the G0 stack remains unpolluted;
- Discuss the optimization results and application scenarios.