-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Fix #1692: Null out fields after use in lazy initialization #4289
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
708a2e0
Fix #1692: Null out fields after use in lazy initialization
allanrenucci f060565
Don't null out private field in trait
allanrenucci fefbe4c
Fix typos and indentation
allanrenucci 78f83c9
Address review comments
allanrenucci d800e31
Apply optimisation on private[this] fields only
allanrenucci File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
112 changes: 112 additions & 0 deletions
112
compiler/src/dotty/tools/dotc/transform/CollectNullableFields.scala
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
package dotty.tools.dotc.transform | ||
|
||
import dotty.tools.dotc.ast.tpd | ||
import dotty.tools.dotc.core.Contexts.Context | ||
import dotty.tools.dotc.core.Flags._ | ||
import dotty.tools.dotc.core.Symbols.Symbol | ||
import dotty.tools.dotc.core.Types.{Type, ExprType} | ||
import dotty.tools.dotc.transform.MegaPhase.MiniPhase | ||
import dotty.tools.dotc.transform.SymUtils._ | ||
|
||
import scala.collection.JavaConverters._ | ||
import scala.collection.mutable | ||
|
||
import java.util.IdentityHashMap | ||
|
||
object CollectNullableFields { | ||
val name = "collectNullableFields" | ||
} | ||
|
||
/** Collect fields that can be nulled out after use in lazy initialization. | ||
* | ||
* This information is used during lazy val transformation to assign null to private | ||
* fields that are only used within a lazy val initializer. This is not just an optimization, | ||
* but is needed for correctness to prevent memory leaks. E.g. | ||
* | ||
* ```scala | ||
* class TestByNameLazy(byNameMsg: => String) { | ||
* lazy val byLazyValMsg = byNameMsg | ||
* } | ||
* ``` | ||
* | ||
* Here `byNameMsg` should be null out once `byLazyValMsg` is | ||
* initialised. | ||
* | ||
* A field is nullable if all the conditions below hold: | ||
* - belongs to a non trait-class | ||
* - is private[this] | ||
* - is not lazy | ||
* - its type is nullable | ||
* - is only used in a lazy val initializer | ||
* - defined in the same class as the lazy val | ||
*/ | ||
class CollectNullableFields extends MiniPhase { | ||
import tpd._ | ||
|
||
override def phaseName = CollectNullableFields.name | ||
|
||
/** Running after `ElimByName` to see by names as nullable types. */ | ||
override def runsAfter = Set(ElimByName.name) | ||
|
||
private[this] sealed trait FieldInfo | ||
private[this] case object NotNullable extends FieldInfo | ||
private[this] case class Nullable(by: Symbol) extends FieldInfo | ||
|
||
/** Whether or not a field is nullable */ | ||
private[this] var nullability: IdentityHashMap[Symbol, FieldInfo] = _ | ||
|
||
override def prepareForUnit(tree: Tree)(implicit ctx: Context) = { | ||
if (nullability == null) nullability = new IdentityHashMap | ||
ctx | ||
} | ||
|
||
private def recordUse(tree: Tree)(implicit ctx: Context): Tree = { | ||
val sym = tree.symbol | ||
val isNullablePrivateField = | ||
sym.isField && | ||
!sym.is(Lazy) && | ||
!sym.owner.is(Trait) && | ||
sym.initial.is(PrivateLocal) && | ||
sym.info.widenDealias.typeSymbol.isNullableClass | ||
|
||
if (isNullablePrivateField) | ||
nullability.get(sym) match { | ||
case Nullable(from) if from != ctx.owner => // used in multiple lazy val initializers | ||
nullability.put(sym, NotNullable) | ||
case null => // not in the map | ||
val from = ctx.owner | ||
val isNullable = | ||
from.is(Lazy, butNot = Module) && // is lazy val | ||
from.owner.isClass && // is field | ||
from.owner.eq(sym.owner) // is lazy val and field defined in the same class | ||
val info = if (isNullable) Nullable(from) else NotNullable | ||
nullability.put(sym, info) | ||
case _ => | ||
// Do nothing for: | ||
// - NotNullable | ||
// - Nullable(ctx.owner) | ||
} | ||
|
||
tree | ||
} | ||
|
||
override def transformIdent(tree: Ident)(implicit ctx: Context) = | ||
recordUse(tree) | ||
|
||
override def transformSelect(tree: Select)(implicit ctx: Context) = | ||
recordUse(tree) | ||
|
||
/** Map lazy values to the fields they should null after initialization. */ | ||
def lazyValNullables(implicit ctx: Context): IdentityHashMap[Symbol, mutable.ListBuffer[Symbol]] = { | ||
val result = new IdentityHashMap[Symbol, mutable.ListBuffer[Symbol]] | ||
|
||
nullability.forEach { | ||
case (sym, Nullable(from)) => | ||
val bldr = result.computeIfAbsent(from, _ => new mutable.ListBuffer) | ||
bldr += sym | ||
case _ => | ||
} | ||
|
||
result | ||
} | ||
} |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can use MutableSymbolMap instead.