Hi,it's been ~1 year that I use Rust for my projects and one thing that still disappoint me a lot is Resource usage during compilation (cpu AND memory) .
One of my plan for 2020 is to use a raspberry pi as my main dev machine, but currently it's not possible due to Rust (one of my project take more than 4GB of RAM to compile), also it costs me a lot in CI/CD servers .
It breaks my flow: it's really hard to enter in deep work mode when you have to wait 30 sec each time your program compiles.
Please note that I follow the 'best practices' like splitting in smaller crates and so on.
The Rust project is fully aware that slow compilation is a major problem. There's work being done making the compiler faster. Short term: optimizations and parallelization. Longer term: more MIR-based optimizations (so that LLVM has less work to do), alternative back-ends.
However, don't expect it to run nicely on a Raspberry Pi anytime soon.
Oddly enough I have a not very big Rust program that I cannot compile on a Raspberry Pi at all. The build crashes out with all kind of seg faults. Neither stable or nightly work.
However it builds fine on The Pi in 64 bit mode.
For deep work mode I'm usually cycling around editing and "cargo check". A full compile only comes after a long conversation with the compiler error messages...
Maybe an option is to cross-compile your project on another machine.
I have myself a project running that I want to run on Raspberry. A fresh build took up to half an hour to compile on the Raspberry itself (mostly due to lower system frequency due to heat).
What helped me was to compile on my PC. With rustup it is no problem to install the armv7-unknown-linux-gnueabihf toolchain and compile the project with cargo build --target=armv7-unknown-linux-gnueabihf. Then I used ssh to copy the binary to the Raspberry Pi. Finally, I put everything in a shell-script making iterations quick and simple.
One thing to mention is that this is only that easy if your dependencies are pure Rust and don't depend on C-libraries, as this complicates setting up the toolchain massively.
I will continue to dev Rust on my laptop but I replaced cargo watch -x 'run' by cargo watch -x 'check' , from 30s to 6s. Regarding the raspberry pi I've abandoned the idea for now, but I really want to go 'low tech' in 2020 and will keep looking for other solutions.
Btw - I just realized, that a big part of my "slow compilation time" was actually the task runner. Since I'm targeting WASM I figured I'd just use npm/package.json to run the rust build steps since it's already there.
Turns out npm itself is really slow.
Assuming these scripts:
"echo": "npm run subecho",
"subecho": "echo hi",
I get:
echo hi by itself: 4ms
npm run subecho : 705ms
npm run echo : 1360ms
So if doing multiple steps (like cargo build and wasm-bindgen)- it's adding significant overhead, like a couple seconds, every iteration.
This is probably wasm/web specific (otherwise who on the Rust forum would be using npm as a task runner) but thought it's worth mentioning
(btw using cargo-make brings the total overhead down to like 200-300ms)
Being a Rust user myself, I am well aware of relatively slow compilation of Rust programs, but, to be honest, it's kinda naive to expect Raspberry Pi to be an acceptable dev machine for a statically typed language that tries to do as much work at compile time as possible (as opposed to run time). Performance is not Pi's strongest side, to put it lightly, nor it is intended to be. You're basically making trouble for yourself for no good reason, it's like making furniture without specialized tools in a 2x2 meter room. Anyway, devs are working on it, so all we have to do is just have patience and wait. Even though sometimes I am mildly annoyed by Rust compilation times, I am not trading Rust for anything else from its league anytime soon.
I have kinda "solved" the issue for myself some years ago by building a new PC from scratch with 16 GB RAM and an 8-core AMD processor. Didn't cost much and performs very well for all kinds of purposes. Some projects, like Amethyst, still take a long time to compile, and this is where patience comes in handy. They are not that important to me anyway.
You might not find this approach very useful, but what else can you do? I wish I could help, but I am not bright or knowledgeable enough to work on rustc, let alone optimize it, and neither are most of us
Same for me, I used to use Raspberry Pi as my main machine, even for development, I even did some C back then but I was just trying a bit of Rust, not lots of compilation. After I used it (even until now), made me more cautious that resources are important, using all kinds of minimal stuff, maybe that is one reason that got me into rust, compile time is bad but run time is really good.
@burjui Last time, I can only afford myself a Raspberry Pi 2, I even got it from my dad. Not everyone could afford 16 GB and 8-core AMD processor. Until now, I buying an 8-core AMD processor also cost me a buck which is the reason I choose not to switch (my laptop have some issues but works fine most of the time). Buying a laptop worth 300 USD is still okay but maybe not more than 1.5x of that.
Things got better after I got a x220 few years later, but building Rust itself takes like more than 6 hours on clean build, and one line change and rebuilding stage 1 (keep-stage 0) of compiler incrementally took me an hour just to check (not build/run), so one day can only do one line change to check.
Then I go ask around. If you are doing open source stuff and is really limited in hardware, I guess you could try requesting an account from compile farm GCC Compile Farm for rustc - community - Rust Internals to access machine provided from others (for free), it may be a bit lag if you are in east side of the world like me (far from server). @skerkour Wish you good luck!