Skip to content

perf: break closure chains and lazy instantiation backport - #579

Merged
theutz merged 1 commit into
denful:mainfrom
sini:perf/closure-chain-breakup
May 31, 2026
Merged

theutz merged 1 commit into
denful:mainfrom
sini:perf/closure-chain-breakup

Conversation

@sini

@sini sini commented May 30, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Backport of perf and fix commits from the den-lazy branch, independent of the gen-schema migration.

Performance

  • Closure chain elimination — state helpers (scopedAppend/scopedMerge/etc.) stored accumulated state behind _: ... thunks, creating O(N) closure chains. N reads = O(N²). Fix: materialise the previous state eagerly outside the thunk. Cold eval: -17%, memory: -70%, attrset copies: -88%.
  • Lazy instantiation — applyInstantiates and hostConfigs now defer spec.instantiate behind thunks so hosts are only evaluated when accessed. Eliminates unnecessary NixOS config evaluation for multi-host flakes.

Fixes

  • Safe fallback when entity kind has no aspect defined
  • Default path=[] in policy.route constructor
  • Skip unsatisfied parametric pipe values in resolveLocalParametric

Feature

  • pipe.collectAll — collects from all scopes matching a predicate

Motivation

The closure chain fix directly resolves stack overflow in nix-unit sandbox evaluation for configs with deep NixOS module trees (e.g. configs using stylix + plasma-manager + sops-nix). Verified against a users config where nix flake check was failing with stack overflow.

Test plan

  • 803/803 CI tests pass
  • nix flake check passes on oceangreendev config (previously stack overflow)
  • Formatting clean

@sini
sini requested a review from vic as a code owner May 30, 2026 20:22
@github-actions github-actions Bot added the allow-ci allow all CI integration tests label May 30, 2026
@sini

sini commented May 30, 2026

Copy link
Copy Markdown
Collaborator Author

This is a subset backport of the enhancements I've made on the gen-schema branch while migrating my personal nix-config over.

Backport perf and fix commits from the den-lazy branch that are
independent of the gen-schema migration work.

Perf — closure chain elimination:
  State helpers (scopedAppend/scopedMerge/etc.) stored accumulated
  state behind `_: ...` thunks.  Each write wrapped the previous
  thunk, creating a closure chain of depth N.  Reading the final
  value cost O(N), and ~2,900 reads gave O(N²) total.
  Fix: materialise the previous state eagerly outside the thunk.
  Cold eval: -17%, memory: -70%, attrset copies: -88%.

Perf — lazy instantiation:
  applyInstantiates and hostConfigs now defer spec.instantiate
  behind thunks so hosts are only evaluated when accessed.

Fixes:
  - safe fallback when entity kind has no aspect defined
  - default path=[] in policy.route constructor
  - skip unsatisfied parametric pipe values in resolveLocalParametric

Feature:
  - pipe.collectAll — collects from all scopes matching a predicate
@sini
sini force-pushed the perf/closure-chain-breakup branch from 7c99e12 to 4399b7a Compare May 31, 2026 15:50
@theutz
theutz enabled auto-merge (squash) May 31, 2026 23:32
@theutz
theutz merged commit 18e0d1c into denful:main May 31, 2026
15 checks passed
@sini
sini deleted the perf/closure-chain-breakup branch May 31, 2026 23:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

allow-ci allow all CI integration tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants