[Optimizations] Recursive optimizations (!)

This commit is contained in:
Antoine Viallon 2022-10-12 19:31:54 +02:00
parent b2c521273f
commit 52d7d35514
Signed by: aviallon
GPG key ID: 186FC35EDEB25716
2 changed files with 81 additions and 21 deletions

View file

@ -5,17 +5,19 @@ let
desktopCfg = config.aviallon.desktop; desktopCfg = config.aviallon.desktop;
generalCfg = config.aviallon.general; generalCfg = config.aviallon.general;
_trace = if cfg.trace then (traceValSeqN 2) else (x: x);
_optimizeAttrs = _optimizeAttrs =
{ {
lto ? false , lto ? false ,
go ? false , go ? false ,
cmake ? true , cmake ? false ,
cpuArch ? generalCfg.cpuArch , cpuArch ? generalCfg.cpuArch ,
cpuTune ? generalCfg.cpuTune , cpuTune ? generalCfg.cpuTune ,
extraCFlags ? cfg.extraCompileFlags , extraCFlags ? cfg.extraCompileFlags ,
... ...
}@attrs: }@attrs:
traceValSeq ( _trace (
(myLib.optimizations.makeOptimizationFlags ({ (myLib.optimizations.makeOptimizationFlags ({
inherit lto go cpuArch cpuTune extraCFlags; inherit lto go cpuArch cpuTune extraCFlags;
} // attrs)) } // attrs))
@ -33,22 +35,69 @@ let
) )
); );
addAttrs = pkg: attrs: pkg.overrideAttrs (old: traceValSeqN 2 (myLib.attrsets.mergeAttrsRecursive old attrs) ); addAttrs = pkg: attrs: pkg.overrideAttrs (old: _trace (myLib.attrsets.mergeAttrsRecursive old attrs) );
optimizePkg = {level ? "normal", useAttrs ? false , ... }@attrs: pkg: recurseOverrideCflags = pkg: { cflags ? compilerFlags, _depth ? 0 }:
let
deps = pkg.buildInputs or [];
depsOverriden = forEach deps (_pkg: recurseOverrideCflags _pkg {
inherit cflags;
_depth = _depth + 1;
});
in if isNull pkg then
warn "pkg is null" pkg
else if (hasAttr "overrideAttrs" pkg) then
info "Optimizing '${getName pkg}' at depth ${toString _depth}"
(pkg.overrideAttrs (old:
let
_cflags =
if (! hasAttr "CFLAGS" old) then
[]
else if isList old.CFLAGS then
old.CFLAGS
else
[ old.CFLAGS ]
;
in {
buildInputs = depsOverriden;
CFLAGS = _cflags ++ cflags;
}
))
else
warn "Couldn't optimize '${getName pkg}'" pkg
;
optimizePkg = {level ? "normal" , recursive ? 0 , _depth ? 0 , ... }@attrs: pkg:
if (hasAttr "overrideAttrs" pkg) then
let let
optimizedAttrs = _optimizeAttrs (attrs // {inherit level; go = (hasAttr "GOARCH" pkg); }); optimizedAttrs = _optimizeAttrs (attrs // {inherit level; go = (hasAttr "GOARCH" pkg); });
optStdenv = pkgs.addAttrsToDerivation optimizedAttrs pkgs.fastStdenv; _buildInputs = filter (p: ! isNull p ) (pkg.buildInputs or []);
in ( _buildInputsOverriden = forEach _buildInputs (_pkg:
if (!useAttrs) && (hasAttr "stdenv" pkg.override.__functionArgs) then if (any (n: n == getName _pkg) cfg.blacklist) then
trace "Optimized ${getName pkg} with stdenv at level '${level}'" pkg.override { warn "Skipping blacklisted '${getName _pkg}'" _pkg
stdenv = optStdenv; else optimizePkg ({}
} // attrs
else if (hasAttr "overrideAttrs" pkg) then // {
trace "Optimized ${getName pkg} with overrideAttrs at level '${level}'" (addAttrs pkg optimizedAttrs) inherit level recursive;
else _depth = _depth + 1;
warn "Can't optimize ${getName pkg}" pkg }) _pkg
); );
_pkg =
if (recursive > _depth) then
pkg.overrideAttrs (old: {}
// {
buildInputs = _buildInputsOverriden;
}
// optionalAttrs (hasAttr "CFLAGS" old) {
CFLAGS = if (! isList old.CFLAGS ) then [ old.CFLAGS ] else old.CFLAGS;
}
)
else pkg;
in trace "Optimized ${getName pkg} with overrideAttrs at level '${level}' (depth: ${toString _depth})" (addAttrs _pkg optimizedAttrs)
else
warn "Can't optimize ${getName pkg} (depth: ${toString _depth})" pkg
;
in in
{ {
options.aviallon.optimizations = { options.aviallon.optimizations = {
@ -65,6 +114,13 @@ in
description = "Add specific compile flags"; description = "Add specific compile flags";
type = types.listOf types.str; type = types.listOf types.str;
}; };
trace = mkEnableOption "trace attributes in overriden derivations";
blacklist = mkOption {
default = [ "cmocka" "libkrb5" "libidn2" "tpm2-tss" ];
example = [ "bash" ];
description = "Blacklist specific packages from optimizations";
type = types.listOf types.str;
};
}; };
config = mkIf cfg.enable { config = mkIf cfg.enable {
@ -90,10 +146,14 @@ in
}) })
(self: super: { (self: super: {
opensshOptimized = optimizePkg { level = "very-unsafe"; lto = true; } super.openssh; opensshOptimized = optimizePkg { level = "very-unsafe"; recursive = 0; lto = true; } super.openssh;
#libxslt = optimizePkg { level = "unsafe"; parallelize = generalCfg.cores; lto = true; } super.libxslt; #libxslt = optimizePkg { level = "unsafe"; parallelize = generalCfg.cores; lto = true; } super.libxslt;
htop = optimizePkg {parallelize = generalCfg.cores; lto = true; } super.htop; htop = optimizePkg {
nano = optimizePkg {level = "unsafe";} super.nano; parallelize = generalCfg.cores;
lto = true;
recursive = 2;
} super.htop;
nano = optimizePkg {level = "unsafe"; recursive = 99; } super.nano;
virtmanager = optimizePkg {} super.virtmanager; virtmanager = optimizePkg {} super.virtmanager;
libsForQt5 = super.libsForQt5.overrideScope' (mself: msuper: { libsForQt5 = super.libsForQt5.overrideScope' (mself: msuper: {
plasma5 = msuper.plasma5.overrideScope' (mself: msuper: { plasma5 = msuper.plasma5.overrideScope' (mself: msuper: {

View file

@ -42,14 +42,14 @@ in
inherit unstable; inherit unstable;
inherit nur; inherit nur;
})] })]
++ optional cfg.traceCallPackage [(self: super: { ++ optional cfg.traceCallPackage (self: super: {
callPackage = path: overrides: callPackage = path: overrides:
let let
_pkg = super.callPackage path overrides; _pkg = super.callPackage path overrides;
_name = _pkg.name or _pkg.pname or "<unknown>"; _name = _pkg.name or _pkg.pname or "<unknown>";
in trace "callPackage ${_name}" _pkg in trace "callPackage ${_name}" _pkg
; ;
})] })
++ [(self: super: { ++ [(self: super: {
htop = super.htop.overrideAttrs (old: { htop = super.htop.overrideAttrs (old: {
configureFlags = old.configureFlags ++ [ configureFlags = old.configureFlags ++ [