Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

disjoint closure environments #3

Open
toshok opened this issue Sep 11, 2013 · 0 comments
Open

disjoint closure environments #3

toshok opened this issue Sep 11, 2013 · 0 comments

Comments

@toshok
Copy link
Owner

toshok commented Sep 11, 2013

the following function:

function foo() {
  var a = [1,2,3,4];
  var b = 1;
  var c = 2;

  var new_a = a.map (function (el) { return el * b; }

  return function () {
    return c;
  }
}

will create a single closure environment holding both b (closed over by the function passed to map) and c (closed over by the returned function.)

In this totally disjoint case, ejs could split the closure environments, but I need to investigate if the overhead of splitting the closure makes sense for small environments (I'm guessing no for this particular case, particularly since the closed over variables are never set to anything other than primitives..)

Also, what happens if the sets are not totally disjoint? Take the following:

function foo() {
  var a = [1,2,3,4];
  var b = 1;
  var c = 2;
  var d = 4;

  var new_a = a.map (function (el) { return el * b + d; }

  return function () {
    return c + d;
  }
}

so now both functions close over d as well.

we could make 3 environments in this case;

env_0 = { d: 4 }
env_1 = { env: env_0, b: 1 }
env_2 = { env: env_0, c: 2 }

accesses to d get replaced with something different from within each function:

  1. env_1.env.d in the function passed to map
  2. env_2.env.d in the returned function.

This also has a cost in the increased indirection to get to the d binding, so need some heuristic based on the number of accesses to d to determine if we should just create 1 environment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant