15 ms·
Improving Rust compile times to enable adoption of memory safety
- xiphias2 4y agoI see a lot of work going on making the compiler faster (which looks hard at this point), but I wish I just would be able to make correct changes without needing to recompile code at least. The extract function tool is very buggy. As I spend a lot of time refactoring, maybe putting time in those tools would have a better ROI than so much work into making the compiler faster.
- estebank 4y agoKeep in mind that the people working on rustc are not the same working on rust-analyzer, even if there's some overlap in contributors and there's a desire to share libraries as much as possible. Someone working on speeding up rustc is unlikely to have domain expertise in DX and AST manipulation, and vice-versa.
- xiphias2 4y agoMaybe you're right, but I think both are hard enough that people who are smart enough to do one can do the other if they really want :) By the way AST manipulation is easy, the really hard part of refactoring (that I had a lot of problem with) is creating the lifetime annotations, which requires a deep understanding of the type system. I was trying to learn some type theory and read papers to understand how Rust's life times work, but only found long research papers that don't even do the same thing as Rust. I haven't found any documentation that documents exactly when a function call is accepted by the lifetime checker (borrow checking is easy).
- guipsp 4y agoHave you read the rustnomicon section on lifetimes? I found it pretty useful
- xiphias2 4y agoIt's cool, I just looked at the manual. there's this part though: // NOTE: `'a: {` and `&'b x` is not valid syntax! I hate that I can't introduce new lifetime inside a function, it would make refactoring so much easier. Right now I have to try to refactor, see if the compiler accepts it or not, then revert the change. Sometimes desugaring would be a great feature in itself, sugaring makes interactions between functions much harder to understand.
- dcow 4y agoWhen people complain about rust compile times are they complaining about cold/clean compiles or warm/cached compiles? I can never really tell because people just gripe "compile times". I can see how someone would come to rust, type `cargo run`, wait 3-5 minutes while cargo downloads all the dependencies and compiles them along with the main package, and then say, "well that took awhile it kinda sucks". But if they change a few lines in the actual project and compile again it would be near instant. The fair comparison would be something akin to deleting your node or go modules and running a cold build. I am slightly suspicious, not in a deliberate foul play way but more in a messy semantics and ad-hoc anecdotes way, that many of these compile time discrepancies probably boil down more to differences in how the cargo tooling handles dependencies and what it decides to include in the compile phase, where it decides to store caches and what that means for `clean`, etc. compared to similar package management tooling from other languages, than it does to "rustc is slow". But I could be wrong.
- codetrotter 4y ago> But if they change a few lines in the actual project and compile again it would be near instant. If it’s a big project and the lines you are changing are in something that is being used many other places then the rebuild will still take a little while. (30 seconds or a minute, or more, depending on the size of the project.) Likewise, if you work on things in different branches you may need to wait more when you switch branch and work on something there. Also if you switch between Rust versions you need to wait a while when you rebuild your project. I love Rust, and I welcome everything that is being done to bring the compile times down further!
- Lewton 4y ago> (30 seconds or a minute, or more, depending on the size of the project.) I'm working on a largeish modern java project using gradle, and this sounds great... Every time I start my server it takes 40 seconds just for gradle to find out that all the sub projects are up to date, nothing has been changed and no compilation is necessary...
- 4y ago
- IshKebab 4y agoI really wish there was some work on hermetic compilation of crates. Ideally crates would be able to opt-in (eventually opt-out) to "pure" mode which would mean they can't use `build.rs`, proc macros are fully sandboxed, no `env!()` and so on. Without that you can't really do distributed and cached compilation 100% reliably.
- josephg 4y agoThat would help some stuff, but it wouldn’t help with monomorphized code or macro expansion. Those two are the real killers in terms of compilation performance. And in both of those cases, most of the compilation work happens at the call site - when compiling your library.
- nicoburns 4y agoWould it not allow macro expansion to be cached? Which I believe it can't be currently because macros can run arbitrary code and access arbitrary external state.
- pjmlp 4y agoEiffel, Ada, D, C++ with extern templates (even better if modules are also part of the story) show ways how this can be improved. Naturally somehow has to spend time analysing how their way maps into Rust compilation story.
- IshKebab 4y agoThose are simply different problems though. Macro expansion is not even exactly a problem. For monomorphized code the compiler just needs a mode where it automatically does what the Momo crate does. For proc macros the Watt crate (precompiled WASM macros) will make a big difference. It just needs official sanction and integration. Anyway yeah those are totally separate problems to caching and distributed builds.
- burntsushi 4y agoI originally posted this on reddit[1], but figured I'd share this here. I checked out ripgrep 0.8.0 and compiled it with both Rust 1.20 (from ~5.5 years ago) and Rust 1.67 (just released): $ git clone https://github.com/BurntSushi/ripgrep $ cd ripgrep $ git checkout 0.8.0 $ time cargo +1.20.0 build --release real 34.367 user 1:07.36 sys 1.568 maxmem 520 MB faults 1575 $ time cargo +1.67.0 build --release [... snip sooooo many warnings, lol ...] real 7.761 user 1:32.29 sys 4.489 maxmem 609 MB faults 7503 As kryps pointed out on reddit, I believe at some point there was a change to add/improve compilation times by making more effective use of parallelism. So forcing the build to use a single thread produces more sobering results, but still a huge win: $ time cargo +1.20.0 build -j1 --release real 1:03.11 user 1:01.90 sys 1.156 maxmem 518 MB faults 0 $ time cargo +1.67.0 build -j1 --release real 46.112 user 44.259 sys 1.930 maxmem 344 MB faults 0 (My CPU is a i9-12900K.) These are from-scratch release builds, which probably matter less than incremental builds. But they still matter. This is just one barometer of many. [1]: https://old.reddit.com/r/rust/comments/10s5nkq/improving_rust_compile_times_to_enable_adoption/j6ztle9/ https://old.reddit.com/r/rust/comments/10s5nkq/improving_rus...
- CGamesPlay 4y agoCan you explain why the user time goes down when using a single thread? Does that mean that there's a huge amount of contention in the parallelism?
- nequo 4y agoFaults also drop to zero. Might be worth trying to flush the cache before each cargo build?
- celrod 4y agoUser time is the amount of CPU time spent in user mode. It is aggregated across threads. If you have 8 threads running at 100% in user mode for 1 second, that gives you 8s of user time. Total CPU time in user mode will normally increase when you add more threads, unless you're getting perfect or better-than-perfect scaling.
- gregwebs 4y agoHaskell is one of the few languages that can compile slower than rust. But they have a REPL GHCI that can be used to fairly quickly reload code changes. I wish there were some efforts at dramatically different approaches like this because there’s all this work going into compilation but it’s unlikely to make the development cycle twice as fast in most cases.
- nkit 4y agoI've started liking evcxr (https://github.com/google/evcxr https://github.com/google/evcxr) for REPL. It's a little slow compared to other REPLs, but still good enough to be usable after initial load.
- sitkack 4y agoI agree, evcxr really needs to be advertised more. It might need a new name, I don't even know how to say it.
- antipurist 4y ago> eee-vic-ser https://github.com/google/evcxr/issues/215 https://github.com/google/evcxr/issues/215
- laszlokorte 4y agoPhonetically for a german that sounds like "eww, disgusting wanker"
- sesm 4y agoAre there any articles/papers that explain how a mix of compiled and interpreted code works for Haskell? I wanted to play with this idea for my toy language, but don't know where to start.
- gregwebs 4y agoThis might give you some idea, but you basically just want to reload your top level module and ghci will automatically reload any dependent modules. https://chrisdone.com/posts/ghci-reload/ https://chrisdone.com/posts/ghci-reload/ It’s not really a mix, you do one or the other although certainly ghci for local dev and compilation for prod.
- pornel 4y agoAnother build time improvement coming, especially for fresh CI builds, is a new registry protocol. Instead of git-cloning metadata for 100,000+ packages, it can download only the data for your dependencies. https://blog.rust-lang.org/inside-rust/2023/01/30/cargo-sparse-protocol.html https://blog.rust-lang.org/inside-rust/2023/01/30/cargo-spar...
- Vecr 4y agoGood thing they will continue to support the original protocol. I don't like downloading things on demand like that, not good for privacy.
- charcircuit 4y agoHow is it bad for privacy? Before: Download all metadata, Download xyz package After: Downolad xyz's metadata, Download xyz They already know you are using xyz.
- throwaway894345 4y agoI don't care much either way, but you have the privacy argument backwards. If you're downloading all the things, then no knows if you are using xyz, only that you might be using xyz. If you're just downloading what you need and you're downloading xyz, then they know that you're using xyz.
- rascul 4y agoYou're downloading specific packages either way, which can potentially be tracked, regardless of whether you're downloading metadata for all packages or just one. Edit: A thought occurs to me. Cargo downloads metadata from crates.io but clones the package repo from GitHub/etc. So unless I'm missing something, downloading specific metadata instead of all metadata allows for crates.io to track your specific packages in addition to GitHub.
- 4y ago
- Bjartr 4y agoIt's funny, any other post on HN about improvements to Rust I've seen are chock full of comments to the effect of "I guess that feature is nice, but when will they improve the compile times?" And now many of the replies to this post are "Faster compiles are nice, but when will they improve/implement important features?" The Rust dev team can't win!
- jerf 4y agoThis is when it is important not to model the comment section as some sort of single composite individual. Since it is impossible to mentally model them as the number of humans they are, I find it helpful to model them as at least a few very distinct individuals, or sometimes just as an amorphous philosophical gas that will expand to fill all available comments, where the only question is really with what distribution rather than whether a given point will be occupied.
- deleted 4y ago[deleted]
- thechao 4y agoI used to be a grad student adjacent to Bjarne Stroustrup; he has a quip he's used a bunch: if no one's complaining, no one cares. I see all of these complaints — both the volume, and the count — as great indicators of Rust's total health.
- guhidalg 4y agoSo true. I use a similar quip at work: "Take the shortcut and if we're not out of business when it becomes an issue, then it will be a good problem to have".
- rascul 4y agoI guess that depends on the work. By day I repair houses and taking shortcuts can mean I could have an even bigger problem to solve in a few months. Luckily I have yet to be in such a situation myself but I've fixed other's shortcuts a number of times.
- mcdonje 4y agoI don't know much about how the compiler works, so the answer here is probably that I should read a book, but can external crates from crates.io be precompiled? Or maybe compile my reference to a part of an external crate once and then it doesn't need to be done on future compilations? If the concern is that I could change something in a crate, then could a checksum be created on the first compilation, then checked on future compilations, and if it matches then the crate doesn't need to be recompiled.
- runevault 4y agobecause of the lack of stable ABI they'd need to pre-compile it however many versions of the rust compiler they wanted to support.
- duped 4y agoCargo already does this when building incrementally, and there are tools for doing it within an organization like sccache. > If the concern is that I could change something in a crate It's possible for a change in one crate to require recompiling its dependencies and transitive dependencies, due to conditional compilation (aka, "features' [0]). Basically you can't know which thing to compile until it's referenced by a dependent and provided a feature set. That said, many crates don't have features and have a default feature set, but the number of variants to precompile is still quite large. [0] https://doc.rust-lang.org/cargo/reference/features.html https://doc.rust-lang.org/cargo/reference/features.html Note that C and C++ have the exact same problem, but it's mitigated by people never giving a shit about locking dependencies and living with the horrible bugs that result from it.
- epage 4y agoHosted pre-compiled builds would need to account for - Feature flags - Platform conditionals - The specific rust version being used - (unsure on this) the above for all dependencies of what is being pre-compiled There is also the impediments of designing / agreeing on a security model (do you trust the author like PyPI, trust a central build authority, etc) and then funding the continued hosting. Compiling on demand like in sccache is likely the best route for not over-building and being able to evict unused items.
- fidgewidge 4y agoI wonder about the framing of the title here. Rust is great but realistically a lot of software with memory safety bugs doesn't need to be written in C in the first place. For example Java has a perfectly serviceable TLS stack written entirely in a memory safe language. Although you could try to make OpenSSL memory safe by rewriting it in Rust - which realistically means yet another fork not many people use - you could also do the same thing by implementing the OpenSSL API on top of JSSE and Bouncy Castle. The GraalVM native image project allows you to export Java symbols as C APIs and to compile libraries to standalone native code, so this is technically feasible now. There's also some other approaches. GraalVM can also run many C/C++ programs in a way that makes them automatically memory safe, by JIT compiling LLVM bitcode and replacing allocation/free calls with garbage collected allocations. Pointer dereferences are also replaced with safe member accesses. It works as long as the C is fairly strictly C compliant and doesn't rely on undefined behavior. This functionality is unfortunately an enterprise feature but the core LLVM execution engine is open source, so if you're at the level of major upgrades to Rust you could also reimplement the memory safety aspect on top of the open source code. Then again you can compile the result down to a shared native library that doesn't rely on any external JVM. Don't get me wrong, I'm not saying don't improve Rust compile times. Faster Rust compiles would be great. I'm just pointing out that, well, it's not the only memory safe language in the world, and actually using a GC isn't a major problem these days for many real world tasks that are still done with C.
- titzer 4y agoThere is Go, too.
- nindalf 4y ago> you could try to make OpenSSL memory safe by rewriting it in Rust Or just write a better crypto stack without the many legacy constraints holding OpenSSL back. Rustls (https://github.com/rustls/rustls https://github.com/rustls/rustls) does that. It has also been audited and found to be excellent - report (https://github.com/rustls/rustls/blob/main/audit/TLS-01-report.pdf https://github.com/rustls/rustls/blob/main/audit/TLS-01-repo...). You're suggesting writing this stack in a GC language. That's possible, except most people looking for an OpenSSL solution probably won't be willing to take the hit of slower run time perf and possible GC pauses (even if these might be small in practice). Also, these are hypothetical for now. Rustls exists today.
- bufo 4y ago"There are possible improvements still to be made on bigger buffers for example, where we could make better use of SIMD, but at the moment rustc still targets baseline x86-64 CPUs (SSE2) so that's a work item left for the future." I don't understand this. The vast majority (I would guess 95%+) of people using Rust have CPUs with AVX2 or NEON. Why is that a good reason? Why can't there be a fast path and slow path as a failover?
- pjmlp 4y agoBecause it requires some kind of fat binaries. Some C and C++ compilers offer this and it requires some infrastructure to make it happen (simd attribute in GCC), or explicitly loading different kinds of dynamic libraries.
- rpep 4y agoI found most precompiled C++ libraries target a specific minimum microarchitecture because it swells the binary size if you do fat binaries, and for most users they wouldn’t know anyway. Companies always get complaints when they bump up the minimum though - I remember reading some articles about some games requiring SSE4.1 or AVX and gamers complaining they couldn’t play it on their ten year old machine.
- pjmlp 4y agoNot playing on their 10 year old machine is the main reason why OpenGL 3.3 keeps being the baseline when using GL. Although maybe GL 4.1 would do it nowadays.
- bufo 4y agoRust already has runtime CPU feature detection. But that would require hand writing the SIMD code.
- pornel 4y agoHigher baseline and failovers are possible in Rust, just not the default. The default configuration for the "x86-64" target is really meant to run on every x86-64 CPU. I know at lest Debian has the same policy. SSE2 is as old as x86-64 itself, so it can be assumed to be available in every x86-64 CPU, but nothing else can. There has been some movement to define pseudo-targets like x86-64-v1, x86-64-v2, x86-64-v3 for higher baselines.
- TheDesolate0 4y ago[dead]
- lumb63 4y agoI love to see work being done to improve Rust compile times. It’s one of the biggest barriers to adoption today, IMO. Package management, one of Rust’s biggest strengths, is one of its biggest weaknesses here. It’s so easy to pull in another crate to do almost anything you want. How many of them are well-written, optimized, trustworthy, etc.? My guess is, not that many. That leads to applications that use them being bloated and inefficient. Hopefully, as the ecosystem matures, people will pay better attention to this.
- pornel 4y agoOn the contrary, commonly used Rust crates tend to be well written and well optimized (source: I have done security audits of hundreds of deps and I curate https://lib.rs https://lib.rs). Rust has a culture of splitting dependencies into small packages. This helps pull in only focused, tailored functionality that you need rather than depending on multi-purpose large monoliths. Ahead-of-time compilation + generics + LTO means there's no extra overhead to using code from 3rd party dependency vs your own (unlike interpreted or VM languages where loading code costs, or C with dynamic libraries where you depend on the whole library no matter how little you use from it). I assume people scarred by low-quailty dependencies have been burned by npm. Unlike JS, Rust has a strong type system, with rules that make it hard to cut corners and break things. Rust also ships with a good linter, built-in unit testing, and standard documentation generator. These features raise the quality of average code. Use of dependencies can improve efficiency of the whole application. Shared dependencies-of-dependencies increase code reuse, instead of each library rolling its own NIH basics like loggers or base64 decode, you can have one shared copy. You can also easily use very optimized implementations of common tasks like JSON, hashmaps, regexes, cryptography, or channels. Rust has some world-class crates for these tasks.
- memsfty 4y ago[flagged]
- rurban 4y agoI don't understand these overhyping folks. Instead of fixing their over thousands of memory safety bugs on their tracker, they rather ignore it and go for more compiler performance. Well, why not, it's insanely slow. But then I wouldn't dare to their mention their memory safety story. Which would need a better compiler, not a faster one.
- errantmind 4y agoI write a lot of Rust code and, outside of performance optimization with Release builds, I've had next to no issues with iterative compile times, even on fairly large projects. Honestly it feels like a bit of a meme at this point. My CPU is also 5 years old (4 cores at 5ghz), so it isn't like I have a super beefy setup either.
- nazka 4y agoMaybe it’s a dumb idea but what about a mode where it won’t checks types and stuff? So we can just compile super fast when we are just tinkering around. Or a mode where it compile automatically every time you change a line? (With absolutely no optimization like inlining stuff etc to make it fast) kind of like just compiling the new line from Rust to its ASM equivalent and adding that to the rest of the compiles code. Like a big fatjar type of way, if that make sense.