r/rust • u/Intelligent_Score932 • 9h ago
Rust module system?
I recently wanted to compile something for a bevy game I was messing around with, then cargo looks me in the eyes and tells me I need to compile hundreds of dependencies, all for the end result to be 30 mb? I was wondering if there were any systems like a .dll for rust? I understand that you would lose some type checking, but I think that could be solved just with a type definitions segment towards the start, then a functions table of all the code for the system, is this something that would work, or am I oversimplifying a major intentional design choice in rust?
19
u/ToTheBatmobileGuy 9h ago
am I oversimplifying a major intentional design choice in rust?
Bevy makes heavy use of generics. Static dispatch functions are compiled per each type used with the generic and optimized separately... the C ABI (what is used in dll) does not support generics.
3
u/lfairy 7h ago
Swift does pull off dynamic linking with generics, but they put a lot of effort into making that work.
1
u/simonask_ 1h ago
It also comes with a runtime cost. It’s still great, but for something like Bevy’s ECS framework, it would be totally unworkable.
11
u/cynokron 9h ago
Dlls are an operating system feature, rust supports them just not to the level you are hoping for.
6
u/bbkane_ 7h ago
Bevy has specific guidelines to speed up compile times: https://bevy.org/learn/quick-start/getting-started/setup/#compile-with-performance-optimizations
Hopefully these speed up your build
1
u/fbochicchio 6h ago
I guess someone(s) could make an archive of precompiled crates for the most common architectures. So you can pull in both the sources and the object files and won't have to recompile everything. But I don't know if it is worth it, you just have to compile all once, it is not so much different from when you had to do the configure; make; make install dance for most of open source software: just relax and cool for awile...
1
u/South_Survey_2088 5h ago
I mean, everyone could just cache their own "archive" by using something like sccache.
Using public archives with precompiled binaries seems like a massive attack vector.
1
u/Vortetty 6h ago
rust re-orders a considerable amount at compile time, you'd be getting what amounts to the c abi's overhead for not much in return.
1
u/Xevioni 2h ago
You probably have a weak system, inappropriate build flags, or something about your project config inflating build times, since in my experience cold builds of Bevy are maybe 2-3 minutes.
If you know your system is not that weak, meaning a fast disk, modern CPU, reasonable RAM, low contention, then it would be worth tuning your configs.
e.g. Use mold or wild linker, latest stable Rust, disable or lower debug info, split crates into a workspace, try caching (kache project is new and has promise), lower opt levels, among a few other tricks.
1
u/Serianox_ 1h ago
you would lose some type checking
Exactly this. Rust doesn't have an ABI, and you would lose all safety guarantee.
1
u/bluurryyy 1h ago
Bevy has dynamic linking behind a feature flag. This makes rebuilding your game very fast. There are also other things you can configure to make builds faster. You can read about that here: https://bevy.org/learn/quick-start/getting-started/setup/#enable-fast-compiles-optional
1
1
u/Zde-G 8m ago
am I oversimplifying a major intentional design choice in rust?
No and yes. Throw few millions on the problem, push hard on people who are trying to invent right solutions and tell them you need that you need result now, not after next 10 years and voila… done.
You would, most likely, lose low-level developers (because doing required tricks in no-allocation environment is hard), Linux kernel and many other things. But would make bevy much easier to use, sure.
But, for better or for worse, Rust team wasn't organized like that.
23
u/Careful_Pizza1560 9h ago
The compile times for bevy are kinda crazy, my own project with it takes like 8 minutes on a cold build and im just sitting there staring at terminal
rust does have dynamic linking with cdylib crate type but it's not really the same as what you want, the whole ecosystem is built around static linking and that's why cargo pulls in so much stuff. you can use `dylib` but it's pretty limited and you lose a lot of the zero cost abstractions that make rust fast
the type definition idea sounds interesting but you would need to solve the generics problem, rust monomorphizes everything so the function tables would have to include every possible type combination which is basically impossible for something like bevy with all its generics
maybe you can look into using workspaces and splitting your game logic from engine code, at least incremental builds get faster that way even if the first compile still hurts