Clear Lazy<T> executor after value has been computed#315013
Open
yavanosta wants to merge 1 commit into
Open
Conversation
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.
Contributor
There was a problem hiding this comment.
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-
Errorthrown values into anError.
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 { |
Contributor
Author
There was a problem hiding this comment.
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.