about summary refs log tree commit diff
path: root/third_party/nix/src/libexpr/attr-set.cc
diff options
context:
space:
mode:
authorVincent Ambo <mail@tazj.in>2020-07-16T18·31+0100
committertazjin <mail@tazj.in>2020-07-16T18·51+0000
commitcb3d9675084f735c099c211edc4c8472f97a0578 (patch)
tree4888a8fdc264735fccca27ec86b25f5b69446724 /third_party/nix/src/libexpr/attr-set.cc
parent1ba5aa293bac0cd07421d5d1ba92c7fd8e2a5754 (diff)
refactor(3p/nix/libexpr): Use range insertion to merge nix::Bindings r/1320
Instead of manually iterating over the two bindings to be combined,
this adds a new static method on the Bindings class which merges two
attribute sets by calling the range insertion operator over them.

In some anecdotal tests, this can lead to a ~10% speed bump -
depending on the specific operation.

Change-Id: I5dea03b0589a83a789d3a8a0fc81d0d9e6598371
Reviewed-on: https://cl.tvl.fyi/c/depot/+/1216
Tested-by: BuildkiteCI
Reviewed-by: glittershark <grfn@gws.fyi>
Diffstat (limited to 'third_party/nix/src/libexpr/attr-set.cc')
-rw-r--r--third_party/nix/src/libexpr/attr-set.cc24
1 files changed, 16 insertions, 8 deletions
diff --git a/third_party/nix/src/libexpr/attr-set.cc b/third_party/nix/src/libexpr/attr-set.cc
index d44df990ad1e..f1f40454c3f8 100644
--- a/third_party/nix/src/libexpr/attr-set.cc
+++ b/third_party/nix/src/libexpr/attr-set.cc
@@ -33,7 +33,7 @@ void Bindings::push_back(const Attr& attr) {
   }
 }
 
-size_t Bindings::size() { return attributes_.size(); }
+size_t Bindings::size() const { return attributes_.size(); }
 
 bool Bindings::empty() { return attributes_.empty(); }
 
@@ -56,13 +56,6 @@ Bindings::iterator Bindings::begin() { return attributes_.begin(); }
 
 Bindings::iterator Bindings::end() { return attributes_.end(); }
 
-void Bindings::merge(const Bindings& other) {
-  assert(this != &ZERO_BINDINGS);
-  for (auto& [key, value] : other.attributes_) {
-    this->attributes_.insert_or_assign(key, value);
-  }
-}
-
 Bindings* Bindings::NewGC(size_t capacity) {
   if (capacity == 0) {
     return &ZERO_BINDINGS;
@@ -71,6 +64,21 @@ Bindings* Bindings::NewGC(size_t capacity) {
   return new (GC) Bindings;
 }
 
+Bindings* Bindings::Merge(const Bindings& lhs, const Bindings& rhs) {
+  auto bindings = NewGC(lhs.size() + rhs.size());
+
+  // Values are merged by inserting the entire iterator range of both
+  // input sets. The right-hand set (the values of which take
+  // precedence) is inserted *first* because the range insertion
+  // method does not override values.
+  bindings->attributes_.insert(rhs.attributes_.cbegin(),
+                               rhs.attributes_.cend());
+  bindings->attributes_.insert(lhs.attributes_.cbegin(),
+                               lhs.attributes_.cend());
+
+  return bindings;
+}
+
 void EvalState::mkAttrs(Value& v, size_t capacity) {
   clearValue(v);
   v.type = tAttrs;