Allow early release of rvalues in assignments #8123
Closed
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.
The rvalues in assignments might be temporaries (like function return
values) that could be freed immediately after the assignment is done. But
currently, they live to the end of the enclosing scope. For managed
box, this currently means that there is an extra ref count that keeps
the value alive. This leads to a huge memory usage peak in the
expansion phase of compilation with rustc, because all intermediate
expansion results are kept alive till the end of phase 2, instead of
being freed immediately after their last "usable" ref is dropped.
As a stop-gap solution, we can add a scope around the assignment during
translation, which means that the temporary value is dropped right away.
It's only a stop-gap solution because once we switch from refcounting to
garbage collection, it won't help anymore and the code should switch to
either RC or ~ for Crate data, but for now this reduces the peak memory
usage when compiling librustc by about 100MB on my x86_64 Linux box.