Skip to content

lib.modules: further performance cleanups - #583020

Merged
philiptaron merged 13 commits into
NixOS:masterfrom
llakala:lib-modules-cleanups
Nov 4, 2026
Merged

philiptaron merged 13 commits into
NixOS:masterfrom
llakala:lib-modules-cleanups

Conversation

@llakala

@llakala llakala commented Nov 4, 2026

Copy link
Copy Markdown
Contributor

Just going through with my brain, NIX_SHOW_STATS, and an untrustworthy profiler. All lib/modules tests pass.

Stats diff for my own config:

{
  "attrset": {
    "lookups": "-1.58%",
    "merges": "-6.07%",
    "mergeCopies": "-0.95%"
  },
  "list": {
    "concats": "-14.09%"
  },
  "parser": {
    "expressions": "+0%"
  },
  "memoryBytes": {
    "envs": "-6.15%",
    "list": "-0.88%",
    "sets": "-1.83%",
    "symbols": "+0%",
    "values": "-3.82%",
    "total": "-3.2%"
  },
  "speed": {
    "primops": "-7.01%",
    "functionCalls": "-7.97%",
    "thunksMade": "-4.82%",
    "thunksAvoided": "-5.71%"
  }
}

Of course, most of this is fluff. But my wall-time-evaluation goes from 4.923s to 4.883s (averaged with hyperfine over ten runs, for a 0.91% improvement).

I'd politely ask that reviewers post their own thoughts, rather than responding with whatever Claude says. If you must use an LLM, I can't stop you - but I ask that you do it in a separate PR.

Things done

Saves unnecessary function calls, since args are always constant.
Previously, we would always call `pushDownProperties` recursively - but
now we only do it if the second layer also needs to be pushed. Saves a
`map` and a function call on each definition.
@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 Nov 4, 2026
@llakala
llakala requested a review from philiptaron November 4, 2026 06:45

@philiptaron philiptaron 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.

Fully human review.

  • d9cbd05 - LGTM, would use a prime instead of inlining
  • 855f47f - LGTM, can go farther here by using genList instead of imap (for future)
  • d9a395e - LGTM
  • e2b35cb - LGTM after some staring
  • cfffa65 - LGTM, nice win for readability
  • 7e30e5e - LGTM, this "let binding for readability" is so nice but it harms the env and thunk counts due to where textually it is defined. I wish that the evaluator didn't have this property.
  • 4c38714 - ❌ - there's an error regression here. I also didn't like this inlining. I (rather than claude) took a stab at it in 916f1a4, ba3e857, and 9d70e5b stacked on top of 7e30e5e. At least in Nix 2.35 inherit (x) y in an attrset is worse than y = x.y due to an additional env.
  • a1d9b0d - nice win
  • fe0374e - nice win
  • d2a2a92 - hate that this is a win, LGTM
  • 7f67de7 - LGTM
  • 750b7c2 - LGTM

❌ on the review solely for the error prefix regression. You can cherry-pick or not the commits for the alternate path.

Comment thread lib/modules.nix Resolved
Comment thread lib/modules.nix Resolved
@github-project-automation github-project-automation Bot moved this to In progress in Nixpkgs Lib Nov 4, 2026
@llakala
llakala force-pushed the lib-modules-cleanups branch from 750b7c2 to 40440ef Compare November 4, 2026 21:14
@llakala

llakala commented Nov 4, 2026

Copy link
Copy Markdown
Contributor Author

At least in Nix 2.35 inherit (x) y in an attrset is worse than y = x.y due to an additional env.

Can reproduce - ew. cc @xokdvium - is the last commit saving thunks just an artifact of NIX_SHOW_STATS, or does nix allocate a thunk for inherit (foo) bar; compared to bar = foo.bar;?

You can cherry-pick or not the commits for the alternate path.

I'm struggling to get my head around how your alternate version of saving the isAttrs works, so I'll go with mine for now. Pushed a fix to prefix and removal of inherit ().

Fully human review.

Thanks :)

@nixpkgs-ci nixpkgs-ci Bot added the 12.approvals: 1 This PR was reviewed and approved by one person. label Nov 4, 2026
@philiptaron
philiptaron added this pull request to the merge queue Nov 4, 2026
Merged via the queue into NixOS:master with commit 1a2aaff Nov 4, 2026
29 checks passed
@github-project-automation github-project-automation Bot moved this from In progress to Done in Nixpkgs Lib Nov 4, 2026
@llakala
llakala deleted the lib-modules-cleanups branch November 4, 2026 22:25
@llakala

llakala commented Nov 4, 2026

Copy link
Copy Markdown
Contributor Author

Thanks!

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: Done

Development

Successfully merging this pull request may close these issues.

2 participants