Skip to content

Commit

Permalink
performance improvements
Browse files Browse the repository at this point in the history
- improve initialization performance by caching the selected runtime and thereby skipping the slow automatic runtime selection
- print initialization duration when NP_DEBUG is set
- adjust compression levels for nix-portable release to achieve a smaller file size
  • Loading branch information
DavHau committed Apr 15, 2024
1 parent e16c708 commit 9104d8f
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 7 deletions.
35 changes: 30 additions & 5 deletions default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ let

maketar = targets:
pkgsBuild.stdenv.mkDerivation {
name = "maketar";
name = "nix-portable-store-tarball";
nativeBuildInputs = [ pkgsBuild.perl pkgsBuild.zstd ];
exportReferencesGraph = map (x: [("closure-" + baseNameOf x) x]) targets;
buildCommand = ''
Expand Down Expand Up @@ -95,6 +95,8 @@ let
set -eo pipefail
start=\$(date +%s%N) # start time in nanoseconds
# dump environment on exit if debug is enabled
if [ -n "\$NP_DEBUG" ] && [ "\$NP_DEBUG" -ge 1 ]; then
trap "declare -p > /tmp/np_env" EXIT
Expand Down Expand Up @@ -344,11 +346,25 @@ let
debug "proot executable: \$NP_PROOT"
debug "testing all available runtimes..."
if [ -z "\$NP_RUNTIME" ]; then
# read last automatic selected runtime from disk
if [ "\$newNPVersion" == "true" ]; then
debug "removing cached auto selected runtime"
rm -f "\$dir/conf/last_auto_runtime"
fi
if [ -f "\$dir/conf/last_auto_runtime" ]; then
last_auto_runtime="\$(cat "\$dir/conf/last_auto_runtime")"
else
last_auto_runtime=
fi
debug "last auto selected runtime: \$last_auto_runtime"
if [ "\$last_auto_runtime" != "" ]; then
NP_RUNTIME="\$last_auto_runtime"
# check if nix --store works
mkdir -p \$dir/tmp/
touch \$dir/tmp/testfile
debug "testing nix --store"
if "\$NP_NIX" --store "\$dir/tmp/__store" shell -f "\$dir/mini-drv.nix" -c "\$dir/bin/nix" store add-file --store "\$dir/tmp/__store" "\$dir/tmp/testfile" >/dev/null 2>&3; then
elif \\
debug "testing nix --store" \\
&& mkdir -p \$dir/tmp/ \\
&& touch \$dir/tmp/testfile \\
&& "\$NP_NIX" --store "\$dir/tmp/__store" shell -f "\$dir/mini-drv.nix" -c "\$dir/bin/nix" store add-file --store "\$dir/tmp/__store" "\$dir/tmp/testfile" >/dev/null 2>&3; then
chmod -R +w \$dir/tmp/__store
rm -r \$dir/tmp/__store
debug "nix --store works on this system -> will use nix as runtime"
Expand All @@ -363,6 +379,7 @@ let
debug "bwrap doesn't work on this system -> will use proot"
NP_RUNTIME=proot
fi
echo -n "\$NP_RUNTIME" > "\$dir/conf/last_auto_runtime"
else
debug "runtime selected via NP_RUNTIME: \$NP_RUNTIME"
fi
Expand Down Expand Up @@ -540,6 +557,14 @@ let
fi
### print elapsed time
end=\$(date +%s%N) # end time in nanoseconds
# time elapsed in millis with two decimal places
# elapsed=\$(echo "scale=2; (\$end - \$start)/1000000000" | bc)
elapsed=\$(echo "scale=2; (\$end - \$start)/1000000" | bc)
debug "Time to initialize nix-portable: \$elapsed millis"
### run commands
[ -z "\$NP_RUN" ] && NP_RUN="\$run"
Expand Down
26 changes: 24 additions & 2 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,19 @@
({

bundlers = forAllSystems (system: pkgs: {
default = drv: self.packages.${system}.nix-portable.override {
# bundle with fast compression by default
default = self.bundlers.${system}.zstd-fast;
zstd-fast = drv: self.packages.${system}.nix-portable.override {
bundledPackage = drv;
compression = "zstd -3 -T0";
};
zstd-max = drv: self.packages.${system}.nix-portable.override {
bundledPackage = drv;
compression = "zstd -19 -T0";
};
xz-max = drv: self.packages.${system}.nix-portable.override {
bundledPackage = drv;
compression = "xz -9 -T0";
};
});

Expand All @@ -180,7 +191,18 @@
);

packages = forAllSystems (system: pkgs: {
nix-portable = nixPortableForSystem { inherit system; };
nix-portable = (nixPortableForSystem { inherit system; }).override {
# all non x86_64-linux systems are built via emulation
# -> decrease compression level to reduce CI build time
compression =
if system == "x86_64-linux"
then "zstd -19 -T0"
else "zstd -9 -T0";
};
# dev version that builds faster
nix-portable-dev = self.packages.${system}.nix-portable.override {
compression = "zstd -3 -T1";
};
});

defaultPackage = forAllSystems (system: pkgs:
Expand Down

0 comments on commit 9104d8f

Please sign in to comment.