185 points · by telotortium · 2y ago · 78 comments
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).
C# with zero-cost FFI, none of the performance penalty and ability to statically link everything together is a strictly better choice. Saner type system and syntax too.
On top of that, complex blittable structures are first and foremost expressible in C# - you can easily have C binary tree, or an array of arrays with byte**. Performance-sensitive code that does interop heavily exploits that to give actual C-like experience.
Also short-lived FFI calls do not need to notify GC (which is also cheap it toggles a boolean), libraries that care about extra last nanosecond annotate them with `[SuppressGCTransition]` which further streamlines assembly around FFI call.
For performing FFI in a loop - the cost comes from the same reason a non-inlineable cross-compilation-unit calls are expensive within C++. Because we're talking plain call level of overhead, which is by definition as cheap as FFI gets. Of course that can still be a bottleneck if you have a small function beyond FFI boundary within a hot loop. In that case you might as well port it to C# to make it inlineable which is going to be faster, or have an FFI call for a batched operation.
Try `dotnet publish -o . -p:StripSymbols=false`ing this: https://github.com/U8String/U8String/tree/main/Examples/Inte... and then disassembling it with Ghidra. You will be positively impressed with how codegen looks - there will be simple direct calls into Rust with single bool checks for a potential GC poll after them (and compiler will merge those too after multiple consecutive calls).
Yes there are tons of better choices for rust interop. Any LLVM language will work. That’s not interesting.
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?
But generally speaking, the reason I prefer Rust to Go for most developments is its type system. I could see myself using Rust to handle business-specific/safety-critical logic and Go as a fast prototyping language to assemble them.
Let's just say that the designers of Rust and Go have prioritized very different feature sets when designing their type systems. If you develop in Rust, it's typically because you want to write "fearless code", i.e. attach sophisticated invariants to types to avoid safety issues later in development/coming from client code. If you develop in Go, it's typically because you want "productivity", i.e. reach quickly a point where you code can run and you can start testing it.
As usual, it's a tradeoff. For most tasks, I happen to click better with the former. YMMV
There's also no way to declare a non-nil pointer nor to work cleanly with nested nil-able struct fields.
Business/safety critical logic tends to get complicated, and, at least in the case of business software, is more than likely is venturing into unexplored territory, so when you're not very skilled at programming finding the right design can become an insurmountable challenge. A more expressive type system offers a bandaid to work around that.
The only way I see this happening for the same code snippet is if the Rust code was being compiled in debug mode instead of release mode.
Also, for some kinds of code (usually not CRUD), LLVM does better job than the Go compiler.
And if you do concurrency, Rust's async await can be faster than Go's approach (but it is a lot more complicated, and the perf gain usually doesn't matter).
Go has arguably a better concurrency model, so for some usecases it can work much better, but for some realtime programming stuff, Go is a no-go ba-dum-tss.
For webdev I would never chose Rust over Go, each language has their own advantage.
Go might have a better concurrency implementation, Go’s concurrency model is pretty much just threads and telling you to git gud scrub.
The developer experience on using it is much better than using coroutines in another lang.
Try using async in Rust and you'll see what I mean, it sucks to use it.
The same applies to cpp, you'll need years and years of experience to write somewhat decent cpp what you can write in Go with a few weeks learning coroutines, and will still be prone to make mistakes. I've done lots of cpp and can affirm that after decades I'm still not sure if my concurrent solution will run as well as my half-assed Go coroutine code.
Go coroutines is a good abstraction that is very fitting to the language, similar to the actor model and the BEAM VM (erlang/elixir).
Use the same abstraction in another language (like JVM or C) and you'll see how the devex makes a huge difference.