Skip to content

Commit 2e58a66

Browse files
authored
Merge pull request #9638 from dotty-staging/optimze-conditions
Performance tweaks
2 parents 5863b61 + 1f62e6b commit 2e58a66

File tree

5 files changed

+79
-64
lines changed

5 files changed

+79
-64
lines changed

compiler/src/dotty/tools/dotc/config/Feature.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ object Feature:
2323
def toPrefix(sym: Symbol): String =
2424
if !sym.exists || sym == defn.LanguageModule.moduleClass then ""
2525
else toPrefix(sym.owner) + sym.name.stripModuleClassSuffix + "."
26-
val prefix = if owner.exists then toPrefix(owner) else ""
26+
val prefix = if owner ne NoSymbol then toPrefix(owner) else ""
2727
ctx.base.settings.language.value.contains(prefix + feature)
2828

2929
/** Is `feature` enabled by by an import? This is the case if the feature

compiler/src/dotty/tools/dotc/core/Definitions.scala

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1084,17 +1084,22 @@ class Definitions {
10841084
)
10851085
private val compiletimePackageBooleanTypes: Set[Name] = Set(tpnme.Not, tpnme.Xor, tpnme.And, tpnme.Or)
10861086
private val compiletimePackageStringTypes: Set[Name] = Set(tpnme.Plus)
1087-
1088-
final def isCompiletimeAppliedType(sym: Symbol)(using Context): Boolean = {
1089-
def isOpsPackageObjectAppliedType: Boolean =
1090-
sym.owner == CompiletimeOpsPackageObjectAny.moduleClass && compiletimePackageAnyTypes.contains(sym.name) ||
1091-
sym.owner == CompiletimeOpsPackageObjectInt.moduleClass && compiletimePackageIntTypes.contains(sym.name) ||
1092-
sym.owner == CompiletimeOpsPackageObjectBoolean.moduleClass && compiletimePackageBooleanTypes.contains(sym.name) ||
1093-
sym.owner == CompiletimeOpsPackageObjectString.moduleClass && compiletimePackageStringTypes.contains(sym.name)
1094-
1095-
sym.isType && (isCompiletime_S(sym) || isOpsPackageObjectAppliedType)
1096-
}
1097-
1087+
private val compiletimePackageOpTypes: Set[Name] =
1088+
Set(tpnme.S)
1089+
++ compiletimePackageAnyTypes
1090+
++ compiletimePackageIntTypes
1091+
++ compiletimePackageBooleanTypes
1092+
++ compiletimePackageStringTypes
1093+
1094+
final def isCompiletimeAppliedType(sym: Symbol)(using Context): Boolean =
1095+
compiletimePackageOpTypes.contains(sym.name)
1096+
&& (
1097+
sym.owner == CompiletimePackageObject.moduleClass && sym.name == tpnme.S
1098+
|| sym.owner == CompiletimeOpsPackageObjectAny.moduleClass && compiletimePackageAnyTypes.contains(sym.name)
1099+
|| sym.owner == CompiletimeOpsPackageObjectInt.moduleClass && compiletimePackageIntTypes.contains(sym.name)
1100+
|| sym.owner == CompiletimeOpsPackageObjectBoolean.moduleClass && compiletimePackageBooleanTypes.contains(sym.name)
1101+
|| sym.owner == CompiletimeOpsPackageObjectString.moduleClass && compiletimePackageStringTypes.contains(sym.name)
1102+
)
10981103

10991104
// ----- Symbol sets ---------------------------------------------------
11001105

@@ -1143,8 +1148,11 @@ class Definitions {
11431148
def PolyFunctionType = PolyFunctionClass.typeRef
11441149

11451150
/** If `cls` is a class in the scala package, its name, otherwise EmptyTypeName */
1146-
def scalaClassName(cls: Symbol)(using Context): TypeName =
1147-
if (cls.isClass && cls.owner == ScalaPackageClass) cls.asClass.name else EmptyTypeName
1151+
def scalaClassName(cls: Symbol)(using Context): TypeName = cls.denot match
1152+
case clsd: ClassDenotation if clsd.owner eq ScalaPackageClass =>
1153+
clsd.name.asInstanceOf[TypeName]
1154+
case _ =>
1155+
EmptyTypeName
11481156

11491157
/** If type `ref` refers to a class in the scala package, its name, otherwise EmptyTypeName */
11501158
def scalaClassName(ref: Type)(using Context): TypeName = scalaClassName(ref.classSymbol)

compiler/src/dotty/tools/dotc/core/NameOps.scala

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -252,15 +252,21 @@ object NameOps {
252252
}
253253

254254
def unmangle(kind: NameKind): N = likeSpacedN {
255-
name replace {
256-
case unmangled: SimpleName =>
257-
kind.unmangle(unmangled)
258-
case ExpandedName(prefix, last) =>
259-
kind.unmangle(last) replace {
260-
case kernel: SimpleName =>
261-
ExpandedName(prefix, kernel)
255+
name match
256+
case name: SimpleName =>
257+
kind.unmangle(name)
258+
case name: TypeName =>
259+
name.toTermName.unmangle(kind).toTypeName
260+
case _ =>
261+
name replace {
262+
case unmangled: SimpleName =>
263+
kind.unmangle(unmangled)
264+
case ExpandedName(prefix, last) =>
265+
kind.unmangle(last) replace {
266+
case kernel: SimpleName =>
267+
ExpandedName(prefix, kernel)
268+
}
262269
}
263-
}
264270
}
265271

266272
def unmangle(kinds: List[NameKind]): N = {

compiler/src/dotty/tools/dotc/core/TyperState.scala

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -115,16 +115,17 @@ class TyperState() {
115115
*/
116116
def commit()(using Context): Unit = {
117117
Stats.record("typerState.commit")
118+
assert(isCommittable)
118119
val targetState = ctx.typerState
119-
if (constraint ne targetState.constraint)
120+
if constraint ne targetState.constraint then
121+
Stats.record("typerState.commit.new constraint")
120122
constr.println(i"committing $this to $targetState, fromConstr = $constraint, toConstr = ${targetState.constraint}")
121-
assert(isCommittable)
122-
if (targetState.constraint eq previousConstraint) targetState.constraint = constraint
123-
else targetState.mergeConstraintWith(this)
124-
constraint foreachTypeVar { tvar =>
125-
if (tvar.owningState.get eq this) tvar.owningState = new WeakReference(targetState)
126-
}
127-
targetState.ownedVars ++= ownedVars
123+
if targetState.constraint eq previousConstraint then targetState.constraint = constraint
124+
else targetState.mergeConstraintWith(this)
125+
if !ownedVars.isEmpty then
126+
for tvar <- ownedVars do
127+
tvar.owningState = new WeakReference(targetState)
128+
targetState.ownedVars ++= ownedVars
128129
targetState.gc()
129130
reporter.flush()
130131
isCommitted = true
@@ -137,22 +138,19 @@ class TyperState() {
137138
* type variable instantiation cannot be retracted anymore. Then, remove
138139
* no-longer needed constraint entries.
139140
*/
140-
def gc()(using Context): Unit = {
141-
Stats.record("typerState.gc")
142-
val toCollect = new mutable.ListBuffer[TypeLambda]
143-
constraint foreachTypeVar { tvar =>
144-
if (!tvar.inst.exists) {
145-
val inst = constraint.instType(tvar)
146-
if (inst.exists && (tvar.owningState.get eq this)) {
147-
tvar.inst = inst
148-
val lam = tvar.origin.binder
149-
if (constraint.isRemovable(lam)) toCollect += lam
150-
}
151-
}
152-
}
153-
for (poly <- toCollect)
154-
constraint = constraint.remove(poly)
155-
}
141+
def gc()(using Context): Unit =
142+
if !ownedVars.isEmpty then
143+
Stats.record("typerState.gc")
144+
val toCollect = new mutable.ListBuffer[TypeLambda]
145+
for tvar <- ownedVars do
146+
if !tvar.inst.exists then
147+
val inst = constraint.instType(tvar)
148+
if inst.exists then
149+
tvar.inst = inst
150+
val lam = tvar.origin.binder
151+
if constraint.isRemovable(lam) then toCollect += lam
152+
for poly <- toCollect do
153+
constraint = constraint.remove(poly)
156154

157155
override def toString: String = {
158156
def ids(state: TyperState): List[String] =

compiler/src/dotty/tools/dotc/typer/Typer.scala

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -191,23 +191,26 @@ class Typer extends Namer
191191
if found eq previous then checkNewOrShadowed(found, prevPrec)
192192
else found
193193

194-
def selection(imp: ImportInfo, name: Name, checkBounds: Boolean) =
195-
if imp.sym.isCompleting then
196-
report.warning(i"cyclic ${imp.sym}, ignored", pos)
197-
NoType
198-
else if unimported.nonEmpty && unimported.contains(imp.site.termSymbol) then
199-
NoType
200-
else
201-
val pre = imp.site
202-
var denot = pre.memberBasedOnFlags(name, required, EmptyFlags)
203-
.accessibleFrom(pre)(using refctx)
194+
def selection(imp: ImportInfo, name: Name, checkBounds: Boolean): Type =
195+
imp.sym.info match
196+
case ImportType(expr) =>
197+
val pre = expr.tpe
198+
var denot = pre.memberBasedOnFlags(name, required, EmptyFlags)
199+
.accessibleFrom(pre)(using refctx)
204200
// Pass refctx so that any errors are reported in the context of the
205201
// reference instead of the context of the import scope
206-
if checkBounds && denot.exists then
207-
denot = denot.filterWithPredicate { mbr =>
208-
mbr.matchesImportBound(if mbr.symbol.is(Given) then imp.givenBound else imp.wildcardBound)
209-
}
210-
if reallyExists(denot) then pre.select(name, denot) else NoType
202+
if denot.exists then
203+
if checkBounds then
204+
denot = denot.filterWithPredicate { mbr =>
205+
mbr.matchesImportBound(if mbr.symbol.is(Given) then imp.givenBound else imp.wildcardBound)
206+
}
207+
if reallyExists(denot) then
208+
if unimported.isEmpty || !unimported.contains(pre.termSymbol) then
209+
return pre.select(name, denot)
210+
case _ =>
211+
if imp.sym.isCompleting then
212+
report.warning(i"cyclic ${imp.sym}, ignored", pos)
213+
NoType
211214

212215
/** The type representing a named import with enclosing name when imported
213216
* from given `site` and `selectors`.
@@ -356,7 +359,7 @@ class Typer extends Namer
356359
if !curOwner.is(Package) || isDefinedInCurrentUnit(defDenot) then
357360
result = checkNewOrShadowed(found, Definition) // no need to go further out, we found highest prec entry
358361
found match
359-
case found: NamedType if ctx.owner.isClass && isInherited(found.denot) =>
362+
case found: NamedType if curOwner.isClass && isInherited(found.denot) =>
360363
checkNoOuterDefs(found.denot, ctx, ctx)
361364
case _ =>
362365
else
@@ -373,8 +376,8 @@ class Typer extends Namer
373376
val outer = ctx.outer
374377
val curImport = ctx.importInfo
375378
def updateUnimported() =
376-
if (curImport.unimported.exists) unimported += curImport.unimported
377-
if (ctx.owner.is(Package) && curImport != null && curImport.isRootImport && previous.exists)
379+
if (curImport.unimported ne NoSymbol) unimported += curImport.unimported
380+
if (curOwner.is(Package) && curImport != null && curImport.isRootImport && previous.exists)
378381
previous // no more conflicts possible in this case
379382
else if (isPossibleImport(NamedImport) && (curImport ne outer.importInfo)) {
380383
val namedImp = namedImportRef(curImport)

0 commit comments

Comments
 (0)