Get rid of the parse tree cache

Since we already cache files in normal form (fileEvalCache), caching
parse trees is redundant.

Note that getting rid of this cache doesn't actually save much memory
at the moment, because parse trees are currently not freed / GC'ed.
This commit is contained in:
Eelco Dolstra 2013-09-03 12:56:33 +02:00
parent 57d18df7d0
commit 6f809194d7
6 changed files with 35 additions and 34 deletions

View File

@ -440,26 +440,30 @@ Value * ExprPath::maybeThunk(EvalState & state, Env & env)
void EvalState::evalFile(const Path & path, Value & v) void EvalState::evalFile(const Path & path, Value & v)
{ {
FileEvalCache::iterator i = fileEvalCache.find(path); Path path2 = resolveExprPath(path);
if (i == fileEvalCache.end()) {
startNest(nest, lvlTalkative, format("evaluating file `%1%'") % path); FileEvalCache::iterator i = fileEvalCache.find(path2);
Expr * e = parseExprFromFile(path); if (i != fileEvalCache.end()) {
try {
eval(e, v);
} catch (Error & e) {
addErrorPrefix(e, "while evaluating the file `%1%':\n", path);
throw;
}
fileEvalCache[path] = v;
} else
v = i->second; v = i->second;
return;
}
startNest(nest, lvlTalkative, format("evaluating file `%1%'") % path2);
Expr * e = parseExprFromFile(path2);
try {
eval(e, v);
} catch (Error & e) {
addErrorPrefix(e, "while evaluating the file `%1%':\n", path2);
throw;
}
fileEvalCache[path2] = v;
//if (path != path2) fileEvalCache[path2] = v;
} }
void EvalState::resetFileCache() void EvalState::resetFileCache()
{ {
fileEvalCache.clear(); fileEvalCache.clear();
parseTrees.clear();
} }

View File

@ -103,9 +103,6 @@ public:
private: private:
SrcToStore srcToStore; SrcToStore srcToStore;
/* A cache from path names to parse trees. */
std::map<Path, Expr *> parseTrees;
/* A cache from path names to values. */ /* A cache from path names to values. */
#if HAVE_BOEHMGC #if HAVE_BOEHMGC
typedef std::map<Path, Value, std::less<Path>, gc_allocator<std::pair<const Path, Value> > > FileEvalCache; typedef std::map<Path, Value, std::less<Path>, gc_allocator<std::pair<const Path, Value> > > FileEvalCache;
@ -125,9 +122,8 @@ public:
void addToSearchPath(const string & s); void addToSearchPath(const string & s);
/* Parse a Nix expression from the specified file. If `path' /* Parse a Nix expression from the specified file. */
refers to a directory, then "/default.nix" is appended. */ Expr * parseExprFromFile(const Path & path);
Expr * parseExprFromFile(Path path);
/* Parse a Nix expression from the specified string. */ /* Parse a Nix expression from the specified string. */
Expr * parseExprFromString(const string & s, const Path & basePath, StaticEnv & staticEnv); Expr * parseExprFromString(const string & s, const Path & basePath, StaticEnv & staticEnv);
@ -278,4 +274,8 @@ private:
string showType(const Value & v); string showType(const Value & v);
/* If `path' refers to a directory, then append "/default.nix". */
Path resolveExprPath(Path path);
} }

View File

@ -505,7 +505,7 @@ Expr * EvalState::parse(const char * text,
} }
Expr * EvalState::parseExprFromFile(Path path) Path resolveExprPath(Path path)
{ {
assert(path[0] == '/'); assert(path[0] == '/');
@ -523,15 +523,13 @@ Expr * EvalState::parseExprFromFile(Path path)
if (S_ISDIR(st.st_mode)) if (S_ISDIR(st.st_mode))
path = canonPath(path + "/default.nix"); path = canonPath(path + "/default.nix");
/* Read and parse the input file, unless it's already in the parse return path;
tree cache. */ }
Expr * e = parseTrees[path];
if (!e) {
e = parse(readFile(path).c_str(), path, dirOf(path), staticBaseEnv);
parseTrees[path] = e;
}
return e;
Expr * EvalState::parseExprFromFile(const Path & path)
{
return parse(readFile(path).c_str(), path, dirOf(path), staticBaseEnv);
} }

View File

@ -82,8 +82,7 @@ static void prim_import(EvalState & state, Value * * args, Value & v)
} }
w.attrs->sort(); w.attrs->sort();
Value fun; Value fun;
state.mkThunk_(fun, state.evalFile(state.findFile("nix/imported-drv-to-derivation.nix"), fun);
state.parseExprFromFile(state.findFile("nix/imported-drv-to-derivation.nix")));
state.forceFunction(fun); state.forceFunction(fun);
mkApp(v, fun, w); mkApp(v, fun, w);
state.forceAttrs(v); state.forceAttrs(v);
@ -1263,7 +1262,7 @@ void EvalState::createBaseEnv()
/* Add a wrapper around the derivation primop that computes the /* Add a wrapper around the derivation primop that computes the
`drvPath' and `outPath' attributes lazily. */ `drvPath' and `outPath' attributes lazily. */
mkThunk_(v, parseExprFromFile(findFile("nix/derivation.nix"))); evalFile(findFile("nix/derivation.nix"), v);
addConstant("derivation", v); addConstant("derivation", v);
/* Now that we've added all primops, sort the `builtins' attribute /* Now that we've added all primops, sort the `builtins' attribute

View File

@ -131,7 +131,7 @@ static void getAllExprs(EvalState & state,
if (hasSuffix(attrName, ".nix")) if (hasSuffix(attrName, ".nix"))
attrName = string(attrName, 0, attrName.size() - 4); attrName = string(attrName, 0, attrName.size() - 4);
attrs.attrs[state.symbols.create(attrName)] = attrs.attrs[state.symbols.create(attrName)] =
ExprAttrs::AttrDef(state.parseExprFromFile(absPath(path2)), noPos); ExprAttrs::AttrDef(state.parseExprFromFile(resolveExprPath(absPath(path2))), noPos);
} }
else else
/* `path2' is a directory (with no default.nix in it); /* `path2' is a directory (with no default.nix in it);
@ -143,7 +143,7 @@ static void getAllExprs(EvalState & state,
static Expr * loadSourceExpr(EvalState & state, const Path & path) static Expr * loadSourceExpr(EvalState & state, const Path & path)
{ {
if (isNixExpr(path)) return state.parseExprFromFile(absPath(path)); if (isNixExpr(path)) return state.parseExprFromFile(resolveExprPath(absPath(path)));
/* The path is a directory. Put the Nix expressions in the /* The path is a directory. Put the Nix expressions in the
directory in an attribute set, with the file name of each directory in an attribute set, with the file name of each

View File

@ -160,7 +160,7 @@ void run(Strings args)
files.push_back("./default.nix"); files.push_back("./default.nix");
foreach (Strings::iterator, i, files) { foreach (Strings::iterator, i, files) {
Expr * e = state.parseExprFromFile(lookupFileArg(state, *i)); Expr * e = state.parseExprFromFile(resolveExprPath(lookupFileArg(state, *i)));
processExpr(state, attrPaths, parseOnly, strict, autoArgs, processExpr(state, attrPaths, parseOnly, strict, autoArgs,
evalOnly, xmlOutput, xmlOutputSourceLocation, e); evalOnly, xmlOutput, xmlOutputSourceLocation, e);
} }