diff options
author | Paul Phillips <paulp@improving.org> | 2011-07-14 01:27:04 +0000 |
---|---|---|
committer | Paul Phillips <paulp@improving.org> | 2011-07-14 01:27:04 +0000 |
commit | 733669230a470b60c2ecc92c666f0871cecf22ef (patch) | |
tree | 70ee5f0e49e348e317bfd9db5d2230b433dada31 /src/compiler/scala/tools/nsc/typechecker | |
parent | 5e49b4181976f20d28625008a775223dbf8e7f6e (diff) | |
download | scala-733669230a470b60c2ecc92c666f0871cecf22ef.tar.gz scala-733669230a470b60c2ecc92c666f0871cecf22ef.tar.bz2 scala-733669230a470b60c2ecc92c666f0871cecf22ef.zip |
Adding some Sets/Maps to perRunCaches, and elim...
Adding some Sets/Maps to perRunCaches, and eliminating ambiguously named
imports.
Did a tour of the compiler adding a few longer-lived mutable structures
to the per-run cache clearing mechanism. Some of these were not a big
threat, but there is (almost) literally no cost to tracking them and the
fewer mutable structures which are created "lone wolf style" the easier
it is to spot the one playing by his own rules.
While I was at it I followed through on long held ambition to eliminate
the importing of highly ambiguous names like "Map" and "HashSet" from
the mutable and immutable packages. I didn't quite manage elimination
but it's pretty close. Something potentially as pernicious which I
didn't do much about is this import:
import scala.collection._
Imagine coming across that one on lines 407 and 474 of a 1271 file.
That's not cool. Some poor future programmer will be on line 1100 and
use "Map[A, B]" in some function and only after the product has shipped
will it be discovered that the signature is wrong and the rocket will
now be crashing into the mountainside straightaway. No review.
Diffstat (limited to 'src/compiler/scala/tools/nsc/typechecker')
5 files changed, 21 insertions, 24 deletions
diff --git a/src/compiler/scala/tools/nsc/typechecker/Implicits.scala b/src/compiler/scala/tools/nsc/typechecker/Implicits.scala index 29ca9f4a70..c0e78d716c 100644 --- a/src/compiler/scala/tools/nsc/typechecker/Implicits.scala +++ b/src/compiler/scala/tools/nsc/typechecker/Implicits.scala @@ -13,7 +13,7 @@ package typechecker import annotation.tailrec import scala.collection.{ mutable, immutable } -import mutable.{ HashMap, LinkedHashMap, ListBuffer } +import mutable.{ LinkedHashMap, ListBuffer } import scala.util.matching.Regex import symtab.Flags._ import util.Statistics._ @@ -83,7 +83,7 @@ trait Implicits { private type InfoMap = LinkedHashMap[Symbol, List[ImplicitInfo]] // A map from class symbols to their associated implicits private val implicitsCache = new LinkedHashMap[Type, Infoss] private val infoMapCache = new LinkedHashMap[Symbol, InfoMap] - private val improvesCache = new HashMap[(ImplicitInfo, ImplicitInfo), Boolean] + private val improvesCache = perRunCaches.newMap[(ImplicitInfo, ImplicitInfo), Boolean]() def resetImplicits() { implicitsCache.clear() @@ -175,7 +175,7 @@ trait Implicits { /** An extractor for types of the form ? { name: ? } */ object HasMember { - private val hasMemberCache = new mutable.HashMap[Name, Type] + private val hasMemberCache = perRunCaches.newMap[Name, Type]() def apply(name: Name): Type = hasMemberCache.getOrElseUpdate(name, memberWildcardType(name, WildcardType)) def unapply(pt: Type): Option[Name] = pt match { case RefinedType(List(WildcardType), decls) => diff --git a/src/compiler/scala/tools/nsc/typechecker/Namers.scala b/src/compiler/scala/tools/nsc/typechecker/Namers.scala index 1ea41ff928..713c5f607a 100644 --- a/src/compiler/scala/tools/nsc/typechecker/Namers.scala +++ b/src/compiler/scala/tools/nsc/typechecker/Namers.scala @@ -6,9 +6,8 @@ package scala.tools.nsc package typechecker -import scala.collection.mutable.{HashMap, WeakHashMap} +import scala.collection.mutable import scala.ref.WeakReference -import symtab.Flags import symtab.Flags._ import scala.tools.nsc.io.AbstractFile @@ -52,7 +51,7 @@ trait Namers { self: Analyzer => // is stored in this map. The map is cleared lazily, i.e. when the new symbol // is created with the same name, the old one (if present) is wiped out, or the // entry is deleted when it is used and no longer needed. - private val caseClassOfModuleClass = new WeakHashMap[Symbol, WeakReference[ClassDef]] + private val caseClassOfModuleClass = perRunCaches.newWeakMap[Symbol, WeakReference[ClassDef]]() // Default getters of constructors are added to the companion object in the // typeCompleter of the constructor (methodSig). To compute the signature, @@ -60,7 +59,7 @@ trait Namers { self: Analyzer => // object, we need the templateNamer of that module class. // This map is extended during naming of classes, the Namer is added in when // it's available, i.e. in the type completer (templateSig) of the module class. - private[typechecker] val classAndNamerOfModule = new HashMap[Symbol, (ClassDef, Namer)] + private[typechecker] val classAndNamerOfModule = perRunCaches.newMap[Symbol, (ClassDef, Namer)]() def resetNamer() { classAndNamerOfModule.clear @@ -548,7 +547,7 @@ trait Namers { self: Analyzer => // --- Lazy Type Assignment -------------------------------------------------- def typeCompleter(tree: Tree) = mkTypeCompleter(tree) { sym => - if (settings.debug.value) log("defining " + sym + Flags.flagsToString(sym.flags)+sym.locationString) + if (settings.debug.value) log("defining " + sym + flagsToString(sym.flags)+sym.locationString) val tp = typeSig(tree) tp match { case TypeBounds(lo, hi) => @@ -1323,10 +1322,10 @@ trait Namers { self: Analyzer => if (sym.hasFlag(flag1) && sym.hasFlag(flag2)) context.error(sym.pos, if (flag1 == DEFERRED) - "abstract member may not have " + Flags.flagsToString(flag2) + " modifier"; + "abstract member may not have " + flagsToString(flag2) + " modifier"; else "illegal combination of modifiers: " + - Flags.flagsToString(flag1) + " and " + Flags.flagsToString(flag2) + + flagsToString(flag1) + " and " + flagsToString(flag2) + " for: " + sym); } diff --git a/src/compiler/scala/tools/nsc/typechecker/NamesDefaults.scala b/src/compiler/scala/tools/nsc/typechecker/NamesDefaults.scala index 18840de28f..a5c5aa5320 100644 --- a/src/compiler/scala/tools/nsc/typechecker/NamesDefaults.scala +++ b/src/compiler/scala/tools/nsc/typechecker/NamesDefaults.scala @@ -7,9 +7,7 @@ package scala.tools.nsc package typechecker import symtab.Flags._ - -import scala.collection.mutable.{ListBuffer, WeakHashMap} -import scala.collection.immutable.Set +import scala.collection.mutable /** * @author Lukas Rytz @@ -20,9 +18,8 @@ trait NamesDefaults { self: Analyzer => import global._ import definitions._ - val defaultParametersOfMethod = new WeakHashMap[Symbol, Set[Symbol]] { - override def default(key: Symbol) = Set() - } + val defaultParametersOfMethod = + perRunCaches.newWeakMap[Symbol, Set[Symbol]]() withDefaultValue Set() case class NamedApplyInfo(qual: Option[Tree], targs: List[Tree], vargss: List[List[Tree]], blockTyper: Typer) @@ -45,7 +42,7 @@ trait NamesDefaults { self: Analyzer => /** @param pos maps indicies from new to old (!) */ def reorderArgsInv[T: ClassManifest](args: List[T], pos: Int => Int): List[T] = { val argsArray = args.toArray - val res = new ListBuffer[T] + val res = new mutable.ListBuffer[T] for (i <- 0 until argsArray.length) res += argsArray(pos(i)) res.toList diff --git a/src/compiler/scala/tools/nsc/typechecker/RefChecks.scala b/src/compiler/scala/tools/nsc/typechecker/RefChecks.scala index 9ca70bbefb..8a05045514 100644 --- a/src/compiler/scala/tools/nsc/typechecker/RefChecks.scala +++ b/src/compiler/scala/tools/nsc/typechecker/RefChecks.scala @@ -862,7 +862,7 @@ abstract class RefChecks extends InfoTransform { } private var currentLevel: LevelInfo = null - private val symIndex = new mutable.HashMap[Symbol, Int] + private val symIndex = perRunCaches.newMap[Symbol, Int]() private def pushLevel() { currentLevel = new LevelInfo(currentLevel) diff --git a/src/compiler/scala/tools/nsc/typechecker/TreeCheckers.scala b/src/compiler/scala/tools/nsc/typechecker/TreeCheckers.scala index a8c3a13646..c8e2b6fca4 100644 --- a/src/compiler/scala/tools/nsc/typechecker/TreeCheckers.scala +++ b/src/compiler/scala/tools/nsc/typechecker/TreeCheckers.scala @@ -8,7 +8,7 @@ package typechecker import scala.tools.nsc.symtab.Flags._ import scala.collection.mutable -import mutable.{ HashMap, HashSet, ListBuffer } +import mutable.ListBuffer import util.returning abstract class TreeCheckers extends Analyzer { @@ -33,12 +33,13 @@ abstract class TreeCheckers extends Analyzer { /** This is a work in progress, don't take it too seriously. */ object SymbolTracker extends Traverser { - type PhaseMap = HashMap[Symbol, List[Tree]] - val maps: ListBuffer[(Phase, PhaseMap)] = ListBuffer() + type PhaseMap = mutable.HashMap[Symbol, List[Tree]] + + val maps = ListBuffer[(Phase, PhaseMap)]() def prev = maps.init.last._2 def latest = maps.last._2 - val defSyms = new HashMap[Symbol, List[DefTree]] - val newSyms = new HashSet[Symbol] + val defSyms = mutable.HashMap[Symbol, List[DefTree]]() + val newSyms = mutable.HashSet[Symbol]() val movedMsgs = new ListBuffer[String] def sortedNewSyms = newSyms.toList.distinct sortBy (_.name.toString) @@ -110,7 +111,7 @@ abstract class TreeCheckers extends Analyzer { } } - lazy val tpeOfTree = new HashMap[Tree, Type] + lazy val tpeOfTree = mutable.HashMap[Tree, Type]() def posstr(p: Position) = try p.source.path + ":" + p.line |