Skip to content

Consider performance implications of unsetting individual variables after run #3

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

Open
shicks opened this issue Mar 25, 2025 · 0 comments

Comments

@shicks
Copy link

shicks commented Mar 25, 2025

Current AsyncContext spec proposal allows Variable.prototype.run to be implemented very efficiently by saving and restoring an immutable context mapping. Essentially:

Variable.prototype.run = function(newValue, func) {
  const snapshot = GLOBAL_MAPPING;
  try {
    GLOBAL_MAPPING = {...snapshot, [this.key]: newValue};
    return func();
  } finally {
    GLOBAL_MAPPING = snapshot;
  }
}

This will no longer work if we want v1.run not to clobber a value set by v2.withValue - implementations would instead need to do something more along the lines of

Variable.prototype.run = function(newValue, func) {
  const oldValue = GLOBAL_MAPPING[this.key];
  try {
    GLOBAL_MAPPING = {...GLOBAL_MAPPING, [this.key]: newValue};
    return func();
  } finally {
    GLOBAL_MAPPING = {...GLOBAL_MAPPING, [this.key]: oldValue}; // extra copy
  }
}

or else we could make GLOBAL_MAPPING mutable and make Snapshot construction much slower by needing to make defensive copies.

It may be possible to use a persistent data structure (e.g. a HAMT or some such) so that the copies/updates are O(log n) instead of O(n), but AIUI that's not currently the implementation plan.

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