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 in nix environments #1564

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open

use mold in nix environments #1564

wants to merge 6 commits into from

Conversation

tbro
Copy link
Contributor

@tbro tbro commented Jun 7, 2024

Install mold into nix environment and configure rust to use it.

@tbro tbro force-pushed the tb/moldy-nix branch 3 times, most recently from 7694771 to 9d4bcc7 Compare July 23, 2024 18:00
mkShell (rustEnvVars // {
buildInputs = [
# Rust dependencies
stdenv =
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is stdenv meant to be passed to mkShell?

I get compilation errors on NixOS currently (and also if i pass stdenv to mkShell).

  = note: gcc: error: unrecognized command-line option '-fuse-ld=/nix/store/b7hb6xpazdnidp0s87fain2im6lk4f4r-mold-2.30.0/bin/mold'

So it looks like the devShell still brings in gcc and that's used for compilation.

I can compile with this change

diff --git a/flake.nix b/flake.nix
index 66d29e7a..7e0872a5 100644
--- a/flake.nix
+++ b/flake.nix
@@ -205,7 +205,8 @@
             jq
           ] ++ lib.optionals moldLinker [ mold ];
         in
-        mkShell (rustEnvVars // {
+        stdenv.mkDerivation (rustEnvVars // {
+          name = "shell";
           buildInputs = [
             stableToolchain

However linking still seems fairly slow. What kind of speedup do you get?

Copy link
Contributor Author

@tbro tbro Jul 24, 2024

Choose a reason for hiding this comment

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

Is stdenv meant to be passed to mkShell?

I get compilation errors on NixOS currently (and also if i pass stdenv to mkShell).

  = note: gcc: error: unrecognized command-line option '-fuse-ld=/nix/store/b7hb6xpazdnidp0s87fain2im6lk4f4r-mold-2.30.0/bin/mold'

Sorry my nix skills are very thin, so I'm probably doing something wrong. The gcc error is the same that caused me to add the env var. I'm unsure what configuration is causing gcc to be selected in some cases and not others. Possibly the issue is related to the gcc version.

So it looks like the devShell still brings in gcc and that's used for compilation.

However linking still seems fairly slow. What kind of speedup do you get?

It is noticeably faster for me. It isn't very straightforward to get real figures, because the biggest speedups are on incremental changes and modifying RUSTFLAGS will trigger a recompile of many things.

Copy link
Collaborator

@sveitser sveitser Jul 25, 2024

Choose a reason for hiding this comment

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

What I think works quite well and is somewhat realistic for development is to run a full build, then touch sequencer/src/lib.rs and then run the build again and time it.

Copy link
Collaborator

Choose a reason for hiding this comment

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

#!/usr/bin/env bash

function build() {
    echo "building..."
    cargo clippy --all-features
}

build
touch sequencer/src/lib.rs
echo "timing re-build ..."
time build

I tried with this script and for both the new shell and the old shell I get 4-5 seconds real time.

Copy link
Collaborator

@sveitser sveitser Jul 25, 2024

Choose a reason for hiding this comment

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

I think the linker is actually not invoked when running cargo clippy. If I make this change

diff --git a/flake.nix b/flake.nix
index 66d29e7a..5015ff00 100644
--- a/flake.nix
+++ b/flake.nix
@@ -55,7 +55,7 @@
       ASYNC_FLAGS = " --cfg async_executor_impl=\"async-std\" --cfg async_channel_impl=\"async-std\" ";
       LINKER_FLAGS =
         if moldLinker then
-          "-C link-arg=-fuse-ld=${pkgs.mold}/bin/mold"
+          "-C link-arg=-fuse-ld=${pkgs.mold}/bin/mold-blah"
         else
           ""
       ;

things still work.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Also the shell still doesn't seem to be using mold for linking in my case 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

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

maybe you can confirm like so:

readelf -p .comment target/debug/sequencer

String dump of section '.comment':
  [     0]  **mold** 2.31.0 (20fa8d56f5e0c47d1f4bbf7b829c12d3f43298e1; compatible with GNU ld)
  [    4f]  rustc version 1.78.0 (9b00956e5 2024-04-29)
  [    7b]  GCC: (Debian 12.2.0-14) 12.2.0

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think the linker is actually not invoked when running cargo clippy. If I make this change

Cargo clippy evokes rustc w/ linker arguments... I think some linking would have to go on since you are compiling a new binary... but I'm really not clear on the internals.

Copy link
Contributor Author

@tbro tbro Jul 25, 2024

Choose a reason for hiding this comment

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

Maybe if you want to get a better idea if it mold is useful for you, start by removing nix from the equation. Then we can figure out how to fix my terrible nix code :/.

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.

3 participants