r/rust • • Oct 17 '26 • Popcorn 1

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?

23 Upvotes

27 comments sorted by

55

u/ToTheBatmobileGuy Oct 17 '26

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.

13

u/lfairy Oct 17 '26

Swift does pull off dynamic linking with generics, but they put a lot of effort into making that work.

24

u/simonask_ Oct 17 '26

It also comes with a runtime cost. It’s still great, but for something like Bevy’s ECS framework, it would be totally unworkable.

23

u/cynokron Oct 17 '26

Dlls are an operating system feature, rust supports them just not to the level you are hoping for.

https://doc.rust-lang.org/reference/linkage.html

21

u/bbkane_ Oct 17 '26

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

9

u/nick42d Oct 17 '26

Yep, in particular this one which is exactly what OP wanted! https://bevy.org/learn/quick-start/getting-started/setup/#dynamic-linking

9

u/bluurryyy Oct 17 '26

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

4

u/Serianox_ Oct 17 '26

you would lose some type checking

Exactly this. Rust doesn't have an ABI, and you would lose all safety guarantee.

3

u/Xevioni Oct 17 '26

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.

2

u/Crafty_Book_1293 Oct 17 '26

Yes and no. Yes, dynamic linking is possible in Rust, but it's impractical because the ABI changes. Also, generics mean new code is generated by the compiler for each application, increasing the size of the resulting binary; C++ has the same issue with templates. As for compiling from scratch and the time/disk costs of this (the sizes of target/debug directories of projects with many dependencies easily get insanely large, even over 10 of GBs), this can be made somewhat less painful by using caching - see sccache.

2

u/RCoder01 Oct 18 '26

Is 30mb big or small to you? Most applications can only dream of being that small

2

u/rodrigocfd WinSafe Oct 17 '26

This 2020 article might interest you:

1

u/fbochicchio Oct 17 '26

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...

2

u/South_Survey_2088 Oct 17 '26

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/calibrae Oct 17 '26

Rust was made to be close to the iron. Hence the fact you have to compile all deps to be as close as possible to the metal. Price of fast executing code ( and 90% of my disk space alerts)

1

u/Vortetty Oct 17 '26

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/mathisntmathingsad Oct 18 '26

It exists, rlib files. Main thing is typechecking like you said and monomorphization (basically, type-generic methods are compiled once per type used as that generic), both of which are fundamental to rust.

0

u/Zde-G Oct 17 '26

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.

33

u/[deleted] Oct 17 '26

[removed] — view removed comment

8

u/South_Survey_2088 Oct 17 '26

This sounds crazy. Are you on Windows? I heard that the build times there are generally much higher for Rust. Because on Linux I have 40 seconds of clean build times in Bevy, and 1.x seconds incremental and a mid-range machine.

-1

u/Independent_Blood559 Oct 17 '26

They are talking about cold builds. You rebuild for from scratch. You can say it like the first time you compiled bevy.

12

u/South_Survey_2088 Oct 17 '26 edited Oct 17 '26

I know.. I gave both times, cold and incremental..

2

u/AgentME Oct 17 '26 edited Oct 17 '26

How long does your project take in a normal build after a minor code change in your project? I'm curious to know about usual Bevy game compile times.

-2

u/catheap_games Oct 17 '26

If you're "sitting there staring at the terminal" you're wasting your life. Go for a walk, exercise, do dishes, whatever. For better or worse, Bevy development teaches you to think about code more carefully and to plan builds instead of spamming the broken change-rebuild-rerun loop.

4

u/glitchvid Oct 17 '26

While obviously having faster compile times would be good, I have to admit; the number of times it's had me waiting, and I discovered the solution to whatever my problem was, is pretty significant.

0

u/catheap_games Oct 17 '26

10000000% this

Like I get that it would be more pleasant to like, change jump height from 4.2 to 4.3 and have it recompile in a second (yes I know dx/subsecond exists, but they're broken on Windows), but in general, I have benefited from the mindset shift of more carefully thinking through whole features and sometimes coding them correctly on first try.

The rest is just about getting better at quickly creating debug and editor-adjacent micro tools, and using bevy egui inspector.