Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: improved rust build times #26

Merged
merged 1 commit into from
Oct 4, 2024
Merged

chore: improved rust build times #26

merged 1 commit into from
Oct 4, 2024

Conversation

daviroo
Copy link
Collaborator

@daviroo daviroo commented Oct 4, 2024

Optimize Rust Build Process and Cargo Settings

This PR introduces several optimizations to improve our Rust build process and cargo settings. These changes aim to reduce build times and enhance runtime performance.

Changes

1. GitHub Actions Workflow (.github/workflows/docker-publish.yml)

  • Added Rust installation step using actions-rs/toolchain@v1.
  • Implemented caching for cargo registry and git repositories to speed up subsequent builds.
  • Introduced cargo-chef for efficient dependency caching:
    • Added steps to install cargo-chef, prepare recipe, and cook dependencies.
    • Implemented caching for cargo-chef artifacts.
  • Added a separate step to build the project with release optimizations.
  • Set CARGO_TERM_COLOR environment variable for colored output in CI logs.

These changes will significantly reduce build times by caching dependencies and build artifacts between runs.

2. Cargo Settings (Cargo.toml)

Added release profile optimizations:

[profile.release]
lto = "thin"
codegen-units = 1
opt-level = 3
panic = "abort"
  • lto = "thin": Enables thin Link Time Optimization, which is faster than full LTO but still provides good optimization.
  • codegen-units = 1: This can improve runtime performance at the cost of longer compile times.
  • opt-level = 3: Sets the highest level of optimization.
  • panic = "abort": Reduces binary size by removing panic unwinding code.

These optimizations will improve the runtime performance and reduce the size of our binary.

3. Cargo Configuration (.cargo/config.toml)

Added a new configuration file to enable parallel compilation:

[build]
jobs = 4

This setting allows Cargo to use up to 4 parallel jobs during compilation, which can significantly speed up the build process our github actions runner (which uses 4 cores).

Why These Changes?

  1. Faster CI Builds: By implementing caching, using cargo-chef, and enabling parallel compilation, we can significantly reduce the time it takes to build our project in CI, especially for incremental changes.

  2. Improved Performance: The release profile optimizations in Cargo.toml will result in a more efficient binary, potentially improving runtime performance.

  3. Smaller Binary Size: The panic = "abort" setting can lead to a smaller binary, which can be beneficial for deployment and startup times.

  4. Better CI Logs: Setting CARGO_TERM_COLOR will make our CI logs more readable with colored output.

  5. Optimized Resource Usage: By specifying the number of parallel jobs, we can better control and optimize our resource usage during the build process.

These changes strike a balance between build time optimization and runtime performance, which should lead to a more efficient development and deployment process. The combination of caching strategies, parallel compilation, and binary optimizations should provide noticeable improvements in both build times and runtime performance without requiring changes to our core application code.

@daviroo daviroo requested review from nully0x and Extheoisah October 4, 2024 08:54
@daviroo daviroo self-assigned this Oct 4, 2024
Copy link
Collaborator

@nully0x nully0x left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@nully0x nully0x merged commit 9376455 into main Oct 4, 2024
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants