diff options
author | Adriaan Moors <adriaan.moors@typesafe.com> | 2013-06-05 18:00:02 -0700 |
---|---|---|
committer | Adriaan Moors <adriaan.moors@typesafe.com> | 2013-06-05 18:00:02 -0700 |
commit | 5312d6305530eb14d369d0f4acaf7ca4e278ea72 (patch) | |
tree | b02deb0eeae845a5f914b10df509360ca94ea62b | |
parent | 803d451a28824af17f0cab446e4c76f51003fd01 (diff) | |
parent | 67caf85d73932af2734e9e16b7d9ff90943b64e9 (diff) | |
download | scala-5312d6305530eb14d369d0f4acaf7ca4e278ea72.tar.gz scala-5312d6305530eb14d369d0f4acaf7ca4e278ea72.tar.bz2 scala-5312d6305530eb14d369d0f4acaf7ca4e278ea72.zip |
Merge pull request #2582 from paulp/pr/SubstTypeMap
Cache the most recently created SubstTypeMap.
-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 0a50eb8ecc..5c9665cdc7 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 c35825dcee..bebc419c7c 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 = { |