Skip to content

Commit d905558

Browse files
oderskyAdriaan Moors
authored and
Adriaan Moors
committed
Variation #10 to optimze findMember
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.
1 parent fcb0c01 commit d905558

File tree

3 files changed

+55
-49
lines changed

3 files changed

+55
-49
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/* NSC -- new Scala compiler
2+
* Copyright 2005-2011 LAMP/EPFL
3+
* @author Martin Odersky
4+
*/
5+
6+
package scala.tools.nsc
7+
8+
import java.io.File
9+
import File.pathSeparator
10+
11+
import scala.tools.nsc.interactive.{ RefinedBuildManager, SimpleBuildManager }
12+
import scala.tools.nsc.io.AbstractFile
13+
import scala.tools.nsc.reporters.{Reporter, ConsoleReporter}
14+
import scala.reflect.internal.util.{ BatchSourceFile, FakePos } //{Position}
15+
import Properties.{ versionString, copyrightString, residentPromptString, msilLibPath }
16+
import scala.reflect.internal.util.Statistics
17+
18+
/** The main class for NSC, a compiler for the programming
19+
* language Scala.
20+
*/
21+
object MainBench extends Driver with EvalLoop {
22+
23+
lazy val theCompiler = Global(settings, reporter)
24+
25+
override def newCompiler() = theCompiler
26+
27+
val NIter = 50
28+
val NBest = 10
29+
30+
override def main(args: Array[String]) = {
31+
val times = new Array[Long](NIter)
32+
var start = System.nanoTime()
33+
for (i <- 0 until NIter) {
34+
if (i == NIter-1) {
35+
theCompiler.settings.Ystatistics.value = true
36+
Statistics.enabled = true
37+
}
38+
process(args)
39+
val end = System.nanoTime()
40+
val duration = (end-start)/1000000
41+
println(s"${duration}ms")
42+
times(i) = duration
43+
start = end
44+
}
45+
val avg = times.sorted.take(NBest).sum / NBest
46+
println(s"avg shortest $NBest times ${avg}ms")
47+
}
48+
}

src/reflect/scala/reflect/internal/StdNames.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ trait StdNames {
294294
val WHILE_PREFIX = "while$"
295295

296296
// Compiler internal names
297-
val ANYname: NameType = "<any>"
297+
val ANYname: NameType = "<anyname>"
298298
val CONSTRUCTOR: NameType = "<init>"
299299
val EQEQ_LOCAL_VAR: NameType = "eqEqTemp$"
300300
val FAKE_LOCAL_THIS: NameType = "this$"

src/reflect/scala/reflect/internal/Types.scala

Lines changed: 6 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1032,21 +1032,10 @@ trait Types extends api.Types { self: SymbolTable =>
10321032

10331033
//Console.println("find member " + name.decode + " in " + this + ":" + this.baseClasses)//DEBUG
10341034
var members: Scope = null
1035-
var membertpes: mutable.Map[Symbol, Type] = null
10361035
var required = requiredFlags
10371036
var excluded = excludedFlags | DEFERRED
10381037
var continue = true
10391038
var self: Type = null
1040-
1041-
def getMtpe(cache: mutable.Map[Symbol, Type], sym: Symbol): Type = cache get sym match {
1042-
case Some(tpe) if tpe ne null =>
1043-
tpe
1044-
case _ =>
1045-
val result = self memberType sym
1046-
cache(sym) = result
1047-
result
1048-
}
1049-
10501039
while (continue) {
10511040
continue = false
10521041
val bcs0 = baseClasses
@@ -1064,10 +1053,7 @@ trait Types extends api.Types { self: SymbolTable =>
10641053
(bcs eq bcs0) ||
10651054
(flags & PrivateLocal) != PrivateLocal ||
10661055
(bcs0.head.hasTransOwner(bcs.head)))) {
1067-
if (members eq null) {
1068-
members = newScope
1069-
membertpes = new mutable.HashMap
1070-
}
1056+
if (members eq null) members = newScope
10711057
var others: ScopeEntry = members.lookupEntry(sym.name)
10721058
var symtpe: Type = null
10731059
while ((others ne null) && {
@@ -1077,7 +1063,7 @@ trait Types extends api.Types { self: SymbolTable =>
10771063
(flags & PRIVATE) != 0 || {
10781064
if (self eq null) self = this.narrow
10791065
if (symtpe eq null) symtpe = self.memberType(sym)
1080-
!(getMtpe(membertpes, other) matches symtpe)
1066+
!(self.memberType(other) matches symtpe)
10811067
})}) {
10821068
others = members lookupNextEntry others
10831069
}
@@ -1092,7 +1078,7 @@ trait Types extends api.Types { self: SymbolTable =>
10921078
bcs = bcs.tail
10931079
} // while (!bcs.isEmpty)
10941080
required |= DEFERRED
1095-
excluded = excludedFlags
1081+
excluded &= ~(DEFERRED.toLong)
10961082
} // while (continue)
10971083
Statistics.popTimer(typeOpsStack, start)
10981084
if (suspension ne null) suspension foreach (_.suspended = false)
@@ -1125,32 +1111,12 @@ trait Types extends api.Types { self: SymbolTable =>
11251111
var members: List[Symbol] = null
11261112
var lastM: ::[Symbol] = null
11271113
var membertpe: Type = null
1128-
var membertpes: Array[Type] = null
11291114
var required = requiredFlags
11301115
var excluded = excludedFlags | DEFERRED
11311116
var continue = true
11321117
var self: Type = null
11331118
val fingerPrint: Long = name.fingerPrint
11341119

1135-
def getMtpe(idx: Int, sym: Symbol): Type = {
1136-
var result = membertpes(idx)
1137-
if (result eq null) {
1138-
result = self memberType sym
1139-
membertpes(idx) = result
1140-
}
1141-
result
1142-
}
1143-
1144-
def addMtpe(xs: Array[Type], idx: Int, tpe: Type): Array[Type] =
1145-
if (idx < xs.length) {
1146-
xs(idx) = tpe
1147-
xs
1148-
} else {
1149-
val ys = new Array[Type](xs.length * 2)
1150-
Array.copy(xs, 0, ys, 0, xs.length)
1151-
addMtpe(ys, idx, tpe)
1152-
}
1153-
11541120
while (continue) {
11551121
continue = false
11561122
val bcs0 = baseClasses
@@ -1176,24 +1142,18 @@ trait Types extends api.Types { self: SymbolTable =>
11761142
} else if (member eq NoSymbol) {
11771143
member = sym
11781144
} else if (members eq null) {
1179-
var symtpe: Type = null
11801145
if ((member ne sym) &&
11811146
((member.owner eq sym.owner) ||
11821147
(flags & PRIVATE) != 0 || {
11831148
if (self eq null) self = this.narrow
11841149
if (membertpe eq null) membertpe = self.memberType(member)
1185-
symtpe = self.memberType(sym)
1186-
!(membertpe matches symtpe)
1150+
!(membertpe matches self.memberType(sym))
11871151
})) {
11881152
lastM = new ::(sym, null)
11891153
members = member :: lastM
1190-
membertpes = new Array[Type](8)
1191-
membertpes(0) = membertpe
1192-
membertpes(1) = symtpe
11931154
}
11941155
} else {
11951156
var others: List[Symbol] = members
1196-
var idx = 0
11971157
var symtpe: Type = null
11981158
while ((others ne null) && {
11991159
val other = others.head
@@ -1202,16 +1162,14 @@ trait Types extends api.Types { self: SymbolTable =>
12021162
(flags & PRIVATE) != 0 || {
12031163
if (self eq null) self = this.narrow
12041164
if (symtpe eq null) symtpe = self.memberType(sym)
1205-
!(getMtpe(idx, other) matches symtpe)
1165+
!(self.memberType(other) matches symtpe)
12061166
})}) {
12071167
others = others.tail
1208-
idx += 1
12091168
}
12101169
if (others eq null) {
12111170
val lastM1 = new ::(sym, null)
12121171
lastM.tl = lastM1
12131172
lastM = lastM1
1214-
membertpes = addMtpe(membertpes, idx, symtpe)
12151173
}
12161174
}
12171175
} else if (excl == DEFERRED) {
@@ -1225,7 +1183,7 @@ trait Types extends api.Types { self: SymbolTable =>
12251183
bcs = if (name == nme.CONSTRUCTOR) Nil else bcs.tail
12261184
} // while (!bcs.isEmpty)
12271185
required |= DEFERRED
1228-
excluded = excludedFlags
1186+
excluded &= ~(DEFERRED.toLong)
12291187
} // while (continue)
12301188
Statistics.popTimer(typeOpsStack, start)
12311189
if (suspension ne null) suspension foreach (_.suspended = false)

0 commit comments

Comments
 (0)