git » jacl.git » commit 1060c56

make env copying more efficient in the common case (new context)

author Alan Dipert
2019-10-11 14:13:35 UTC
committer Alan Dipert
2019-10-11 14:13:35 UTC
parent e593229717b9217575cbcbb21710aea0aef2fab2

make env copying more efficient in the common case (new context)

jacl.js +17 -10

diff --git a/jacl.js b/jacl.js
index f56a32e..85d7e66 100644
--- a/jacl.js
+++ b/jacl.js
@@ -817,22 +817,29 @@ const analyzeSymbol = (env, form) => {
 };
 
 class Env {
-  constructor() {
-    this.locals = {
-      fun: new Set(),
-      val: new Set()
-    };
-    this.context = "expr";
+  constructor(init = true) {
+    this.locals = {}
+    if (init) Env.init(this);
+    return this;
+  }
+  static init(env) {
+    env.locals.fun = new Set();
+    env.locals.val = new Set();
+    env.context = "expr";
+    return env;
   }
   clone() {
-    const newEnv = new Env();
+    const newEnv = new Env(false);
     newEnv.locals.fun = new Set(this.locals.fun);
     newEnv.locals.val = new Set(this.locals.val);
+    newEnv.context = this.context;
     return newEnv;
   }
-  withContext(ctx) {
-    const newEnv = this.clone();
-    newEnv.context = ctx;
+  withContext(context) {
+    const newEnv = new Env(false);
+    newEnv.locals.fun = this.locals.fun;
+    newEnv.locals.val = this.locals.val;
+    newEnv.context = context;
     return newEnv;
   }
 }