Skip to content

Commit

Permalink
Only perform footprint optimization when caching result
Browse files Browse the repository at this point in the history
  • Loading branch information
liufengyun committed Aug 23, 2024
1 parent cba9ed1 commit 13cd2a6
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions compiler/src/dotty/tools/dotc/transform/init/Objects.scala
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,8 @@ class Objects(using Context @constructorOnly):
tree match
case ident: Ident =>
val sym = ident.symbol
if sym.isTerm && sym.isLocal then refs += sym
if sym.isTerm && sym.isLocal && !sym.is(Flags.Method)
then refs += sym

case vdef: ValDef =>
val sym = vdef.symbol
Expand Down Expand Up @@ -693,17 +694,20 @@ class Objects(using Context @constructorOnly):

def cachedEval(thisV: ThisValue, expr: Tree, cacheResult: Boolean)(fun: Tree => Value)(using Heap.MutableData, Env.Data, State.Data): Value =
val env = summon[Env.Data]
val footprint = Heap.footprint(Heap.getHeapData(), thisV, env, State.currentObjectRef)
val config = Config(thisV, env, footprint)
val heapBefore = Heap.getHeapData()
val changeSetBefore = Heap.getChangeSet()
// Only perform footprint optimization when cacheResult is true
val footprint =
if cacheResult then Heap.footprint(Heap.getHeapData(), thisV, env, State.currentObjectRef)
else heapBefore
val config = Config(thisV, env, footprint)

Heap.update(footprint, changeSet = Set.empty)
val result = super.cachedEval(config, expr, cacheResult, default = Res(Bottom, footprint, Set.empty)) { expr =>
val value = fun(expr)
val heapAfter = Heap.getHeapData()
val changeSetNew = Heap.getChangeSet()
// Perform garbage collection
// Only perform garbage collection when cacheResult is true
val heapGC =
if cacheResult then Heap.gc(value, footprint, heapAfter, changeSetNew)
else heapAfter
Expand Down

0 comments on commit 13cd2a6

Please sign in to comment.