Closes #23681: Unnecessary reference duplicate in backend #23685
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.
from #23681:
Compiler version
Scala 3.8.0-RC1-bin-20250731-fe6b7eb-NIGHTLY, JVM (21)
Minimized code
Output
The memory grabbed by the
large
object is not freed, even though there is no variable holding it.Root cause
I suspect this is because of the current backend behaviour when generating the byte code for an == operation (in BCodeBodyBuilder.scala:genEqEqPrimitive):
It currently creates a copy of the right-hand side of the "==", so that it can be used later without reevaluating it. However, this copy can create a new GC root, and is a potential memory leak, invisible to the user.
Suggested fix
I suggest to replace this local variable copy by some stack operations, which might also be slightly faster.
current backend code:
I suggest replacing with the following simple stack operations, with the resulting operand stack in comment:
The test DottyBytecodeTests:patmatControlFlow is modified to match the new bytecode.