Skip to content

Clear Lazy<T> executor after value has been computed#315013

Open
yavanosta wants to merge 1 commit into
microsoft:mainfrom
yavanosta:Lazy-clear-closure-after-compute
Open

Clear Lazy<T> executor after value has been computed#315013
yavanosta wants to merge 1 commit into
microsoft:mainfrom
yavanosta:Lazy-clear-closure-after-compute

Conversation

@yavanosta
Copy link
Copy Markdown
Contributor

Executors can often keep a reference to large objects in the closure, preventing garbage collector from memory clean-up. We do not need executor after value has been computed once, so we can safely remove the reference to it.

It has not any drawbacks but can save memory for some Lazy's.

Executors can often keep a reference to large objects in the closure, preventing garbage collector from memory clean-up. We do not need executor after value has been computed once, so we can safely remove the reference to it.
Copilot AI review requested due to automatic review settings May 7, 2026 14:01
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates the Lazy<T> utility in src/vs/base/common to drop the executor function reference after the lazy value is first evaluated, helping avoid retaining large closure-captured objects longer than necessary.

Changes:

  • Store the lazy executor in a private field and clear it on first evaluation to enable earlier GC of captured references.
  • Add an internal invariant check when evaluating the lazy for the first time.
  • Adjust error capture to coerce non-Error thrown values into an Error.

Comment on lines +45 to 48
this._value = executor();
} catch (err) {
this._error = err;
this._error = err instanceof Error ? err : new Error(String(err));
} finally {
Copy link
Copy Markdown
Contributor Author

@yavanosta yavanosta May 7, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the original thrown is already an Error it is thrown as-is. If it is non error I wrap the value to an error to store in _error field which is typed as Error.

But I can revert this line if its not correct.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants