diff options
author | pennae <github@quasiparticle.net> | 2021-12-25 15:29:49 +0100 |
---|---|---|
committer | pennae <github@quasiparticle.net> | 2022-01-03 21:05:53 +0100 |
commit | 00c993f48b1c752656d8cdd2c25e38f9ba572128 (patch) | |
tree | 91c8a705567b281cdfe3c21c3bcb21c8842565c0 /src/libexpr/primops.cc | |
parent | 96d08fcd66e2c38598bab4f39a37a98d58347467 (diff) |
add zipAttrsWith primop
nixpkgs can save a good bit of eval memory with this primop. zipAttrsWith is
used quite a bit around nixpkgs (eg in the form of recursiveUpdate), but the
most costly application for this primop is in the module system. it improves
the implementation of zipAttrsWith from nixpkgs by not checking an attribute
multiple times if it occurs more than once in the input list, allocates less
values and set elements, and just avoids many a temporary object in general.
nixpkgs has a more generic version of this operation, zipAttrsWithNames, but
this version is only used once so isn't suitable for being the base of a new
primop. if it were to be used more we should add a second primop instead.
Diffstat (limited to 'src/libexpr/primops.cc')
-rw-r--r-- | src/libexpr/primops.cc | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/src/libexpr/primops.cc b/src/libexpr/primops.cc index 7c964bd0d..ab99962f7 100644 --- a/src/libexpr/primops.cc +++ b/src/libexpr/primops.cc @@ -2503,6 +2503,90 @@ static RegisterPrimOp primop_mapAttrs({ .fun = prim_mapAttrs, }); +static void prim_zipAttrsWith(EvalState & state, const Pos & pos, Value * * args, Value & v) +{ + // we will first count how many values are present for each given key. + // we then allocate a single attrset and pre-populate it with lists of + // appropriate sizes, stash the pointers to the list elements of each, + // and populate the lists. after that we replace the list in the every + // attribute with the merge function application. this way we need not + // use (slightly slower) temporary storage the GC does not know about. + + std::map<Symbol, std::pair<size_t, Value * *>> attrsSeen; + + state.forceFunction(*args[0], pos); + state.forceList(*args[1], pos); + const auto listSize = args[1]->listSize(); + const auto listElems = args[1]->listElems(); + + for (unsigned int n = 0; n < listSize; ++n) { + Value * vElem = listElems[n]; + try { + state.forceAttrs(*vElem); + for (auto & attr : *vElem->attrs) + attrsSeen[attr.name].first++; + } catch (TypeError & e) { + e.addTrace(pos, hintfmt("while invoking '%s'", "zipAttrsWith")); + throw; + } + } + + state.mkAttrs(v, attrsSeen.size()); + for (auto & [sym, elem] : attrsSeen) { + Value * list = state.allocAttr(v, sym); + state.mkList(*list, elem.first); + elem.second = list->listElems(); + } + + for (unsigned int n = 0; n < listSize; ++n) { + Value * vElem = listElems[n]; + for (auto & attr : *vElem->attrs) + *attrsSeen[attr.name].second++ = attr.value; + } + + for (auto & attr : *v.attrs) { + Value * name = state.allocValue(); + mkString(*name, attr.name); + Value * call1 = state.allocValue(); + mkApp(*call1, *args[0], *name); + Value * call2 = state.allocValue(); + mkApp(*call2, *call1, *attr.value); + attr.value = call2; + } +} + +static RegisterPrimOp primop_zipAttrsWith({ + .name = "__zipAttrsWith", + .args = {"f", "list"}, + .doc = R"( + Transpose a list of attribute sets into an attribute set of lists, + then apply `mapAttrs`. + + `f` receives two arguments: the attribute name and a non-empty + list of all values encountered for that attribute name. + + The result is an attribute set where the attribute names are the + union of the attribute names in each element of `list`. The attribute + values are the return values of `f`. + + ```nix + builtins.zipAttrsWith + (name: values: { inherit name values; }) + [ { a = "x"; } { a = "y"; b = "z"; } ] + ``` + + evaluates to + + ``` + { + a = { name = "a"; values = [ "x" "y" ]; }; + b = { name = "b"; values = [ "z" ]; }; + } + ``` + )", + .fun = prim_zipAttrsWith, +}); + /************************************************************* * Lists |