Get the latest tech news
Reducing Cargo target directory size with -Zno-embed-metadata
Disk usage of the target directory is a commonly cited annoyance with Rust (and Cargo) – in the last year’s Annual Survey, it was the third most pressing issue of Rust users, right after slow compilation1 and subpar debugging experience. Given the “build everything from source” compilation model of Rust, and both debuginfo and incremental compilation being enabled by default in the dev Cargo profile, it is unlikely that the target directory will ever become lean and small. However, there are still ways of how we could reduce the target directory size by a non-trivial amount. I will describe a brand-new method of achieving that in this blog post. Funnily enough, making compilation faster can sometimes increase disk usage; for example, the incremental system of the Rust compiler stores a lot of data on disk, which helps it to make subsequent recompilations faster. ↩
Note that there are also initiatives to reduce the size of the target directory along the temporal axis, i.e. prevent it from ballooning over time (see Cargo Garbage Collection). It might be that we could do something to reduce either of these, but there is one additional thing that (kind of unnecessarily) causes target directory bloat, which is not easily observable in this table, and that is the metadata of the compiled Rust crates. I think that unless we find some major issues, we should make the-Zno-embed-metadata behavior the default in Cargo, to reduce the disk space usage of the target directory for everyone.
Or read this on Hacker News