From 43a183120a2bb6d56d15cecf237f5882466cd6c7 Mon Sep 17 00:00:00 2001 From: Taeer Bar-Yam Date: Mon, 24 Nov 2025 20:23:10 +0100 Subject: [PATCH] libexpr: factor out functions for moving data to a new allocator --- src/libexpr/include/nix/expr/nixexpr.hh | 2 ++ src/libexpr/nixexpr.cc | 34 +++++++++++++------------ 2 files changed, 20 insertions(+), 16 deletions(-) diff --git a/src/libexpr/include/nix/expr/nixexpr.hh b/src/libexpr/include/nix/expr/nixexpr.hh index e4880a3fb..c7bfb7359 100644 --- a/src/libexpr/include/nix/expr/nixexpr.hh +++ b/src/libexpr/include/nix/expr/nixexpr.hh @@ -438,6 +438,7 @@ struct ExprAttrs : Expr std::shared_ptr bindInheritSources(EvalState & es, const std::shared_ptr & env); Env * buildInheritFromEnv(EvalState & state, Env & up); void showBindings(const SymbolTable & symbols, std::ostream & str) const; + void moveDataToAllocator(std::pmr::polymorphic_allocator & alloc); }; struct ExprList : Expr @@ -622,6 +623,7 @@ struct ExprCall : Expr virtual void resetCursedOr() override; virtual void warnIfCursedOr(const SymbolTable & symbols, const PosTable & positions) override; + void moveDataToAllocator(std::pmr::polymorphic_allocator & alloc); COMMON_METHODS }; diff --git a/src/libexpr/nixexpr.cc b/src/libexpr/nixexpr.cc index 37e22c466..6c9aa26dd 100644 --- a/src/libexpr/nixexpr.cc +++ b/src/libexpr/nixexpr.cc @@ -399,18 +399,19 @@ ExprAttrs::bindInheritSources(EvalState & es, const std::shared_ptr & alloc) +{ + AttrDefs newAttrs{std::move(*attrs), alloc}; + attrs.emplace(std::move(newAttrs), alloc); + DynamicAttrDefs newDynamicAttrs{std::move(*dynamicAttrs), alloc}; + dynamicAttrs.emplace(std::move(newDynamicAttrs), alloc); + if (inheritFromExprs) + inheritFromExprs = std::make_unique>(std::move(*inheritFromExprs), alloc); +} + void ExprAttrs::bindVars(EvalState & es, const std::shared_ptr & env) { - // Move storage into the Exprs arena - { - auto arena = es.mem.exprs.alloc; - AttrDefs newAttrs{std::move(*attrs), arena}; - attrs.emplace(std::move(newAttrs), arena); - DynamicAttrDefs newDynamicAttrs{std::move(*dynamicAttrs), arena}; - dynamicAttrs.emplace(std::move(newDynamicAttrs), arena); - if (inheritFromExprs) - inheritFromExprs = std::make_unique>(std::move(*inheritFromExprs), arena); - } + moveDataToAllocator(es.mem.exprs.alloc); if (es.debugRepl) es.exprEnvs.insert(std::make_pair(this, env)); @@ -484,14 +485,15 @@ void ExprLambda::bindVars(EvalState & es, const std::shared_ptr body->bindVars(es, newEnv); } +void ExprCall::moveDataToAllocator(std::pmr::polymorphic_allocator & alloc) +{ + std::pmr::vector newArgs{std::move(*args), alloc}; + args.emplace(std::move(newArgs), alloc); +} + void ExprCall::bindVars(EvalState & es, const std::shared_ptr & env) { - // Move storage into the Exprs arena - { - auto arena = es.mem.exprs.alloc; - std::pmr::vector newArgs{std::move(*args), arena}; - args.emplace(std::move(newArgs), arena); - } + moveDataToAllocator(es.mem.exprs.alloc); if (es.debugRepl) es.exprEnvs.insert(std::make_pair(this, env));