From 17daec0b832205df781f6443292667c9aa617047 Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Tue, 4 Jan 2022 19:23:11 +0100 Subject: [PATCH] Move empty attrset optimisation --- src/libexpr/attr-set.cc | 8 +++----- src/libexpr/eval.cc | 3 +-- src/libexpr/eval.hh | 2 +- 3 files changed, 5 insertions(+), 8 deletions(-) diff --git a/src/libexpr/attr-set.cc b/src/libexpr/attr-set.cc index d74243f45..fc971957d 100644 --- a/src/libexpr/attr-set.cc +++ b/src/libexpr/attr-set.cc @@ -12,6 +12,8 @@ namespace nix { structure. */ Bindings * EvalState::allocBindings(size_t capacity) { + if (capacity == 0) + return &emptyBindings; if (capacity > std::numeric_limits::max()) throw Error("attribute set of size %d is too big", capacity); return new (allocBytes(sizeof(Bindings) + sizeof(Attr) * capacity)) Bindings((Bindings::size_t) capacity); @@ -20,10 +22,6 @@ Bindings * EvalState::allocBindings(size_t capacity) void EvalState::mkAttrs(Value & v, size_t capacity) { - if (capacity == 0) { - v = vEmptySet; - return; - } v.mkAttrs(allocBindings(capacity)); nrAttrsets++; nrAttrsInAttrsets += capacity; @@ -63,7 +61,7 @@ Value & BindingsBuilder::alloc(std::string_view name, ptr pos) void Bindings::sort() { - std::sort(begin(), end()); + if (size_) std::sort(begin(), end()); } diff --git a/src/libexpr/eval.cc b/src/libexpr/eval.cc index cd7d19747..112690189 100644 --- a/src/libexpr/eval.cc +++ b/src/libexpr/eval.cc @@ -413,6 +413,7 @@ EvalState::EvalState( , sSelf(symbols.create("self")) , sEpsilon(symbols.create("")) , repair(NoRepair) + , emptyBindings(0) , store(store) , buildStore(buildStore ? buildStore : store) , regexCache(makeRegexCache()) @@ -454,8 +455,6 @@ EvalState::EvalState( } } - vEmptySet.mkAttrs(allocBindings(0)); - createBaseEnv(); } diff --git a/src/libexpr/eval.hh b/src/libexpr/eval.hh index c7f74d7b7..c0d88201a 100644 --- a/src/libexpr/eval.hh +++ b/src/libexpr/eval.hh @@ -91,7 +91,7 @@ public: mode. */ std::optional allowedPaths; - Value vEmptySet; + Bindings emptyBindings; /* Store used to materialise .drv files. */ const ref store;