Skip to content

Commit ce48f5a

Browse files
authored
Merge pull request #9722 from dotty-staging/optimize-caching
Add some more caches
2 parents a3c81dd + 679345f commit ce48f5a

File tree

4 files changed

+37
-23
lines changed

4 files changed

+37
-23
lines changed

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

+24-16
Original file line numberDiff line numberDiff line change
@@ -1104,8 +1104,6 @@ class Definitions {
11041104
@tu lazy val AbstractFunctionType: Array[TypeRef] = mkArityArray("scala.runtime.AbstractFunction", MaxImplementedFunctionArity, 0)
11051105
val AbstractFunctionClassPerRun: PerRun[Array[Symbol]] = new PerRun(AbstractFunctionType.map(_.symbol.asClass))
11061106
def AbstractFunctionClass(n: Int)(using Context): Symbol = AbstractFunctionClassPerRun()(using ctx)(n)
1107-
@tu private lazy val ImplementedFunctionType = mkArityArray("scala.Function", MaxImplementedFunctionArity, 0)
1108-
def FunctionClassPerRun: PerRun[Array[Symbol]] = new PerRun(ImplementedFunctionType.map(_.symbol.asClass))
11091107

11101108
val LazyHolder: PerRun[Map[Symbol, Symbol]] = new PerRun({
11111109
def holderImpl(holderType: String) = requiredClass("scala.runtime." + holderType)
@@ -1124,23 +1122,33 @@ class Definitions {
11241122

11251123
@tu lazy val TupleType: Array[TypeRef] = mkArityArray("scala.Tuple", MaxTupleArity, 1)
11261124

1125+
private class FunType(prefix: String):
1126+
private var classRefs: Array[TypeRef] = new Array(22)
1127+
def apply(n: Int): TypeRef =
1128+
while n >= classRefs.length do
1129+
val classRefs1 = new Array[TypeRef](classRefs.length * 2)
1130+
Array.copy(classRefs, 0, classRefs1, 0, classRefs.length)
1131+
classRefs = classRefs1
1132+
if classRefs(n) == null then
1133+
classRefs(n) = requiredClassRef(prefix + n.toString)
1134+
classRefs(n)
1135+
1136+
private val erasedContextFunType = FunType("scala.ErasedContextFunction")
1137+
private val contextFunType = FunType("scala.ContextFunction")
1138+
private val erasedFunType = FunType("scala.ErasedFunction")
1139+
private val funType = FunType("scala.Function")
1140+
11271141
def FunctionClass(n: Int, isContextual: Boolean = false, isErased: Boolean = false)(using Context): Symbol =
1128-
if (isContextual && isErased)
1129-
requiredClass("scala.ErasedContextFunction" + n.toString)
1130-
else if (isContextual)
1131-
requiredClass("scala.ContextFunction" + n.toString)
1132-
else if (isErased)
1133-
requiredClass("scala.ErasedFunction" + n.toString)
1134-
else if (n <= MaxImplementedFunctionArity)
1135-
FunctionClassPerRun()(n)
1136-
else
1137-
requiredClass("scala.Function" + n.toString)
1138-
1139-
@tu lazy val Function0_apply: Symbol = ImplementedFunctionType(0).symbol.requiredMethod(nme.apply)
1142+
( if isContextual && isErased then erasedContextFunType(n)
1143+
else if isContextual then contextFunType(n)
1144+
else if isErased then erasedFunType(n)
1145+
else funType(n)
1146+
).symbol.asClass
1147+
1148+
@tu lazy val Function0_apply: Symbol = FunctionClass(0).requiredMethod(nme.apply)
11401149

11411150
def FunctionType(n: Int, isContextual: Boolean = false, isErased: Boolean = false)(using Context): TypeRef =
1142-
if (n <= MaxImplementedFunctionArity && (!isContextual || ctx.erasedTypes) && !isErased) ImplementedFunctionType(n)
1143-
else FunctionClass(n, isContextual, isErased).typeRef
1151+
FunctionClass(n, isContextual && !ctx.erasedTypes, isErased).typeRef
11441152

11451153
lazy val PolyFunctionClass = requiredClass("scala.PolyFunction")
11461154
def PolyFunctionType = PolyFunctionClass.typeRef

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

+11-2
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,15 @@ object Names {
258258
val (first, last, sep) = split
259259
if (first.isEmpty) f2(last) else str.sanitize(f1(first) + sep + f2(last))
260260
}
261+
262+
protected def computeToString: String
263+
264+
@sharable private var myToString: String = null
265+
266+
override def toString =
267+
if myToString == null then myToString = computeToString
268+
myToString
269+
261270
}
262271

263272
/** A simple name is essentially an interned string */
@@ -371,7 +380,7 @@ object Names {
371380

372381
override def hashCode: Int = start
373382

374-
override def toString: String =
383+
protected def computeToString: String =
375384
if (length == 0) ""
376385
else {
377386
if (Config.checkBackendNames)
@@ -500,7 +509,7 @@ object Names {
500509
case qual: QualifiedInfo => qual.name
501510
case _ => underlying.lastPart
502511
}
503-
override def toString: String = info.mkString(underlying)
512+
protected def computeToString: String = info.mkString(underlying)
504513
override def debugString: String = s"${underlying.debugString}[$info]"
505514
}
506515

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

+1-4
Original file line numberDiff line numberDiff line change
@@ -219,10 +219,7 @@ object SymbolLoaders {
219219
Stats.record("package scopes")
220220

221221
/** The scope of a package. This is different from a normal scope
222-
* in two aspects:
223-
*
224-
* 1. Names of scope entries are kept in mangled form.
225-
* 2. Some function types in the `scala` package are synthesized.
222+
* in that names of scope entries are kept in mangled form.
226223
*/
227224
final class PackageScope extends MutableScope {
228225
override def newScopeEntry(name: Name, sym: Symbol)(using Context): ScopeEntry =

tests/pos-with-compiler/lazyValsSepComp.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@ import dotty.tools.dotc.core.Contexts._
1212
object Foo {
1313
val definitions: Definitions = null
1414
def defn = definitions
15-
def go = defn.FunctionClassPerRun
15+
def go = defn.ScalaBoxedClasses
1616
}

0 commit comments

Comments
 (0)