From 5f1e18b1cd8f76f40bb01d257ae3b81cb70e3e07 Mon Sep 17 00:00:00 2001 From: Paul Phillips Date: Mon, 3 Dec 2012 11:53:17 -0800 Subject: Optimization in SpecializeTypes. Avoid time traveling to find type parameters which will never be there. --- .../scala/tools/nsc/transform/SpecializeTypes.scala | 15 ++++++++++----- src/compiler/scala/tools/nsc/typechecker/Typers.scala | 9 ++------- src/reflect/scala/reflect/internal/Definitions.scala | 15 +++++++++++++++ 3 files changed, 27 insertions(+), 12 deletions(-) diff --git a/src/compiler/scala/tools/nsc/transform/SpecializeTypes.scala b/src/compiler/scala/tools/nsc/transform/SpecializeTypes.scala index 4e4c1b98ac..e3239313de 100644 --- a/src/compiler/scala/tools/nsc/transform/SpecializeTypes.scala +++ b/src/compiler/scala/tools/nsc/transform/SpecializeTypes.scala @@ -403,11 +403,16 @@ abstract class SpecializeTypes extends InfoTransform with TypingTransformers { case _ => false }) def specializedTypeVars(tpes: List[Type]): immutable.Set[Symbol] = { - val buf = Set.newBuilder[Symbol] - tpes foreach (tp => buf ++= specializedTypeVars(tp)) - buf.result + if (tpes.isEmpty) immutable.Set.empty else { + val buf = Set.newBuilder[Symbol] + tpes foreach (tp => buf ++= specializedTypeVars(tp)) + buf.result + } } - def specializedTypeVars(sym: Symbol): immutable.Set[Symbol] = enteringTyper(specializedTypeVars(sym.info)) + def specializedTypeVars(sym: Symbol): immutable.Set[Symbol] = ( + if (definitions.neverHasTypeParameters(sym)) immutable.Set.empty + else enteringTyper(specializedTypeVars(sym.info)) + ) /** Return the set of @specialized type variables mentioned by the given type. * It only counts type variables that appear: @@ -436,7 +441,7 @@ abstract class SpecializeTypes extends InfoTransform with TypingTransformers { case AnnotatedType(_, tp, _) => specializedTypeVars(tp) case TypeBounds(lo, hi) => specializedTypeVars(lo :: hi :: Nil) case RefinedType(parents, _) => parents flatMap specializedTypeVars toSet - case _ => Set() + case _ => immutable.Set.empty } /** Returns the type parameter in the specialized class `sClass` that corresponds to type parameter diff --git a/src/compiler/scala/tools/nsc/typechecker/Typers.scala b/src/compiler/scala/tools/nsc/typechecker/Typers.scala index a1c1b53cce..703c12038f 100644 --- a/src/compiler/scala/tools/nsc/typechecker/Typers.scala +++ b/src/compiler/scala/tools/nsc/typechecker/Typers.scala @@ -2716,13 +2716,8 @@ trait Typers extends Modes with Adaptations with Tags { val att = templ.attachments.get[CompoundTypeTreeOriginalAttachment].getOrElse(CompoundTypeTreeOriginalAttachment(Nil, Nil)) templ.removeAttachment[CompoundTypeTreeOriginalAttachment] templ updateAttachment att.copy(stats = stats1) - for (stat <- stats1 if stat.isDef) { - val member = stat.symbol - if (!(context.owner.ancestors forall - (bc => member.matchingSymbol(bc, context.owner.thisType) == NoSymbol))) { - member setFlag OVERRIDE - } - } + for (stat <- stats1 if stat.isDef && stat.symbol.isOverridingSymbol) + stat.symbol setFlag OVERRIDE } } diff --git a/src/reflect/scala/reflect/internal/Definitions.scala b/src/reflect/scala/reflect/internal/Definitions.scala index 8c048ed7f8..d165f66004 100644 --- a/src/reflect/scala/reflect/internal/Definitions.scala +++ b/src/reflect/scala/reflect/internal/Definitions.scala @@ -686,6 +686,21 @@ trait Definitions extends api.StandardDefinitions { def ClassType(arg: Type) = if (phase.erasedTypes) ClassClass.tpe else appliedType(ClassClass, arg) + /** Can we tell by inspecting the symbol that it will never + * at any phase have type parameters? + */ + def neverHasTypeParameters(sym: Symbol) = sym match { + case _: RefinementClassSymbol => true + case _: ModuleClassSymbol => true + case _: ImplClassSymbol => true + case _ => + ( + sym.isPrimitiveValueClass + || sym.isAnonymousClass + || sym.initialize.isMonomorphicType + ) + } + def EnumType(sym: Symbol) = // given (in java): "class A { enum E { VAL1 } }" // - sym: the symbol of the actual enumeration value (VAL1) -- cgit v1.2.3