Skip to content

lib.options: several small performance cleanups - #517802

Open
llakala wants to merge 8 commits into
NixOS:masterfrom
llakala:lib-options-perf
Open

lib.options: several small performance cleanups#517802
llakala wants to merge 8 commits into
NixOS:masterfrom
llakala:lib-options-perf

Conversation

@llakala

@llakala llakala commented May 7, 2026

Copy link
Copy Markdown
Contributor

I'm getting fast at this! Most of these are very small savings, but 0.1% of 4 million function calls is still 4 thousand function calls saved. Hash is the same before/after for both a minimal NixOS config and my own. Stats diff:

NIX_SHOW_STATS=1 \
nix-instantiate -I nixpkgs=. --eval -E '
  let nixos = import ./nixos/lib/eval-config.nix {
    modules = [
      ./nixos/modules/profiles/minimal.nix
      { 
        fileSystems."/" = { device = "/dev/sda1"; fsType = "ext4"; }; 
        boot.loader.grub.devices = ["/dev/sda"];
      }
    ];
  }; in nixos.config.system.build.toplevel.drvPath
'
{
  "attrset": {
    "lookups": "-0.11%",
    "merges": "-0.69%",
    "mergeCopies": "-0.09%"
  },
  "list": {
    "concats": null
  },
  "parser": {
    "expressions": null
  },
  "memory": {
    "envs": "-0.25%",
    "list": "-0.1%",
    "sets": "-0.07%",
    "symbols": null,
    "values": false,
    "total": "-0.12%"
  },
  "speed": {
    "primops": "-0.07%",
    "functionCalls": "-0.1%",
    "thunksMade": "-0.1%",
    "thunksAvoided": "-0.22%"
  }
}

Things done

  • Built on platform:
    • x86_64-linux
    • aarch64-linux
    • x86_64-darwin
    • aarch64-darwin
  • Tested, as applicable:
  • Ran nixpkgs-review on this PR. See nixpkgs-review usage.
  • Tested basic functionality of all binary files, usually in ./result/bin/.
  • Nixpkgs Release Notes
    • Package update: when the change is major or breaking.
  • NixOS Release Notes
    • Module addition: when adding a new NixOS module.
    • Module update: when the change is significant.
  • Fits CONTRIBUTING.md, pkgs/README.md, maintainers/README.md and other READMEs.

@nixpkgs-ci
nixpkgs-ci Bot requested review from hsjobeki, infinisil and roberth May 7, 2026 20:32
@nixpkgs-ci nixpkgs-ci Bot added 10.rebuild-linux: 1-10 This PR causes between 1 and 10 packages to rebuild on Linux. 10.rebuild-darwin: 1-10 This PR causes between 1 and 10 packages to rebuild on Darwin. 6.topic: module system About "NixOS" module system internals 6.topic: lib The Nixpkgs function library labels May 7, 2026
@nixpkgs-ci nixpkgs-ci Bot added the 12.approvals: 1 This PR was reviewed and approved by one person. label May 10, 2026

@hsjobeki hsjobeki left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

lgtm nice improvements overall. Left some nits

Comment thread lib/options.nix
:::
*/
mkEnableOption =
let

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Not sure if i like the inlining for a performance win of what exactly? Can you measure how much we save here?
dry maintainable code vs inlining for performance reasons is a tradeoff in nix unfortunately.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Sure. With the test case of:

NIX_SHOW_STATS=1 NIX_SHOW_STATS_PATH=after.json \
 nix-instantiate -I nixpkgs=. --eval -E '
    let nixos = import ./nixos/lib/eval-config.nix {
      modules = [ ./nixos/modules/profiles/minimal.nix
        { fileSystems."/" = { device = "/dev/sda1"; fsType = "ext4"; };
        boot.loader.grub.devices = ["/dev/sda"]; } ];
      }; in nixos.config.system.build.toplevel.drvPath
    '

Absolute stats diff:

{
  "attrset": {
    "lookups": -3754,
    "merges": -4118,
    "mergeCopies": -2059
  },
  "list": {
    "concats": 0
  },
  "parser": {
    "expressions": 2
  },
  "memory": {
    "envs": -197648,
    "list": 0,
    "sets": -131776,
    "symbols": 0,
    "values": -65872,
    "total": -395296
  },
  "speed": {
    "primops": 0,
    "functionCalls": -2059,
    "thunksMade": -4117,
    "thunksAvoided": -10295
  }
}

And with percentages enabled:

{
  "attrset": {
    "lookups": "-0.11%",
    "merges": "-0.65%",
    "mergeCopies": "-0.04%"
  },
  "list": {
    "concats": null
  },
  "parser": {
    "expressions": "+0%"
  },
  "memory": {
    "envs": "-0.19%",
    "list": null,
    "sets": "-0.06%",
    "symbols": null,
    "values": "-0.04%",
    "total": "-0.08%"
  },
  "speed": {
    "primops": null,
    "functionCalls": "-0.04%",
    "thunksMade": "-0.06%",
    "thunksAvoided": "-0.18%"
  }
}

-0.65% merges does seem nice - everything else is rather minor. Up to you.

Comment thread lib/options.nix
Comment thread lib/options.nix
Comment thread lib/options.nix
@llakala
llakala requested a review from hsjobeki May 10, 2026 21:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

6.topic: lib The Nixpkgs function library 6.topic: module system About "NixOS" module system internals 10.rebuild-darwin: 1-10 This PR causes between 1 and 10 packages to rebuild on Darwin. 10.rebuild-linux: 1-10 This PR causes between 1 and 10 packages to rebuild on Linux. 12.approvals: 1 This PR was reviewed and approved by one person.

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

3 participants