From 403a76a18fbbbc7b1ad4b65ed8c66c9abbbffa6f Mon Sep 17 00:00:00 2001 From: volth Date: Thu, 5 Jul 2018 02:52:02 +0000 Subject: [PATCH 1/3] lib.concatMap and lib.mapAttrs to be builtins --- src/libexpr/primops.cc | 42 ++++++++++++++++++++++++++++++ tests/lang/eval-okay-concatmap.exp | 1 + tests/lang/eval-okay-concatmap.nix | 5 ++++ tests/lang/eval-okay-mapattrs.exp | 1 + tests/lang/eval-okay-mapattrs.nix | 3 +++ 5 files changed, 52 insertions(+) create mode 100644 tests/lang/eval-okay-concatmap.exp create mode 100644 tests/lang/eval-okay-concatmap.nix create mode 100644 tests/lang/eval-okay-mapattrs.exp create mode 100644 tests/lang/eval-okay-mapattrs.nix diff --git a/src/libexpr/primops.cc b/src/libexpr/primops.cc index 3a6c4035b..7ff57f0b0 100644 --- a/src/libexpr/primops.cc +++ b/src/libexpr/primops.cc @@ -1356,6 +1356,24 @@ static void prim_functionArgs(EvalState & state, const Pos & pos, Value * * args } +/* Apply a function to every element of a list. */ +static void prim_mapAttrs(EvalState & state, const Pos & pos, Value * * args, Value & v) +{ + state.forceFunction(*args[0], pos); + state.forceAttrs(*args[1], pos); + + state.mkAttrs(v, args[1]->attrs->size()); + + for (auto & i : *args[1]->attrs) { + Value vName, vFun2; + mkString(vName, i.name); + state.callFunction(*args[0], vName, vFun2, pos); + state.callFunction(vFun2, *i.value, *state.allocAttr(v, i.name), pos); + } +} + + + /************************************************************* * Lists *************************************************************/ @@ -1627,6 +1645,28 @@ static void prim_partition(EvalState & state, const Pos & pos, Value * * args, V } +/* concatMap = f: list: concatLists (map f list); */ +/* C++-version is to avoid allocating `mkApp', call `f' eagerly */ +static void prim_concatMap(EvalState & state, const Pos & pos, Value * * args, Value & v) +{ + state.forceFunction(*args[0], pos); + state.forceList(*args[1], pos); + auto len = args[1]->listSize(); + + Value vList; + state.mkList(vList, len); + + for (unsigned int n = 0; n < len; ++n) { + Value * vElem = args[1]->listElems()[n]; + state.forceValue(*vElem); + state.callFunction(*args[0], *vElem, *(vList.listElems()[n] = state.allocValue()), pos); + } + + state.concatLists(v, len, vList.listElems(), pos); +} + + + /************************************************************* * Integer arithmetic *************************************************************/ @@ -2212,6 +2252,7 @@ void EvalState::createBaseEnv() addPrimOp("__intersectAttrs", 2, prim_intersectAttrs); addPrimOp("__catAttrs", 2, prim_catAttrs); addPrimOp("__functionArgs", 1, prim_functionArgs); + addPrimOp("__mapAttrs", 2, prim_mapAttrs); // Lists addPrimOp("__isList", 1, prim_isList); @@ -2229,6 +2270,7 @@ void EvalState::createBaseEnv() addPrimOp("__genList", 2, prim_genList); addPrimOp("__sort", 2, prim_sort); addPrimOp("__partition", 2, prim_partition); + addPrimOp("__concatMap", 2, prim_concatMap); // Integer arithmetic addPrimOp("__add", 2, prim_add); diff --git a/tests/lang/eval-okay-concatmap.exp b/tests/lang/eval-okay-concatmap.exp new file mode 100644 index 000000000..3b8be7739 --- /dev/null +++ b/tests/lang/eval-okay-concatmap.exp @@ -0,0 +1 @@ +[ [ 1 3 5 7 9 ] [ "a" "z" "b" "z" ] ] diff --git a/tests/lang/eval-okay-concatmap.nix b/tests/lang/eval-okay-concatmap.nix new file mode 100644 index 000000000..97da5d37a --- /dev/null +++ b/tests/lang/eval-okay-concatmap.nix @@ -0,0 +1,5 @@ +with import ./lib.nix; + +[ (builtins.concatMap (x: if x / 2 * 2 == x then [] else [ x ]) (range 0 10)) + (builtins.concatMap (x: [x] ++ ["z"]) ["a" "b"]) +] diff --git a/tests/lang/eval-okay-mapattrs.exp b/tests/lang/eval-okay-mapattrs.exp new file mode 100644 index 000000000..3f113f17b --- /dev/null +++ b/tests/lang/eval-okay-mapattrs.exp @@ -0,0 +1 @@ +{ x = "x-foo"; y = "y-bar"; } diff --git a/tests/lang/eval-okay-mapattrs.nix b/tests/lang/eval-okay-mapattrs.nix new file mode 100644 index 000000000..f075b6275 --- /dev/null +++ b/tests/lang/eval-okay-mapattrs.nix @@ -0,0 +1,3 @@ +with import ./lib.nix; + +builtins.mapAttrs (name: value: name + "-" + value) { x = "foo"; y = "bar"; } From ee218f99cac96bfd48aeb8fa17c271bcc2515ab1 Mon Sep 17 00:00:00 2001 From: volth Date: Thu, 5 Jul 2018 11:58:15 +0000 Subject: [PATCH 2/3] primops.cc: fix comment --- src/libexpr/primops.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libexpr/primops.cc b/src/libexpr/primops.cc index 7ff57f0b0..aea2a3435 100644 --- a/src/libexpr/primops.cc +++ b/src/libexpr/primops.cc @@ -1356,7 +1356,7 @@ static void prim_functionArgs(EvalState & state, const Pos & pos, Value * * args } -/* Apply a function to every element of a list. */ +/* Apply a function to every element of an attribute set. */ static void prim_mapAttrs(EvalState & state, const Pos & pos, Value * * args, Value & v) { state.forceFunction(*args[0], pos); From 841747b0e6d9c391c13159aaeee44599f9508868 Mon Sep 17 00:00:00 2001 From: volth Date: Thu, 5 Jul 2018 12:37:37 +0000 Subject: [PATCH 3/3] prim_concatMap: allocate intermediate list on stack --- src/libexpr/primops.cc | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/src/libexpr/primops.cc b/src/libexpr/primops.cc index aea2a3435..9b4751970 100644 --- a/src/libexpr/primops.cc +++ b/src/libexpr/primops.cc @@ -1651,22 +1651,30 @@ static void prim_concatMap(EvalState & state, const Pos & pos, Value * * args, V { state.forceFunction(*args[0], pos); state.forceList(*args[1], pos); - auto len = args[1]->listSize(); + auto nrLists = args[1]->listSize(); - Value vList; - state.mkList(vList, len); + Value lists[nrLists]; + size_t len = 0; - for (unsigned int n = 0; n < len; ++n) { + for (unsigned int n = 0; n < nrLists; ++n) { Value * vElem = args[1]->listElems()[n]; state.forceValue(*vElem); - state.callFunction(*args[0], *vElem, *(vList.listElems()[n] = state.allocValue()), pos); + state.callFunction(*args[0], *vElem, lists[n], pos); + state.forceList(lists[n], pos); + len += lists[n].listSize(); } - state.concatLists(v, len, vList.listElems(), pos); + state.mkList(v, len); + auto out = v.listElems(); + for (unsigned int n = 0, pos = 0; n < nrLists; ++n) { + auto l = lists[n].listSize(); + if (l) + memcpy(out + pos, lists[n].listElems(), l * sizeof(Value *)); + pos += l; + } } - /************************************************************* * Integer arithmetic *************************************************************/