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

Use mold linker in nix env on Linux #2628

Merged
merged 7 commits into from
Feb 17, 2025
Merged

Use mold linker in nix env on Linux #2628

merged 7 commits into from
Feb 17, 2025

Conversation

sveitser
Copy link
Collaborator

@sveitser sveitser commented Feb 17, 2025

Close #2627
Close #1564

This is a follow up from discussion in #2559

Also adds a workflow to test the nix dev env on the CI, to make sure it's not accidentally broken by making changes to flake.nix.

The change does not affect the dev-env on macOS.

To test the impact the following script can be used

#!/usr/bin/env bash
set -euTo pipefail

trap '! [[ "$BASH_COMMAND" =~ ^(echo|bench) ]] &&
      printf "+ %s\n" "$BASH_COMMAND"' DEBUG

bench() {
    echo "Ensuring all dependencies are compiled"
    nix develop -c cargo build --locked -q
    nix develop -c cargo nextest run --locked --cargo-quiet --no-run

    echo "Touching all lib.rs files to ensure recompilation"
    find . -path '*/src/lib.rs' -exec touch {} \;

    echo "Timing cargo build on crates in this repo"
    nix develop -c time -p cargo build --locked -q

    echo "Check if mold linker was used:"
    readelf -p .comment target/nix/debug/sequencer | grep mold && echo "mold liker used" || echo "mold linker not used"
    echo

    echo "Timing nextest build"
    nix develop -c time -p cargo nextest run --locked --cargo-quiet --no-run
}

git checkout main
bench

git checkout ma/nix-env-use-mold
bench

For me this reduces the compilation time

  • for cargo build from 51 seconds to 20 seconds
  • for nextest build from 64 seconds to 34 seconds.

Full script log:

Already on 'main'
Your branch is up to date with 'origin/main'.
Ensuring all dependencies are compiled
+ nix develop -c cargo build --locked -q
+ nix develop -c cargo nextest run --locked --cargo-quiet --no-run
Touching all lib.rs files to ensure recompilation
+ find . -path '*/src/lib.rs' -exec touch {} \;
Timing cargo build on crates in this repo
+ nix develop -c time -p cargo build --locked -q
real 50.74
user 338.16
sys 278.61
Check if mold linker was used:
+ readelf -p .comment target/nix/debug/sequencer
+ grep mold
mold linker not used

Timing nextest build
+ nix develop -c time -p cargo nextest run --locked --cargo-quiet --no-run
real 63.64 
user 469.64
sys 361.95
+ git checkout ma/nix-env-use-mold
Switched to branch 'ma/nix-env-use-mold'
Ensuring all dependencies are compiled
+ nix develop -c cargo build --locked -q
+ nix develop -c cargo nextest run --locked --cargo-quiet --no-run
Touching all lib.rs files to ensure recompilation
+ find . -path '*/src/lib.rs' -exec touch {} \;
Timing cargo build on crates in this repo
+ nix develop -c time -p cargo build --locked -q
real 20.12
user 50.24
sys 26.84
Check if mold linker was used:
+ readelf -p .comment target/nix/debug/sequencer
+ grep mold
  [    47]  mold 2.36.0 (compatible with GNU ld)
mold liker used

Timing nextest build
+ nix develop -c time -p cargo nextest run --locked --cargo-quiet --no-run
real 34.03
user 193.26
sys 89.67

cc @tbro @ss-es

The build and test CI workflows already use mold. So this change only
affects development in the nix environments on Linux systems.
@sveitser sveitser changed the title Ma/nix env use mold Use mold linker in nix env on Linux Feb 17, 2025
@sveitser sveitser requested review from zacshowa and ss-es February 17, 2025 10:05
@sveitser sveitser marked this pull request as ready for review February 17, 2025 10:39
I guess when the overlay is applied there is a `pkgs` in scope, but not
sure. Probably more idiomatic to refer to attributes of `prev` which is
the package set before applying the overlay.
Also covers the sequencer-sqlite crate.
@sveitser sveitser enabled auto-merge (squash) February 17, 2025 14:00
@sveitser sveitser merged commit 8c8c59a into main Feb 17, 2025
53 checks passed
@sveitser sveitser deleted the ma/nix-env-use-mold branch February 17, 2025 15:23
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.

Compilation on Linux is unnecessarily slow
2 participants