Skip to content

Commit

Permalink
Variation scala#10 to optimze findMember
Browse files Browse the repository at this point in the history
Undoing the memberType caching. This seems to make it slower rather than faster. Also, comparing new vs old statistics shows that only very few asSeenFrom operations are saved by the caching.
  • Loading branch information
odersky authored and adriaanm committed Jul 14, 2012
1 parent fcb0c01 commit d905558
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 49 deletions.
48 changes: 48 additions & 0 deletions src/compiler/scala/tools/nsc/MainBench.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/* NSC -- new Scala compiler
* Copyright 2005-2011 LAMP/EPFL
* @author Martin Odersky
*/

package scala.tools.nsc

import java.io.File
import File.pathSeparator

import scala.tools.nsc.interactive.{ RefinedBuildManager, SimpleBuildManager }
import scala.tools.nsc.io.AbstractFile
import scala.tools.nsc.reporters.{Reporter, ConsoleReporter}
import scala.reflect.internal.util.{ BatchSourceFile, FakePos } //{Position}
import Properties.{ versionString, copyrightString, residentPromptString, msilLibPath }
import scala.reflect.internal.util.Statistics

/** The main class for NSC, a compiler for the programming
* language Scala.
*/
object MainBench extends Driver with EvalLoop {

lazy val theCompiler = Global(settings, reporter)

override def newCompiler() = theCompiler

val NIter = 50
val NBest = 10

override def main(args: Array[String]) = {
val times = new Array[Long](NIter)
var start = System.nanoTime()
for (i <- 0 until NIter) {
if (i == NIter-1) {
theCompiler.settings.Ystatistics.value = true
Statistics.enabled = true
}
process(args)
val end = System.nanoTime()
val duration = (end-start)/1000000
println(s"${duration}ms")
times(i) = duration
start = end
}
val avg = times.sorted.take(NBest).sum / NBest
println(s"avg shortest $NBest times ${avg}ms")
}
}
2 changes: 1 addition & 1 deletion src/reflect/scala/reflect/internal/StdNames.scala
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ trait StdNames {
val WHILE_PREFIX = "while$"

// Compiler internal names
val ANYname: NameType = "<any>"
val ANYname: NameType = "<anyname>"
val CONSTRUCTOR: NameType = "<init>"
val EQEQ_LOCAL_VAR: NameType = "eqEqTemp$"
val FAKE_LOCAL_THIS: NameType = "this$"
Expand Down
54 changes: 6 additions & 48 deletions src/reflect/scala/reflect/internal/Types.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1032,21 +1032,10 @@ trait Types extends api.Types { self: SymbolTable =>

//Console.println("find member " + name.decode + " in " + this + ":" + this.baseClasses)//DEBUG
var members: Scope = null
var membertpes: mutable.Map[Symbol, Type] = null
var required = requiredFlags
var excluded = excludedFlags | DEFERRED
var continue = true
var self: Type = null

def getMtpe(cache: mutable.Map[Symbol, Type], sym: Symbol): Type = cache get sym match {
case Some(tpe) if tpe ne null =>
tpe
case _ =>
val result = self memberType sym
cache(sym) = result
result
}

while (continue) {
continue = false
val bcs0 = baseClasses
Expand All @@ -1064,10 +1053,7 @@ trait Types extends api.Types { self: SymbolTable =>
(bcs eq bcs0) ||
(flags & PrivateLocal) != PrivateLocal ||
(bcs0.head.hasTransOwner(bcs.head)))) {
if (members eq null) {
members = newScope
membertpes = new mutable.HashMap
}
if (members eq null) members = newScope
var others: ScopeEntry = members.lookupEntry(sym.name)
var symtpe: Type = null
while ((others ne null) && {
Expand All @@ -1077,7 +1063,7 @@ trait Types extends api.Types { self: SymbolTable =>
(flags & PRIVATE) != 0 || {
if (self eq null) self = this.narrow
if (symtpe eq null) symtpe = self.memberType(sym)
!(getMtpe(membertpes, other) matches symtpe)
!(self.memberType(other) matches symtpe)
})}) {
others = members lookupNextEntry others
}
Expand All @@ -1092,7 +1078,7 @@ trait Types extends api.Types { self: SymbolTable =>
bcs = bcs.tail
} // while (!bcs.isEmpty)
required |= DEFERRED
excluded = excludedFlags
excluded &= ~(DEFERRED.toLong)
} // while (continue)
Statistics.popTimer(typeOpsStack, start)
if (suspension ne null) suspension foreach (_.suspended = false)
Expand Down Expand Up @@ -1125,32 +1111,12 @@ trait Types extends api.Types { self: SymbolTable =>
var members: List[Symbol] = null
var lastM: ::[Symbol] = null
var membertpe: Type = null
var membertpes: Array[Type] = null
var required = requiredFlags
var excluded = excludedFlags | DEFERRED
var continue = true
var self: Type = null
val fingerPrint: Long = name.fingerPrint

def getMtpe(idx: Int, sym: Symbol): Type = {
var result = membertpes(idx)
if (result eq null) {
result = self memberType sym
membertpes(idx) = result
}
result
}

def addMtpe(xs: Array[Type], idx: Int, tpe: Type): Array[Type] =
if (idx < xs.length) {
xs(idx) = tpe
xs
} else {
val ys = new Array[Type](xs.length * 2)
Array.copy(xs, 0, ys, 0, xs.length)
addMtpe(ys, idx, tpe)
}

while (continue) {
continue = false
val bcs0 = baseClasses
Expand All @@ -1176,24 +1142,18 @@ trait Types extends api.Types { self: SymbolTable =>
} else if (member eq NoSymbol) {
member = sym
} else if (members eq null) {
var symtpe: Type = null
if ((member ne sym) &&
((member.owner eq sym.owner) ||
(flags & PRIVATE) != 0 || {
if (self eq null) self = this.narrow
if (membertpe eq null) membertpe = self.memberType(member)
symtpe = self.memberType(sym)
!(membertpe matches symtpe)
!(membertpe matches self.memberType(sym))
})) {
lastM = new ::(sym, null)
members = member :: lastM
membertpes = new Array[Type](8)
membertpes(0) = membertpe
membertpes(1) = symtpe
}
} else {
var others: List[Symbol] = members
var idx = 0
var symtpe: Type = null
while ((others ne null) && {
val other = others.head
Expand All @@ -1202,16 +1162,14 @@ trait Types extends api.Types { self: SymbolTable =>
(flags & PRIVATE) != 0 || {
if (self eq null) self = this.narrow
if (symtpe eq null) symtpe = self.memberType(sym)
!(getMtpe(idx, other) matches symtpe)
!(self.memberType(other) matches symtpe)
})}) {
others = others.tail
idx += 1
}
if (others eq null) {
val lastM1 = new ::(sym, null)
lastM.tl = lastM1
lastM = lastM1
membertpes = addMtpe(membertpes, idx, symtpe)
}
}
} else if (excl == DEFERRED) {
Expand All @@ -1225,7 +1183,7 @@ trait Types extends api.Types { self: SymbolTable =>
bcs = if (name == nme.CONSTRUCTOR) Nil else bcs.tail
} // while (!bcs.isEmpty)
required |= DEFERRED
excluded = excludedFlags
excluded &= ~(DEFERRED.toLong)
} // while (continue)
Statistics.popTimer(typeOpsStack, start)
if (suspension ne null) suspension foreach (_.suspended = false)
Expand Down

0 comments on commit d905558

Please sign in to comment.