Skip to content

Commit 16ed9f4

Browse files
committed
Merge pull request scala#3494 from retronym/topic/opt-any-ref-map
Optimization: use AnyRef map for Namer -> Typer tree handoff
2 parents 6c73837 + 7957f63 commit 16ed9f4

File tree

7 files changed

+16
-7
lines changed

7 files changed

+16
-7
lines changed

src/compiler/scala/tools/nsc/CompilationUnits.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,11 @@ trait CompilationUnits { global: Global =>
9898
override def toString = map.toString
9999
}
100100

101+
// namer calls typer.computeType(rhs) on DefDef / ValDef when tpt is empty. the result
102+
// is cached here and re-used in typedDefDef / typedValDef
103+
// Also used to cache imports type-checked by namer.
104+
val transformed = new mutable.AnyRefMap[Tree, Tree]
105+
101106
/** things to check at end of compilation unit */
102107
val toCheck = new ListBuffer[() => Unit]
103108

src/compiler/scala/tools/nsc/backend/jvm/GenASM.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,10 +171,10 @@ abstract class GenASM extends SubComponent with BytecodeWriters with GenJVMASM {
171171

172172
var pickledBytes = 0 // statistics
173173

174-
val javaNameCache = perRunCaches.newMap[Symbol, Name]()
174+
val javaNameCache = perRunCaches.newAnyRefMap[Symbol, Name]()
175175

176176
// unlike javaNameCache, reverseJavaName contains entries only for class symbols and their internal names.
177-
val reverseJavaName = perRunCaches.newMap[String, Symbol]()
177+
val reverseJavaName = perRunCaches.newAnyRefMap[String, Symbol]()
178178

179179
private def mkFlags(args: Int*) = args.foldLeft(0)(_ | _)
180180
private def hasPublicBitSet(flags: Int) = (flags & asm.Opcodes.ACC_PUBLIC) != 0

src/compiler/scala/tools/nsc/typechecker/MethodSynthesis.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,7 @@ trait MethodSynthesis {
427427
override def derivedSym = basisSym.lazyAccessor
428428
override def derivedTree: DefDef = {
429429
val ValDef(_, _, tpt0, rhs0) = tree
430-
val rhs1 = transformed.getOrElse(rhs0, rhs0)
430+
val rhs1 = context.unit.transformed.getOrElse(rhs0, rhs0)
431431
val body = (
432432
if (tree.symbol.owner.isTrait || hasUnitType(basisSym)) rhs1
433433
else gen.mkAssignAndReturn(basisSym, rhs1)

src/compiler/scala/tools/nsc/typechecker/Namers.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1419,7 +1419,7 @@ trait Namers extends MethodSynthesis {
14191419

14201420
val newImport = treeCopy.Import(imp, expr1, selectors).asInstanceOf[Import]
14211421
checkSelectors(newImport)
1422-
transformed(imp) = newImport
1422+
context.unit.transformed(imp) = newImport
14231423
// copy symbol and type attributes back into old expression
14241424
// so that the structure builder will find it.
14251425
expr setSymbol expr1.symbol setType expr1.tpe

src/compiler/scala/tools/nsc/typechecker/Typers.scala

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ package scala
1313
package tools.nsc
1414
package typechecker
1515

16-
import scala.collection.{ mutable, immutable }
16+
import scala.collection.{mutable, immutable}
1717
import scala.reflect.internal.util.{ BatchSourceFile, Statistics, shortClassOfInstance }
1818
import mutable.ListBuffer
1919
import symtab.Flags._
@@ -39,7 +39,7 @@ trait Typers extends Adaptations with Tags with TypersTracking with PatternTyper
3939
// namer calls typer.computeType(rhs) on DefDef / ValDef when tpt is empty. the result
4040
// is cached here and re-used in typedDefDef / typedValDef
4141
// Also used to cache imports type-checked by namer.
42-
val transformed = new mutable.HashMap[Tree, Tree]
42+
val transformed = new mutable.AnyRefMap[Tree, Tree]
4343

4444
final val shortenImports = false
4545

@@ -52,7 +52,6 @@ trait Typers extends Adaptations with Tags with TypersTracking with PatternTyper
5252
//println("resetTyper called")
5353
resetContexts()
5454
resetImplicits()
55-
transformed.clear()
5655
resetDocComments()
5756
}
5857

@@ -108,6 +107,8 @@ trait Typers extends Adaptations with Tags with TypersTracking with PatternTyper
108107
val runDefinitions = currentRun.runDefinitions
109108
import runDefinitions._
110109

110+
private val transformed: mutable.Map[Tree, Tree] = unit.transformed
111+
111112
val infer = new Inferencer(context0) {
112113
// See SI-3281 re undoLog
113114
override def isCoercible(tp: Type, pt: Type) = undoLog undo viewExists(tp, pt)

src/interactive/scala/tools/nsc/interactive/Global.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -638,6 +638,7 @@ class Global(settings: Settings, _reporter: Reporter, projectName: String = "")
638638
unit.problems.clear()
639639
unit.body = EmptyTree
640640
unit.status = NotLoaded
641+
unit.transformed.clear()
641642
}
642643

643644
/** Parse unit and create a name index, unless this has already been done before */

src/reflect/scala/reflect/internal/SymbolTable.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,8 @@ abstract class SymbolTable extends macros.Universe
371371
def newMap[K, V]() = recordCache(mutable.HashMap[K, V]())
372372
def newSet[K]() = recordCache(mutable.HashSet[K]())
373373
def newWeakSet[K <: AnyRef]() = recordCache(new WeakHashSet[K]())
374+
375+
def newAnyRefMap[K <: AnyRef, V]() = recordCache(mutable.AnyRefMap[K, V]())
374376
def newGeneric[T](f: => T): () => T = {
375377
val NoCached: T = null.asInstanceOf[T]
376378
var cached: T = NoCached

0 commit comments

Comments
 (0)