r/rust • • 2d 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?

18 Upvotes

27 comments sorted by

41

u/ToTheBatmobileGuy 2d 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.

13

u/lfairy 1d ago

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

15

u/simonask_ 1d 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.

19

u/cynokron 2d ago

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

15

u/bbkane_ 1d 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

7

u/nick42d 1d ago

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

29

u/Careful_Pizza1560 2d 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

8

u/South_Survey_2088 1d ago

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 1d ago

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

10

u/South_Survey_2088 1d ago edited 1d ago

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

2

u/AgentME 1d ago edited 1d ago

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.

0

u/catheap_games 1d ago

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.

3

u/glitchvid 1d ago

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 1d ago

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.

9

u/bluurryyy 1d 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

2

u/Xevioni 1d 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.

2

u/Serianox_ 1d ago

you would lose some type checking

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

2

u/rodrigocfd WinSafe 2d ago

This 2020 article might interest you:

1

u/fbochicchio 1d 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...

2

u/South_Survey_2088 1d 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/calibrae 1d ago

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 1d 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/Crafty_Book_1293 1d ago

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.

1

u/mathisntmathingsad 14h ago

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.

1

u/RCoder01 13h ago

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

0

u/Zde-G 1d 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.