@@ -194,6 +194,15 @@ static Symbol getName(const AttrName & name, EvalState & state, Env & env)
194194
195195static constexpr size_t BASE_ENV_SIZE = 128 ;
196196
197+ EvalMemory::EvalMemory ()
198+ #if NIX_USE_BOEHMGC
199+ : valueAllocCache(std::allocate_shared<void *>(traceable_allocator<void *>(), nullptr ))
200+ , env1AllocCache(std::allocate_shared<void *>(traceable_allocator<void *>(), nullptr ))
201+ #endif
202+ {
203+ assertGCInitialized ();
204+ }
205+
197206EvalState::EvalState (
198207 const LookupPath & lookupPathFromArguments,
199208 ref<Store> store,
@@ -274,12 +283,10 @@ EvalState::EvalState(
274283 , fileEvalCache(make_ref<decltype (fileEvalCache)::element_type>())
275284 , regexCache(makeRegexCache())
276285#if NIX_USE_BOEHMGC
277- , valueAllocCache(std::allocate_shared<void *>(traceable_allocator<void *>(), nullptr ))
278- , env1AllocCache(std::allocate_shared<void *>(traceable_allocator<void *>(), nullptr ))
279- , baseEnvP(std::allocate_shared<Env *>(traceable_allocator<Env *>(), &allocEnv (BASE_ENV_SIZE)))
286+ , baseEnvP(std::allocate_shared<Env *>(traceable_allocator<Env *>(), &mem.allocEnv(BASE_ENV_SIZE)))
280287 , baseEnv(**baseEnvP)
281288#else
282- , baseEnv(allocEnv(BASE_ENV_SIZE))
289+ , baseEnv(mem. allocEnv(BASE_ENV_SIZE))
283290#endif
284291 , staticBaseEnv{std::make_shared<StaticEnv>(nullptr , nullptr )}
285292{
@@ -288,8 +295,6 @@ EvalState::EvalState(
288295
289296 countCalls = getEnv (" NIX_COUNT_CALLS" ).value_or (" 0" ) != " 0" ;
290297
291- assertGCInitialized ();
292-
293298 static_assert (sizeof (Env) <= 16 , " environment must be <= 16 bytes" );
294299 static_assert (sizeof (Counter) == 64 , " counters must be 64 bytes" );
295300
@@ -885,11 +890,10 @@ inline Value * EvalState::lookupVar(Env * env, const ExprVar & var, bool noEval)
885890 }
886891}
887892
888- ListBuilder::ListBuilder (EvalState & state, size_t size)
893+ ListBuilder::ListBuilder (size_t size)
889894 : size(size)
890895 , elems(size <= 2 ? inlineElems : (Value **) allocBytes(size * sizeof (Value *)))
891896{
892- state.nrListElems += size;
893897}
894898
895899Value * EvalState::getBool (bool b)
@@ -1183,7 +1187,7 @@ void ExprPath::eval(EvalState & state, Env & env, Value & v)
11831187
11841188Env * ExprAttrs::buildInheritFromEnv (EvalState & state, Env & up)
11851189{
1186- Env & inheritEnv = state.allocEnv (inheritFromExprs->size ());
1190+ Env & inheritEnv = state.mem . allocEnv (inheritFromExprs->size ());
11871191 inheritEnv.up = &up;
11881192
11891193 Displacement displ = 0 ;
@@ -1202,7 +1206,7 @@ void ExprAttrs::eval(EvalState & state, Env & env, Value & v)
12021206 if (recursive) {
12031207 /* Create a new environment that contains the attributes in
12041208 this `rec'. */
1205- Env & env2 (state.allocEnv (attrs.size ()));
1209+ Env & env2 (state.mem . allocEnv (attrs.size ()));
12061210 env2.up = &env;
12071211 dynamicEnv = &env2;
12081212 Env * inheritEnv = inheritFromExprs ? buildInheritFromEnv (state, env2) : nullptr ;
@@ -1294,7 +1298,7 @@ void ExprLet::eval(EvalState & state, Env & env, Value & v)
12941298{
12951299 /* Create a new environment that contains the attributes in this
12961300 `let'. */
1297- Env & env2 (state.allocEnv (attrs->attrs .size ()));
1301+ Env & env2 (state.mem . allocEnv (attrs->attrs .size ()));
12981302 env2.up = &env;
12991303
13001304 Env * inheritEnv = attrs->inheritFromExprs ? attrs->buildInheritFromEnv (state, env2) : nullptr ;
@@ -1500,7 +1504,7 @@ void EvalState::callFunction(Value & fun, std::span<Value *> args, Value & vRes,
15001504 ExprLambda & lambda (*vCur.lambda ().fun );
15011505
15021506 auto size = (!lambda.arg ? 0 : 1 ) + (lambda.hasFormals () ? lambda.formals ->formals .size () : 0 );
1503- Env & env2 (allocEnv (size));
1507+ Env & env2 (mem. allocEnv (size));
15041508 env2.up = vCur.lambda ().env ;
15051509
15061510 Displacement displ = 0 ;
@@ -1789,7 +1793,7 @@ values, or passed explicitly with '--arg' or '--argstr'. See
17891793
17901794void ExprWith::eval (EvalState & state, Env & env, Value & v)
17911795{
1792- Env & env2 (state.allocEnv (1 ));
1796+ Env & env2 (state.mem . allocEnv (1 ));
17931797 env2.up = &env;
17941798 env2.values [0 ] = attrs->maybeThunk (state, env);
17951799
@@ -2916,10 +2920,12 @@ void EvalState::printStatistics()
29162920 std::chrono::microseconds cpuTimeDuration = getCpuUserTime ();
29172921 float cpuTime = std::chrono::duration_cast<std::chrono::duration<float >>(cpuTimeDuration).count ();
29182922
2919- uint64_t bEnvs = nrEnvs * sizeof (Env) + nrValuesInEnvs * sizeof (Value *);
2920- uint64_t bLists = nrListElems * sizeof (Value *);
2921- uint64_t bValues = nrValues * sizeof (Value);
2922- uint64_t bAttrsets = nrAttrsets * sizeof (Bindings) + nrAttrsInAttrsets * sizeof (Attr);
2923+ auto & memstats = mem.getStats ();
2924+
2925+ uint64_t bEnvs = memstats.nrEnvs * sizeof (Env) + memstats.nrValuesInEnvs * sizeof (Value *);
2926+ uint64_t bLists = memstats.nrListElems * sizeof (Value *);
2927+ uint64_t bValues = memstats.nrValues * sizeof (Value);
2928+ uint64_t bAttrsets = memstats.nrAttrsets * sizeof (Bindings) + memstats.nrAttrsInAttrsets * sizeof (Attr);
29232929
29242930#if NIX_USE_BOEHMGC
29252931 GC_word heapSize, totalBytes;
@@ -2945,28 +2951,28 @@ void EvalState::printStatistics()
29452951#endif
29462952 };
29472953 topObj[" envs" ] = {
2948- {" number" , nrEnvs.load ()},
2949- {" elements" , nrValuesInEnvs.load ()},
2954+ {" number" , memstats. nrEnvs .load ()},
2955+ {" elements" , memstats. nrValuesInEnvs .load ()},
29502956 {" bytes" , bEnvs},
29512957 };
29522958 topObj[" nrExprs" ] = Expr::nrExprs.load ();
29532959 topObj[" list" ] = {
2954- {" elements" , nrListElems.load ()},
2960+ {" elements" , memstats. nrListElems .load ()},
29552961 {" bytes" , bLists},
29562962 {" concats" , nrListConcats.load ()},
29572963 };
29582964 topObj[" values" ] = {
2959- {" number" , nrValues.load ()},
2965+ {" number" , memstats. nrValues .load ()},
29602966 {" bytes" , bValues},
29612967 };
29622968 topObj[" symbols" ] = {
29632969 {" number" , symbols.size ()},
29642970 {" bytes" , symbols.totalSize ()},
29652971 };
29662972 topObj[" sets" ] = {
2967- {" number" , nrAttrsets.load ()},
2973+ {" number" , memstats. nrAttrsets .load ()},
29682974 {" bytes" , bAttrsets},
2969- {" elements" , nrAttrsInAttrsets.load ()},
2975+ {" elements" , memstats. nrAttrsInAttrsets .load ()},
29702976 };
29712977 topObj[" sizes" ] = {
29722978 {" Env" , sizeof (Env)},
0 commit comments