Skip to content

Commit 04a731b

Browse files
authored
Merge pull request #13387 from NaN-git/opt-listToAttrs
libexpr: don't allocate additional set in `builtins.listToAttrs`
2 parents 4bf23d2 + fa3d7e6 commit 04a731b

File tree

1 file changed

+32
-10
lines changed

1 file changed

+32
-10
lines changed

src/libexpr/primops.cc

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2962,25 +2962,47 @@ static void prim_listToAttrs(EvalState & state, const PosIdx pos, Value * * args
29622962
{
29632963
state.forceList(*args[0], pos, "while evaluating the argument passed to builtins.listToAttrs");
29642964

2965-
auto attrs = state.buildBindings(args[0]->listSize());
2965+
// Step 1. Sort the name-value attrsets in place using the memory we allocate for the result
2966+
size_t listSize = args[0]->listSize();
2967+
auto & bindings = *state.allocBindings(listSize);
2968+
using ElemPtr = decltype(&bindings[0].value);
29662969

2967-
std::set<Symbol> seen;
2968-
2969-
for (auto v2 : args[0]->listItems()) {
2970+
for (const auto & [n, v2] : enumerate(args[0]->listItems())) {
29702971
state.forceAttrs(*v2, pos, "while evaluating an element of the list passed to builtins.listToAttrs");
29712972

29722973
auto j = state.getAttr(state.sName, v2->attrs(), "in a {name=...; value=...;} pair");
29732974

29742975
auto name = state.forceStringNoCtx(*j->value, j->pos, "while evaluating the `name` attribute of an element of the list passed to builtins.listToAttrs");
2975-
29762976
auto sym = state.symbols.create(name);
2977-
if (seen.insert(sym).second) {
2978-
auto j2 = state.getAttr(state.sValue, v2->attrs(), "in a {name=...; value=...;} pair");
2979-
attrs.insert(sym, j2->value, j2->pos);
2980-
}
2977+
2978+
// (ab)use Attr to store a Value * * instead of a Value *, so that we can stabilize the sort using the Value * *
2979+
bindings[n] = Attr(sym, std::bit_cast<Value *>(&v2));
29812980
}
29822981

2983-
v.mkAttrs(attrs);
2982+
std::sort(&bindings[0], &bindings[listSize], [](const Attr & a, const Attr & b) {
2983+
// Note that .value is actually a Value * * that corresponds to the position in the list
2984+
return a < b || (!(a > b) && std::bit_cast<ElemPtr>(a.value) < std::bit_cast<ElemPtr>(b.value));
2985+
});
2986+
2987+
// Step 2. Unpack the bindings in place and skip name-value pairs with duplicate names
2988+
Symbol prev;
2989+
for (size_t n = 0; n < listSize; n++) {
2990+
auto attr = bindings[n];
2991+
if (prev == attr.name) {
2992+
continue;
2993+
}
2994+
// Note that .value is actually a Value * *; see earlier comments
2995+
Value * v2 = *std::bit_cast<ElemPtr>(attr.value);
2996+
2997+
auto j = state.getAttr(state.sValue, v2->attrs(), "in a {name=...; value=...;} pair");
2998+
prev = attr.name;
2999+
bindings.push_back({prev, j->value, j->pos});
3000+
}
3001+
// help GC and clear end of allocated array
3002+
for (size_t n = bindings.size(); n < listSize; n++) {
3003+
bindings[n] = Attr{};
3004+
}
3005+
v.mkAttrs(&bindings);
29843006
}
29853007

29863008
static RegisterPrimOp primop_listToAttrs({

0 commit comments

Comments
 (0)