diff options
author | Paul Phillips <paulp@improving.org> | 2013-05-22 20:57:06 -0700 |
---|---|---|
committer | Paul Phillips <paulp@improving.org> | 2013-05-22 21:17:16 -0700 |
commit | 67caf85d73932af2734e9e16b7d9ff90943b64e9 (patch) | |
tree | 015400404de90859973eb64173c5c8479d7947c1 | |
parent | 649d5bb3a59326ea8fb7790f6abc948951c73905 (diff) | |
download | scala-67caf85d73932af2734e9e16b7d9ff90943b64e9.tar.gz scala-67caf85d73932af2734e9e16b7d9ff90943b64e9.tar.bz2 scala-67caf85d73932af2734e9e16b7d9ff90943b64e9.zip |
Cache the most recently created SubstTypeMap.
I discovered that a 1-element cache for SubstTypeMaps
gave a 75-90% hit rate.
-rw-r--r-- | src/reflect/scala/reflect/internal/Types.scala | 15 | ||||
-rw-r--r-- | src/reflect/scala/reflect/internal/tpe/TypeMaps.scala | 3 |
2 files changed, 14 insertions, 4 deletions
diff --git a/src/reflect/scala/reflect/internal/Types.scala b/src/reflect/scala/reflect/internal/Types.scala index 6582459afe..a1963e010d 100644 --- a/src/reflect/scala/reflect/internal/Types.scala +++ b/src/reflect/scala/reflect/internal/Types.scala @@ -107,6 +107,18 @@ trait Types protected val enableTypeVarExperimentals = settings.Xexperimental.value + /** Caching the most recent map has a 75-90% hit rate. */ + private object substTypeMapCache { + private[this] var cached: SubstTypeMap = new SubstTypeMap(Nil, Nil) + + def apply(from: List[Symbol], to: List[Type]): SubstTypeMap = { + if ((cached.from ne from) || (cached.to ne to)) + cached = new SubstTypeMap(from, to) + + cached + } + } + /** The current skolemization level, needed for the algorithms * in isSameType, isSubType that do constraint solving under a prefix. */ @@ -698,8 +710,7 @@ trait Types * symbols `from` in this type. */ def subst(from: List[Symbol], to: List[Type]): Type = - if (from.isEmpty) this - else new SubstTypeMap(from, to) apply this + if (from.isEmpty) this else substTypeMapCache(from, to)(this) /** Substitute symbols `to` for occurrences of symbols `from` in this type. * diff --git a/src/reflect/scala/reflect/internal/tpe/TypeMaps.scala b/src/reflect/scala/reflect/internal/tpe/TypeMaps.scala index 7665e7c0f4..5384acbc37 100644 --- a/src/reflect/scala/reflect/internal/tpe/TypeMaps.scala +++ b/src/reflect/scala/reflect/internal/tpe/TypeMaps.scala @@ -794,8 +794,7 @@ private[internal] trait TypeMaps { } /** A map to implement the `subst` method. */ - class SubstTypeMap(from: List[Symbol], to: List[Type]) - extends SubstMap(from, to) { + class SubstTypeMap(val from: List[Symbol], val to: List[Type]) extends SubstMap(from, to) { protected def toType(fromtp: Type, tp: Type) = tp override def mapOver(tree: Tree, giveup: () => Nothing): Tree = { |