summaryrefslogtreecommitdiff
path: root/src/reflect/scala/reflect/internal/Types.scala
diff options
context:
space:
mode:
authorPaul Phillips <paulp@improving.org>2013-05-22 20:57:06 -0700
committerPaul Phillips <paulp@improving.org>2013-05-22 21:17:16 -0700
commit67caf85d73932af2734e9e16b7d9ff90943b64e9 (patch)
tree015400404de90859973eb64173c5c8479d7947c1 /src/reflect/scala/reflect/internal/Types.scala
parent649d5bb3a59326ea8fb7790f6abc948951c73905 (diff)
downloadscala-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.
Diffstat (limited to 'src/reflect/scala/reflect/internal/Types.scala')
-rw-r--r--src/reflect/scala/reflect/internal/Types.scala15
1 files changed, 13 insertions, 2 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.
*