Skip to content

Commit fcb0c01

Browse files
oderskyAdriaan Moors
authored and
Adriaan Moors
committed
Attempt #9 to opimize findMember.
Also avoid recomputation of memberType in findMembers
1 parent 71d2ceb commit fcb0c01

File tree

1 file changed

+43
-24
lines changed

1 file changed

+43
-24
lines changed

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

Lines changed: 43 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import Flags._
1414
import scala.util.control.ControlThrowable
1515
import scala.annotation.tailrec
1616
import util.Statistics
17+
import scala.runtime.ObjectRef
1718

1819
/* A standard type pattern match:
1920
case ErrorType =>
@@ -1031,9 +1032,21 @@ trait Types extends api.Types { self: SymbolTable =>
10311032

10321033
//Console.println("find member " + name.decode + " in " + this + ":" + this.baseClasses)//DEBUG
10331034
var members: Scope = null
1035+
var membertpes: mutable.Map[Symbol, Type] = null
1036+
var required = requiredFlags
10341037
var excluded = excludedFlags | DEFERRED
10351038
var continue = true
10361039
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+
10371050
while (continue) {
10381051
continue = false
10391052
val bcs0 = baseClasses
@@ -1044,29 +1057,31 @@ trait Types extends api.Types { self: SymbolTable =>
10441057
while (entry ne null) {
10451058
val sym = entry.sym
10461059
val flags = sym.flags
1047-
if ((flags & requiredFlags) == requiredFlags) {
1060+
if ((flags & required) == required) {
10481061
val excl = flags & excluded
10491062
if (excl == 0L &&
10501063
(// omit PRIVATE LOCALS unless selector class is contained in class owning the def.
10511064
(bcs eq bcs0) ||
10521065
(flags & PrivateLocal) != PrivateLocal ||
10531066
(bcs0.head.hasTransOwner(bcs.head)))) {
1054-
if (members eq null) members = newScope
1055-
var prevEntry = members.lookupEntry(sym.name)
1056-
var symtpe: Type = null
1057-
while ((prevEntry ne null) &&
1058-
!((prevEntry.sym eq sym) ||
1059-
(prevEntry.sym.owner ne sym.owner) &&
1060-
!sym.hasFlag(PRIVATE) && {
1061-
if (self eq null) self = this.narrow
1062-
if (symtpe eq null) symtpe = self.memberType(sym)
1063-
self.memberType(prevEntry.sym) matches symtpe
1064-
})) {
1065-
prevEntry = members lookupNextEntry prevEntry
1067+
if (members eq null) {
1068+
members = newScope
1069+
membertpes = new mutable.HashMap
10661070
}
1067-
if (prevEntry eq null) {
1068-
members enter sym
1071+
var others: ScopeEntry = members.lookupEntry(sym.name)
1072+
var symtpe: Type = null
1073+
while ((others ne null) && {
1074+
val other = others.sym
1075+
(other ne sym) &&
1076+
((other.owner eq sym.owner) ||
1077+
(flags & PRIVATE) != 0 || {
1078+
if (self eq null) self = this.narrow
1079+
if (symtpe eq null) symtpe = self.memberType(sym)
1080+
!(getMtpe(membertpes, other) matches symtpe)
1081+
})}) {
1082+
others = members lookupNextEntry others
10691083
}
1084+
if (others eq null) members enter sym
10701085
} else if (excl == DEFERRED) {
10711086
continue = true
10721087
}
@@ -1076,6 +1091,7 @@ trait Types extends api.Types { self: SymbolTable =>
10761091
// excluded = excluded | LOCAL
10771092
bcs = bcs.tail
10781093
} // while (!bcs.isEmpty)
1094+
required |= DEFERRED
10791095
excluded = excludedFlags
10801096
} // while (continue)
10811097
Statistics.popTimer(typeOpsStack, start)
@@ -1116,20 +1132,23 @@ trait Types extends api.Types { self: SymbolTable =>
11161132
var self: Type = null
11171133
val fingerPrint: Long = name.fingerPrint
11181134

1119-
def getMtpe(sym: Symbol, idx: Int): Type = {
1135+
def getMtpe(idx: Int, sym: Symbol): Type = {
11201136
var result = membertpes(idx)
1121-
if (result eq null) { result = self memberType sym; membertpes(idx) = result }
1137+
if (result eq null) {
1138+
result = self memberType sym
1139+
membertpes(idx) = result
1140+
}
11221141
result
11231142
}
11241143

1125-
def addMtpe(xs: Array[Type], tpe: Type, idx: Int): Array[Type] =
1126-
if (idx < xs.length ) {
1144+
def addMtpe(xs: Array[Type], idx: Int, tpe: Type): Array[Type] =
1145+
if (idx < xs.length) {
11271146
xs(idx) = tpe
11281147
xs
11291148
} else {
11301149
val ys = new Array[Type](xs.length * 2)
11311150
Array.copy(xs, 0, ys, 0, xs.length)
1132-
addMtpe(ys, tpe, idx)
1151+
addMtpe(ys, idx, tpe)
11331152
}
11341153

11351154
while (continue) {
@@ -1173,7 +1192,7 @@ trait Types extends api.Types { self: SymbolTable =>
11731192
membertpes(1) = symtpe
11741193
}
11751194
} else {
1176-
var others = members
1195+
var others: List[Symbol] = members
11771196
var idx = 0
11781197
var symtpe: Type = null
11791198
while ((others ne null) && {
@@ -1183,7 +1202,7 @@ trait Types extends api.Types { self: SymbolTable =>
11831202
(flags & PRIVATE) != 0 || {
11841203
if (self eq null) self = this.narrow
11851204
if (symtpe eq null) symtpe = self.memberType(sym)
1186-
!(getMtpe(other, idx) matches symtpe)
1205+
!(getMtpe(idx, other) matches symtpe)
11871206
})}) {
11881207
others = others.tail
11891208
idx += 1
@@ -1192,7 +1211,7 @@ trait Types extends api.Types { self: SymbolTable =>
11921211
val lastM1 = new ::(sym, null)
11931212
lastM.tl = lastM1
11941213
lastM = lastM1
1195-
membertpes = addMtpe(membertpes, symtpe, idx)
1214+
membertpes = addMtpe(membertpes, idx, symtpe)
11961215
}
11971216
}
11981217
} else if (excl == DEFERRED) {
@@ -1205,8 +1224,8 @@ trait Types extends api.Types { self: SymbolTable =>
12051224
// excluded = excluded | LOCAL
12061225
bcs = if (name == nme.CONSTRUCTOR) Nil else bcs.tail
12071226
} // while (!bcs.isEmpty)
1208-
excluded = excludedFlags
12091227
required |= DEFERRED
1228+
excluded = excludedFlags
12101229
} // while (continue)
12111230
Statistics.popTimer(typeOpsStack, start)
12121231
if (suspension ne null) suspension foreach (_.suspended = false)

0 commit comments

Comments
 (0)