Cargo build performance issues

If I run cargo build on my local machine and on my Docker container I have the big problem of perfomance issues. After trying cargo chefetc etc. there were no changes. It still takes ~100 sec. in my Docker container to finish the compilation time. Another thing is that cargo build uses all of my hardware resources.

I developed my program on a Mac M2 before and there were no performance issues.

Specs:
Debian 12
8GB DDR4
250GB SSD
Intel(R) Core(TM) i7-7700HQ CPU @ 2.80GHz

Can you tell us what project you're building? without that information, we won't be able to tell you if that result is normal.

[dependencies]
actix-web = "4"
async-std = "1.12.0"
cfonts = "1"
dns-lookup = "2.0.2"
futures = "*"
lazy_static = "*"
log = "0.4"
paho-mqtt = "0.12.1"
r2d2 = "0.8.10"
regex = "1"
serde_json = "1"
simple_logger = "*"
whois-rust = "1.5.1"

[dependencies.diesel]
version = "2.0.0"
features = ["mysql"]

[dependencies.redis]
version = "0.22.3"
features = ["r2d2"]

[dependencies.reqwest]
version = "0.11"
features = ["json"]

[dependencies.serde]
version = "1.0"
features = ["derive"]

[dependencies.utoipa]
version = "3.3.0"
features = ["actix_extras"]

[dependencies.utoipa-swagger-ui]
version = "3.1.3"
features = ["actix-web"]

Is the compilation slow only when you build the docker container or also outside it? What is the dockerfile?

It is also slow when I compile it outside of it.

FROM rust:slim

RUN apt-get update && \
  apt-get install -y pkg-config make g++ libssl-dev cmake libmariadb-dev-compat && \
  rustup target add x86_64-unknown-linux-gnu 

WORKDIR /whiz/src

COPY . .

RUN CARGO_HTTP_MULTIPLEXING=false cargo build

CMD ["./target/debug/isumis"]

Are those 100s for the root crate or is it rebuilding the entire crate-graph every time? If it's rebuilding the crate graph every time there might be something invalidating/deleting the cached data.

If it's only the root crate taking so long then run it under time to see if it's using CPU or bottlenecked on something else. You might be hitting swap.

Another thing is that cargo build uses all of my hardware resources.

You can run reduce parallelism via cargo build -j <number of cores>

1 Like

This happens in Docker everytime because if I changed something then I have to delete the image and rebuild it completly. Limiting the number of CPU cores is a well solution on my local machine.

You said it's also an issue outside docker. That's likely an easier issue to diagnose. With docker you'll have to setup layers for caching of the dependencies or use cache dirs.

Ok, thanks. I will check how to do this in docker.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.