Skip to content

Commit 5312d63

Browse files
committed
Merge pull request scala#2582 from paulp/pr/SubstTypeMap
Cache the most recently created SubstTypeMap.
2 parents 803d451 + 67caf85 commit 5312d63

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,18 @@ trait Types
107107

108108
protected val enableTypeVarExperimentals = settings.Xexperimental.value
109109

110+
/** Caching the most recent map has a 75-90% hit rate. */
111+
private object substTypeMapCache {
112+
private[this] var cached: SubstTypeMap = new SubstTypeMap(Nil, Nil)
113+
114+
def apply(from: List[Symbol], to: List[Type]): SubstTypeMap = {
115+
if ((cached.from ne from) || (cached.to ne to))
116+
cached = new SubstTypeMap(from, to)
117+
118+
cached
119+
}
120+
}
121+
110122
/** The current skolemization level, needed for the algorithms
111123
* in isSameType, isSubType that do constraint solving under a prefix.
112124
*/
@@ -698,8 +710,7 @@ trait Types
698710
* symbols `from` in this type.
699711
*/
700712
def subst(from: List[Symbol], to: List[Type]): Type =
701-
if (from.isEmpty) this
702-
else new SubstTypeMap(from, to) apply this
713+
if (from.isEmpty) this else substTypeMapCache(from, to)(this)
703714

704715
/** Substitute symbols `to` for occurrences of symbols `from` in this type.
705716
*

src/reflect/scala/reflect/internal/tpe/TypeMaps.scala

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -794,8 +794,7 @@ private[internal] trait TypeMaps {
794794
}
795795

796796
/** A map to implement the `subst` method. */
797-
class SubstTypeMap(from: List[Symbol], to: List[Type])
798-
extends SubstMap(from, to) {
797+
class SubstTypeMap(val from: List[Symbol], val to: List[Type]) extends SubstMap(from, to) {
799798
protected def toType(fromtp: Type, tp: Type) = tp
800799

801800
override def mapOver(tree: Tree, giveup: () => Nothing): Tree = {

0 commit comments

Comments
 (0)