Skip to content

Dynamic attrs - #132

Closed
shlevy wants to merge 76 commits into
NixOS:masterfrom
shlevy:dynamic-attrs
Closed

shlevy wants to merge 76 commits into
NixOS:masterfrom
shlevy:dynamic-attrs

Conversation

@shlevy

@shlevy shlevy commented Jun 10, 2013

Copy link
Copy Markdown
Member

This series adds a new syntax for attribute names:

  • attrs.${name} is equivalent to builtins.getAttr name attrs
  • attrs ? ${name} is equivalent builtins.hasAttr name attrs
  • attrs.${name} or def is equivalent to if builtins.isAttrs attrs && builtins.hasAttr name attrs then builtins.getAttr name attrs else def
  • { ${name1} = value1; ${name2} = value2; } is equivalent to builtins.listToAttrs [ { name = name1; value = value1; } { name = name2; value = value2; } ]

This eliminates the need for the bulky getAttr, hasAttr, and listToAttrs builtins with a much more nixy syntax.

Sorry for the massive number of commits, I was testing out a new development style (which actually worked fairly well)

shlevy added 30 commits June 8, 2013 23:25
The AttrName class doesn't exist yet, I'm trying a tdd-style approach

Signed-off-by: Shea Levy <[email protected]>
Signed-off-by: Shea Levy <[email protected]>
I misunderstood the function, it's not useless

This reverts commit bf682a4.

Signed-off-by: Shea Levy <[email protected]>
shlevy added 17 commits June 10, 2013 00:26
std::map<k,v>::iterator is of type pair<const k, v>, so calling eval has
no effect unless we make a local copy.

Signed-off-by: Shea Levy <[email protected]>
I have no idea how to naturally trigger this code path to test.

Signed-off-by: Shea Levy <[email protected]>
@edolstra

Copy link
Copy Markdown
Member

Wouldn't it be easier to implement this by desugaring to hasAttr/getAttr calls? E.g. x.{y} would be desugared to getAttr "y" x. This also prevents the overhead of wrapping everything in an AttrName object.

@shlevy

shlevy commented Jun 10, 2013

Copy link
Copy Markdown
Member Author

This is actually the first time I've really touched a parser and don't know what's involved in implementing sugar. Wouldn't there be issues with the fact that multiple selects are joined into an attrPath? e.g. would foo.bar.${baz}.qux work with a naive desugaring? Also not sure what a good desugaring would look like for the listToAttrs case. But if you'd rather I go that route, I can go learn how to implement it :)

@edolstra

Copy link
Copy Markdown
Member

Desugaring is a matter of returning an AST that corresponds to the desugared expression. For instance,
foo.bar.${baz}.qux can be desugared to (getAttr baz foo.bar).qux, so you build an AST like

ExprSelect(ExprApp(ExprApp(ExprVar("getAttr"), ExprVar("baz")), ExprSelect(ExprVar("foo"), "bar")), "quz")

Set construction can be desugared to something like staticAttrs // listToAttrs { ... }.

@shlevy

shlevy commented Jun 10, 2013

Copy link
Copy Markdown
Member Author

Ah OK, that seems reasonable enough (though IMO the staticAttrs should take precedence, that's how it works in this implementation)

shlevy added a commit to shlevy/nix that referenced this pull request Jun 17, 2013
This adds new syntax for attribute names:

* `attrs.${name}` => `getAttr name attrs`
* `attrs ? ${name}` => `isAttrs attrs && hasAttr attrs name`
* `attrs.${name} or def` => `if attrs ? ${name} then attrs.${name} else def`
* { ${name} = value; } => `listToAttrs [{ inherit name value; }]`

Of course, it's a bit more complicated than that. The attribute chains
can be arbitrarily long and contain combinations of static and dynamic
parts (e.g. `attrs.${foo}.bar.${baz} or qux`), which is relatively
straightforward for the getAttrs/hasAttrs cases but is more complex for
the listToAttrs case due to rules about duplicate attribute definitions.

For attribute sets with dynamic attribute names, duplicate static
attributes are detected at parse time while duplicate dynamic attributes
are detected when the attribute set is forced. So, for example, `{ a =
{}; a.b = null; ${"c"} = true; }` will be a parse-time error, while `{ a
= {}; ${"a"}.b = null; c = true; }` will be an eval-time error
(technically that case could theoretically be detected at parse time,
but the general case would require full evaluation). The rules for
duplicate attributes are the same in both cases, though: if some
sub-chain of an attribute path already has a value, that's an error (for
example, `{ a.b = true; a.b.c = false; }` would cause an error when
parsing the `a.b.c` since `a.b`, a sub-chain, has a value. `{ a.b.d =
true; a.b.c = false; }`, on the other hand, is fine as neither is a
subchain of the other.

Finally, recursive attribute sets with dynamic attributes have the
static attributes in scope but not the dynamic ones. So `rec { a = true;
${"b"} = a; }` is equivalent to `{ a = true; b = true; }` but `rec {
${"a"} = true; b = a; }` would be an error or use `a` from the
surrounding scope if it exists.

This is an alternative solution to NixOS#132

Signed-off-by: Shea Levy <[email protected]>
@shlevy shlevy mentioned this pull request Jun 17, 2013
@shlevy

shlevy commented Jul 2, 2013

Copy link
Copy Markdown
Member Author

Closing in favor of #134

@shlevy shlevy closed this Jul 2, 2013
@shlevy
shlevy deleted the dynamic-attrs branch March 10, 2014 10:53
zolodev pushed a commit to zolodev/nix that referenced this pull request Jan 1, 2024
angerman added a commit to angerman/nix that referenced this pull request Aug 5, 2026
…are.sh

WARM=1 drops NIX_V3_NO_DISK_CACHE (disk bytecode cache ON) + adds one discarded
warmup run to populate it — measures the PRODUCTION steady state where parse+lower
is amortized (the cache-off gap overstates the real gap).  Cold path unchanged
(preserves the committed cache-off baseline).  For darwin-4.

Copyright (c) 2026 Moritz Angermann <[email protected]>, Input Output
Group. SPDX-License-Identifier: Apache-2.0
angerman added a commit to angerman/nix that referenced this pull request Aug 5, 2026
…n gap measured

Answers: "what is the REAL production gap" (the cache-off 3x includes v3's
one-time parse+lower, amortized in production).  darwin-4, median-of-N, HEAD
binary, tight spreads.

  firefox  cold 3.64x CPU/1.89x RSS  ->  WARM 2.49x CPU/1.64x RSS
  M5       cold 3.06x CPU/3.04x RSS  ->  WARM 1.83x CPU/2.26x RSS

Findings:
- parse+lower is 32% (firefox) / 40% (M5) of v3's cold CPU -> production CPU gap
  is 1.83-2.49x, NOT 3x.  Cache-off OVERSTATES the gap.
- warm also cuts RSS hugely (firefox -91MB, M5 -765MB/-26%): cold builds+retains
  IR/parse structures (MALLOC_SMALL churn + fragmentation); warm loads bytecode
  from SQLite without those transients.  So much of the cold RSS (and the NixOS#139
  ~360MB "fragmentation") is parse/lower TRANSIENT, not steady-state.
- TW warm-insensitive (no persistent cache, eval-dominated CPU) -> stable anchor.

Production gap to close: CPU 1.83-2.49x = structural per-op dispatch -> JIT (NixOS#137);
RSS 1.64-2.26x = live representation > TW's 16B niche Value -> broad foundational
(all reclaim levers killed NixOS#134/NixOS#136).  Confirms prior-session warm reframe on
fresh darwin-4 data.  P0.2 COMPLETE.

lode/P02_WARM_HEADTOHEAD_2026-06-23.md.

Copyright (c) 2026 Moritz Angermann <[email protected]>, Input Output
Group. SPDX-License-Identifier: Apache-2.0
angerman added a commit to angerman/nix that referenced this pull request Aug 5, 2026


Production gap (warm, darwin-4): firefox 2.49x CPU/1.64x RSS, M5 1.83x CPU/2.26x
RSS — cache-off overstates it (parse+lower 32-40% of cold CPU, amortized).

Copyright (c) 2026 Moritz Angermann <[email protected]>, Input Output
Group. SPDX-License-Identifier: Apache-2.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants