Skip to content

Commit af0ac14

Browse files
authored
Merge pull request #14544 from Radvendii/evalmemory-alocbytes
move allocBytes() into EvalMemory
2 parents 918c1a9 + 7ff3cc6 commit af0ac14

File tree

6 files changed

+44
-36
lines changed

6 files changed

+44
-36
lines changed

src/libexpr/eval.cc

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -852,25 +852,26 @@ void Value::mkString(std::string_view s)
852852
mkStringNoCopy(StringData::make(s));
853853
}
854854

855-
Value::StringWithContext::Context * Value::StringWithContext::Context::fromBuilder(const NixStringContext & context)
855+
Value::StringWithContext::Context *
856+
Value::StringWithContext::Context::fromBuilder(const NixStringContext & context, EvalMemory & mem)
856857
{
857858
if (context.empty())
858859
return nullptr;
859860

860-
auto ctx = new (allocBytes(sizeof(Context) + context.size() * sizeof(value_type))) Context(context.size());
861+
auto ctx = new (mem.allocBytes(sizeof(Context) + context.size() * sizeof(value_type))) Context(context.size());
861862
std::ranges::transform(
862863
context, ctx->elems, [](const NixStringContextElem & elt) { return &StringData::make(elt.to_string()); });
863864
return ctx;
864865
}
865866

866-
void Value::mkString(std::string_view s, const NixStringContext & context)
867+
void Value::mkString(std::string_view s, const NixStringContext & context, EvalMemory & mem)
867868
{
868-
mkStringNoCopy(StringData::make(s), Value::StringWithContext::Context::fromBuilder(context));
869+
mkStringNoCopy(StringData::make(s), Value::StringWithContext::Context::fromBuilder(context, mem));
869870
}
870871

871-
void Value::mkStringMove(const StringData & s, const NixStringContext & context)
872+
void Value::mkStringMove(const StringData & s, const NixStringContext & context, EvalMemory & mem)
872873
{
873-
mkStringNoCopy(s, Value::StringWithContext::Context::fromBuilder(context));
874+
mkStringNoCopy(s, Value::StringWithContext::Context::fromBuilder(context, mem));
874875
}
875876

876877
void Value::mkPath(const SourcePath & path)
@@ -911,9 +912,9 @@ inline Value * EvalState::lookupVar(Env * env, const ExprVar & var, bool noEval)
911912
}
912913
}
913914

914-
ListBuilder::ListBuilder(size_t size)
915+
ListBuilder::ListBuilder(EvalMemory & mem, size_t size)
915916
: size(size)
916-
, elems(size <= 2 ? inlineElems : (Value **) allocBytes(size * sizeof(Value *)))
917+
, elems(size <= 2 ? inlineElems : (Value **) mem.allocBytes(size * sizeof(Value *)))
917918
{
918919
}
919920

@@ -953,7 +954,8 @@ void EvalState::mkStorePathString(const StorePath & p, Value & v)
953954
store->printStorePath(p),
954955
NixStringContext{
955956
NixStringContextElem::Opaque{.path = p},
956-
});
957+
},
958+
mem);
957959
}
958960

959961
std::string EvalState::mkOutputStringRaw(
@@ -975,7 +977,7 @@ void EvalState::mkOutputString(
975977
std::optional<StorePath> optStaticOutputPath,
976978
const ExperimentalFeatureSettings & xpSettings)
977979
{
978-
value.mkString(mkOutputStringRaw(b, optStaticOutputPath, xpSettings), NixStringContext{b});
980+
value.mkString(mkOutputStringRaw(b, optStaticOutputPath, xpSettings), NixStringContext{b}, mem);
979981
}
980982

981983
std::string EvalState::mkSingleDerivedPathStringRaw(const SingleDerivedPath & p)
@@ -1010,7 +1012,8 @@ void EvalState::mkSingleDerivedPathString(const SingleDerivedPath & p, Value & v
10101012
mkSingleDerivedPathStringRaw(p),
10111013
NixStringContext{
10121014
std::visit([](auto && v) -> NixStringContextElem { return v; }, p),
1013-
});
1015+
},
1016+
mem);
10141017
}
10151018

10161019
Value * Expr::maybeThunk(EvalState & state, Env & env)
@@ -2143,7 +2146,7 @@ void ExprConcatStrings::eval(EvalState & state, Env & env, Value & v)
21432146
tmp += part->size();
21442147
}
21452148
*tmp = '\0';
2146-
v.mkStringMove(resultStr, context);
2149+
v.mkStringMove(resultStr, context, state.mem);
21472150
}
21482151
}
21492152

src/libexpr/include/nix/expr/eval-inline.hh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace nix {
1212
* Note: Various places expect the allocated memory to be zeroed.
1313
*/
1414
[[gnu::always_inline]]
15-
inline void * allocBytes(size_t n)
15+
inline void * EvalMemory::allocBytes(size_t n)
1616
{
1717
void * p;
1818
#if NIX_USE_BOEHMGC

src/libexpr/include/nix/expr/eval.hh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,7 @@ public:
335335
EvalMemory & operator=(const EvalMemory &) = delete;
336336
EvalMemory & operator=(EvalMemory &&) = delete;
337337

338+
inline void * allocBytes(size_t n);
338339
inline Value * allocValue();
339340
inline Env & allocEnv(size_t size);
340341

@@ -348,7 +349,7 @@ public:
348349
ListBuilder buildList(size_t size)
349350
{
350351
stats.nrListElems += size;
351-
return ListBuilder(size);
352+
return ListBuilder(*this, size);
352353
}
353354

354355
const Statistics & getStats() const &

src/libexpr/include/nix/expr/value.hh

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ class PosIdx;
8888
struct Pos;
8989
class StorePath;
9090
class EvalState;
91+
class EvalMemory;
9192
class XMLWriter;
9293
class Printer;
9394

@@ -161,7 +162,7 @@ class ListBuilder
161162
Value * inlineElems[2] = {nullptr, nullptr};
162163
public:
163164
Value ** elems;
164-
ListBuilder(size_t size);
165+
ListBuilder(EvalMemory & mem, size_t size);
165166

166167
// NOTE: Can be noexcept because we are just copying integral values and
167168
// raw pointers.
@@ -364,7 +365,7 @@ struct ValueBase
364365
/**
365366
* @return null pointer when context.empty()
366367
*/
367-
static Context * fromBuilder(const NixStringContext & context);
368+
static Context * fromBuilder(const NixStringContext & context, EvalMemory & mem);
368369
};
369370

370371
/**
@@ -1148,9 +1149,9 @@ public:
11481149

11491150
void mkString(std::string_view s);
11501151

1151-
void mkString(std::string_view s, const NixStringContext & context);
1152+
void mkString(std::string_view s, const NixStringContext & context, EvalMemory & mem);
11521153

1153-
void mkStringMove(const StringData & s, const NixStringContext & context);
1154+
void mkStringMove(const StringData & s, const NixStringContext & context, EvalMemory & mem);
11541155

11551156
void mkPath(const SourcePath & path);
11561157

src/libexpr/primops.cc

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,8 @@ void derivationToValue(
222222
path2,
223223
{
224224
NixStringContextElem::DrvDeep{.drvPath = storePath},
225-
});
225+
},
226+
state.mem);
226227
attrs.alloc(state.s.name).mkString(drv.env["name"]);
227228

228229
auto list = state.buildList(drv.outputs.size());
@@ -1811,7 +1812,8 @@ static void derivationStrictInternal(EvalState & state, std::string_view drvName
18111812
drvPathS,
18121813
{
18131814
NixStringContextElem::DrvDeep{.drvPath = drvPath},
1814-
});
1815+
},
1816+
state.mem);
18151817
for (auto & i : drv.outputs)
18161818
mkOutputString(state, result, drvPath, i);
18171819

@@ -1864,7 +1866,7 @@ static void prim_toPath(EvalState & state, const PosIdx pos, Value ** args, Valu
18641866
NixStringContext context;
18651867
auto path =
18661868
state.coerceToPath(pos, *args[0], context, "while evaluating the first argument passed to builtins.toPath");
1867-
v.mkString(path.path.abs(), context);
1869+
v.mkString(path.path.abs(), context, state.mem);
18681870
}
18691871

18701872
static RegisterPrimOp primop_toPath({
@@ -1907,7 +1909,7 @@ static void prim_storePath(EvalState & state, const PosIdx pos, Value ** args, V
19071909
if (!settings.readOnlyMode)
19081910
state.store->ensurePath(path2);
19091911
context.insert(NixStringContextElem::Opaque{.path = path2});
1910-
v.mkString(path.abs(), context);
1912+
v.mkString(path.abs(), context, state.mem);
19111913
}
19121914

19131915
static RegisterPrimOp primop_storePath({
@@ -1989,7 +1991,8 @@ static void prim_baseNameOf(EvalState & state, const PosIdx pos, Value ** args,
19891991
v.mkString(
19901992
legacyBaseNameOf(*state.coerceToString(
19911993
pos, *args[0], context, "while evaluating the first argument passed to builtins.baseNameOf", false, false)),
1992-
context);
1994+
context,
1995+
state.mem);
19931996
}
19941997

19951998
static RegisterPrimOp primop_baseNameOf({
@@ -2025,11 +2028,11 @@ static void prim_dirOf(EvalState & state, const PosIdx pos, Value ** args, Value
20252028
pos, *args[0], context, "while evaluating the first argument passed to 'builtins.dirOf'", false, false);
20262029
auto pos = path->rfind('/');
20272030
if (pos == path->npos)
2028-
v.mkStringMove("."_sds, context);
2031+
v.mkStringMove("."_sds, context, state.mem);
20292032
else if (pos == 0)
2030-
v.mkStringMove("/"_sds, context);
2033+
v.mkStringMove("/"_sds, context, state.mem);
20312034
else
2032-
v.mkString(path->substr(0, pos), context);
2035+
v.mkString(path->substr(0, pos), context, state.mem);
20332036
}
20342037
}
20352038

@@ -2071,7 +2074,7 @@ static void prim_readFile(EvalState & state, const PosIdx pos, Value ** args, Va
20712074
.path = std::move((StorePath &&) p),
20722075
});
20732076
}
2074-
v.mkString(s, context);
2077+
v.mkString(s, context, state.mem);
20752078
}
20762079

20772080
static RegisterPrimOp primop_readFile({
@@ -2467,7 +2470,7 @@ static void prim_toXML(EvalState & state, const PosIdx pos, Value ** args, Value
24672470
std::ostringstream out;
24682471
NixStringContext context;
24692472
printValueAsXML(state, true, false, *args[0], out, context, pos);
2470-
v.mkString(out.view(), context);
2473+
v.mkString(out.view(), context, state.mem);
24712474
}
24722475

24732476
static RegisterPrimOp primop_toXML({
@@ -2575,7 +2578,7 @@ static void prim_toJSON(EvalState & state, const PosIdx pos, Value ** args, Valu
25752578
std::ostringstream out;
25762579
NixStringContext context;
25772580
printValueAsJSON(state, true, *args[0], pos, out, context);
2578-
v.mkString(out.view(), context);
2581+
v.mkString(out.view(), context, state.mem);
25792582
}
25802583

25812584
static RegisterPrimOp primop_toJSON({
@@ -4404,7 +4407,7 @@ static void prim_toString(EvalState & state, const PosIdx pos, Value ** args, Va
44044407
NixStringContext context;
44054408
auto s = state.coerceToString(
44064409
pos, *args[0], context, "while evaluating the first argument passed to builtins.toString", true, false);
4407-
v.mkString(*s, context);
4410+
v.mkString(*s, context, state.mem);
44084411
}
44094412

44104413
static RegisterPrimOp primop_toString({
@@ -4477,7 +4480,7 @@ static void prim_substring(EvalState & state, const PosIdx pos, Value ** args, V
44774480
auto s = state.coerceToString(
44784481
pos, *args[2], context, "while evaluating the third argument (the string) passed to builtins.substring");
44794482

4480-
v.mkString(NixUInt(start) >= s->size() ? "" : s->substr(start, _len), context);
4483+
v.mkString(NixUInt(start) >= s->size() ? "" : s->substr(start, _len), context, state.mem);
44814484
}
44824485

44834486
static RegisterPrimOp primop_substring({
@@ -4875,7 +4878,7 @@ static void prim_concatStringsSep(EvalState & state, const PosIdx pos, Value **
48754878
"while evaluating one element of the list of strings to concat passed to builtins.concatStringsSep");
48764879
}
48774880

4878-
v.mkString(res, context);
4881+
v.mkString(res, context, state.mem);
48794882
}
48804883

48814884
static RegisterPrimOp primop_concatStringsSep({
@@ -4950,7 +4953,7 @@ static void prim_replaceStrings(EvalState & state, const PosIdx pos, Value ** ar
49504953
}
49514954
}
49524955

4953-
v.mkString(res, context);
4956+
v.mkString(res, context, state.mem);
49544957
}
49554958

49564959
static RegisterPrimOp primop_replaceStrings({

src/libexpr/primops/context.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ static void prim_unsafeDiscardOutputDependency(EvalState & state, const PosIdx p
6969
}
7070
}
7171

72-
v.mkString(*s, context2);
72+
v.mkString(*s, context2, state.mem);
7373
}
7474

7575
static RegisterPrimOp primop_unsafeDiscardOutputDependency(
@@ -137,7 +137,7 @@ static void prim_addDrvOutputDependencies(EvalState & state, const PosIdx pos, V
137137
context.begin()->raw)}),
138138
};
139139

140-
v.mkString(*s, context2);
140+
v.mkString(*s, context2, state.mem);
141141
}
142142

143143
static RegisterPrimOp primop_addDrvOutputDependencies(
@@ -321,7 +321,7 @@ static void prim_appendContext(EvalState & state, const PosIdx pos, Value ** arg
321321
}
322322
}
323323

324-
v.mkString(orig, context);
324+
v.mkString(orig, context, state.mem);
325325
}
326326

327327
static RegisterPrimOp primop_appendContext({.name = "__appendContext", .arity = 2, .fun = prim_appendContext});

0 commit comments

Comments
 (0)