From 67caf85d73932af2734e9e16b7d9ff90943b64e9 Mon Sep 17 00:00:00 2001 From: Paul Phillips Date: Wed, 22 May 2013 20:57:06 -0700 Subject: Cache the most recently created SubstTypeMap. I discovered that a 1-element cache for SubstTypeMaps gave a 75-90% hit rate. --- src/reflect/scala/reflect/internal/Types.scala | 15 +++++++++++++-- src/reflect/scala/reflect/internal/tpe/TypeMaps.scala | 3 +-- 2 files changed, 14 insertions(+), 4 deletions(-) (limited to 'src/reflect') 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 = { -- cgit v1.2.3