1
1
Fork 0
mirror of https://github.com/NixOS/nix.git synced 2025-11-09 12:06:01 +01:00

libexpr: Remove vString* Values from EvalState

EvalState is too big and cluttered. These strings
can be private constant statics.
This commit is contained in:
Sergei Zimmerman 2025-09-12 23:44:52 +03:00
parent 377b60ee9b
commit f4c38278ca
No known key found for this signature in database
3 changed files with 36 additions and 22 deletions

View file

@ -284,11 +284,6 @@ EvalState::EvalState(
static_assert(sizeof(Env) <= 16, "environment must be <= 16 bytes"); static_assert(sizeof(Env) <= 16, "environment must be <= 16 bytes");
vStringRegular.mkStringNoCopy("regular");
vStringDirectory.mkStringNoCopy("directory");
vStringSymlink.mkStringNoCopy("symlink");
vStringUnknown.mkStringNoCopy("unknown");
/* Construct the Nix expression search path. */ /* Construct the Nix expression search path. */
assert(lookupPath.elements.empty()); assert(lookupPath.elements.empty());
if (!settings.pureEval) { if (!settings.pureEval) {

View file

@ -315,15 +315,6 @@ public:
*/ */
RepairFlag repair; RepairFlag repair;
/** `"regular"` */
Value vStringRegular;
/** `"directory"` */
Value vStringDirectory;
/** `"symlink"` */
Value vStringSymlink;
/** `"unknown"` */
Value vStringUnknown;
/** /**
* The accessor corresponding to `store`. * The accessor corresponding to `store`.
*/ */

View file

@ -2243,19 +2243,45 @@ static RegisterPrimOp primop_hashFile({
.fun = prim_hashFile, .fun = prim_hashFile,
}); });
static Value * fileTypeToString(EvalState & state, SourceAccessor::Type type) static const Value & fileTypeToString(EvalState & state, SourceAccessor::Type type)
{ {
return type == SourceAccessor::Type::tRegular ? &state.vStringRegular struct Constants
: type == SourceAccessor::Type::tDirectory ? &state.vStringDirectory {
: type == SourceAccessor::Type::tSymlink ? &state.vStringSymlink Value regular;
: &state.vStringUnknown; Value directory;
Value symlink;
Value unknown;
};
static const Constants stringValues = []() {
Constants res;
res.regular.mkStringNoCopy("regular");
res.directory.mkStringNoCopy("directory");
res.symlink.mkStringNoCopy("symlink");
res.unknown.mkStringNoCopy("unknown");
return res;
}();
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wswitch-enum"
using enum SourceAccessor::Type;
switch (type) {
case tRegular:
return stringValues.regular;
case tDirectory:
return stringValues.directory;
case tSymlink:
return stringValues.symlink;
default:
return stringValues.unknown;
}
} }
static void prim_readFileType(EvalState & state, const PosIdx pos, Value ** args, Value & v) static void prim_readFileType(EvalState & state, const PosIdx pos, Value ** args, Value & v)
{ {
auto path = realisePath(state, pos, *args[0], std::nullopt); auto path = realisePath(state, pos, *args[0], std::nullopt);
/* Retrieve the directory entry type and stringize it. */ /* Retrieve the directory entry type and stringize it. */
v = *fileTypeToString(state, path.lstat().type); v = fileTypeToString(state, path.lstat().type);
} }
static RegisterPrimOp primop_readFileType({ static RegisterPrimOp primop_readFileType({
@ -2299,7 +2325,9 @@ static void prim_readDir(EvalState & state, const PosIdx pos, Value ** args, Val
} else { } else {
// This branch of the conditional is much more likely. // This branch of the conditional is much more likely.
// Here we just stringize the directory entry type. // Here we just stringize the directory entry type.
attrs.insert(state.symbols.create(name), fileTypeToString(state, *type)); // N.B. const_cast here is ok, because these values will never be modified, since
// only thunks are mutable - other types do not change once constructed.
attrs.insert(state.symbols.create(name), const_cast<Value *>(&fileTypeToString(state, *type)));
} }
} }
@ -2674,7 +2702,7 @@ bool EvalState::callPathFilter(Value * filterFun, const SourcePath & path, PosId
arg1.mkString(path.path.abs()); arg1.mkString(path.path.abs());
// assert that type is not "unknown" // assert that type is not "unknown"
Value * args[]{&arg1, fileTypeToString(*this, st.type)}; Value * args[]{&arg1, const_cast<Value *>(&fileTypeToString(*this, st.type))};
Value res; Value res;
callFunction(*filterFun, args, res, pos); callFunction(*filterFun, args, res, pos);