Author: eelco
Date: Sat Aug  6 19:45:43 2011
New Revision: 28204
URL: https://svn.nixos.org/websvn/nix/?rev=28204&sc=1

Log:
* Cache the result of file evaluation (i.e, memoize evalFile()).  This
  prevents files from being evaluated and stored as values multiple
  times.  For instance, evaluation of the ‘system’ attribute in NixOS
  causes ‘nixpkgs/pkgs/lib/lists.nix’ to be evaluated 2019 times.

  Caching gives a modest speedup and a decent memory footprint
  reduction (e.g., from 1.44s to 1.28s, and from 81 MiB to 59 MiB with
  GC_INITIAL_HEAP_SIZE=100000 on my system).

Modified:
   nix/trunk/src/libexpr/eval.cc
   nix/trunk/src/libexpr/eval.hh

Modified: nix/trunk/src/libexpr/eval.cc
==============================================================================
--- nix/trunk/src/libexpr/eval.cc       Sat Aug  6 19:14:13 2011        (r28203)
+++ nix/trunk/src/libexpr/eval.cc       Sat Aug  6 19:45:43 2011        (r28204)
@@ -431,17 +431,19 @@
 
 void EvalState::evalFile(const Path & path, Value & v)
 {
-    startNest(nest, lvlTalkative, format("evaluating file `%1%'") % path);
-
-    Expr * e = parseExprFromFile(path);
-    
-    try {
-        /* !!! Maybe we should cache the evaluation result. */
-        eval(e, v);
-    } catch (Error & e) {
-        addErrorPrefix(e, "while evaluating the file `%1%':\n", path);
-        throw;
-    }
+    FileEvalCache::iterator i = fileEvalCache.find(path);
+    if (i == fileEvalCache.end()) {
+        startNest(nest, lvlTalkative, format("evaluating file `%1%'") % path);
+        Expr * e = parseExprFromFile(path);
+        try {
+            eval(e, v);
+        } catch (Error & e) {
+            addErrorPrefix(e, "while evaluating the file `%1%':\n", path);
+            throw;
+        }
+        fileEvalCache[path] = v;
+    } else
+        v = i->second;
 }
 
 

Modified: nix/trunk/src/libexpr/eval.hh
==============================================================================
--- nix/trunk/src/libexpr/eval.hh       Sat Aug  6 19:14:13 2011        (r28203)
+++ nix/trunk/src/libexpr/eval.hh       Sat Aug  6 19:45:43 2011        (r28204)
@@ -211,8 +211,17 @@
 
     bool allowUnsafeEquality;
 
+    /* A cache from path names to parse trees. */
     std::map<Path, Expr *> parseTrees;
 
+    /* A cache from path names to values. */
+#if HAVE_BOEHMGC
+    typedef std::map<Path, Value, std::less<Path>, 
gc_allocator<std::pair<const Path, Value> > > FileEvalCache;
+#else
+    typedef std::map<Path, Value> FileEvalCache;
+#endif
+    FileEvalCache fileEvalCache;
+
     typedef list<std::pair<string, Path> > SearchPath;
     SearchPath searchPath;
     SearchPath::iterator searchPathInsertionPoint;
_______________________________________________
nix-commits mailing list
[email protected]
http://mail.cs.uu.nl/mailman/listinfo/nix-commits

Reply via email to