Skip to content

Commit

Permalink
nix: use -ld_new on macOS devshell
Browse files Browse the repository at this point in the history
The new parallel macOS linker reduces link time for the debug `jj` binary from
3s to 0.7s on my M2 Macbook Air, which is a significant reduction for nearly
no cost at all. This only assumes that you have a new enough Xcode environment
as your default (where `/usr/bin/ld` resides.)

This change requires Sonoma and Xcode 15, but in theory I think we could target
a lower macOS SDK version in order to produce binaries that are more backwards
compatible, so the only real cost is that developers who also use Nix would
require Sonoma.
  • Loading branch information
thoughtpolice committed Jun 30, 2024
1 parent 3b2197c commit fe40c9e
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,25 @@
export DYLD_FALLBACK_LIBRARY_PATH=$(${ourRustVersion}/bin/rustc --print sysroot)/lib
'';

# NOTE (aseipp): on Linux, go ahead and use mold by default to improve
# link times a bit; mostly useful for debug build speed, but will help
# over time if we ever get more dependencies, too
useMoldLinker = pkgs.stdenv.isLinux;

# these are needed in both devShell and buildInputs
linuxNativeDeps = with pkgs; lib.optionals stdenv.isLinux [
mold-wrapped
];

# on macOS and Linux, use faster parallel linkers that are much more
# efficient than the defaults. these noticeably improve link time even for
# medium sized rust projects like jj
rustLinkerFlags =
if pkgs.stdenv.isLinux then
[ "-fuse-ld=mold" "-Wl,--compress-debug-sections=zstd" ]
else if pkgs.stdenv.isDarwin then
[ "-fuse-ld=/usr/bin/ld" "-ld_new" ]
else
[ ];

rustLinkFlagsString = pkgs.lib.concatStringsSep " " (pkgs.lib.concatMap (x:
[ "-C" "link-arg=${x}" ]
) rustLinkerFlags);
in
{
packages = {
Expand Down Expand Up @@ -98,7 +107,7 @@

ZSTD_SYS_USE_PKG_CONFIG = "1";
LIBSSH2_SYS_USE_PKG_CONFIG = "1";
RUSTFLAGS = pkgs.lib.optionalString useMoldLinker "-C link-arg=-fuse-ld=mold";
RUSTFLAGS = pkgs.lib.optionalString pkgs.stdenv.isLinux "-C link-arg=-fuse-ld=mold";
NIX_JJ_GIT_HASH = self.rev or "";
CARGO_INCREMENTAL = "0";

Expand Down Expand Up @@ -175,9 +184,7 @@
export ZSTD_SYS_USE_PKG_CONFIG=1
export LIBSSH2_SYS_USE_PKG_CONFIG=1
export RUSTFLAGS="-Zthreads=0"
'' + pkgs.lib.optionalString useMoldLinker ''
export RUSTFLAGS+=" -C link-arg=-fuse-ld=mold -C link-arg=-Wl,--compress-debug-sections=zstd"
export RUSTFLAGS="-Zthreads=0 ${rustLinkFlagsString}"
'' + darwinNextestHack;
};
}));
Expand Down

0 comments on commit fe40c9e

Please sign in to comment.