From cf18d879d3f43d5a0c15c5c0af72f88c25605a08 Mon Sep 17 00:00:00 2001 From: Paul Phillips Date: Mon, 30 Apr 2012 07:44:42 -0700 Subject: Optimization of Predef implicits. All those wildcards in a default-scoped implicit are expensive, they each lead to a typevar on every search. Restructured the Tuple2/Tuple3 Zipped classes, they're better this way anyway. This also gets all that Tuple[23] code out of genprod. --- src/compiler/scala/tools/cmd/gen/AnyVals.scala | 45 ++++++++++++++++---------- 1 file changed, 28 insertions(+), 17 deletions(-) (limited to 'src/compiler/scala/tools/cmd') diff --git a/src/compiler/scala/tools/cmd/gen/AnyVals.scala b/src/compiler/scala/tools/cmd/gen/AnyVals.scala index e8230b8ca4..83cd9c2578 100644 --- a/src/compiler/scala/tools/cmd/gen/AnyVals.scala +++ b/src/compiler/scala/tools/cmd/gen/AnyVals.scala @@ -15,18 +15,27 @@ trait AnyValReps { case class Op(val op : String, val doc : String) - private def companionCoercions(tos: String*) = { + private def companionCoercions(tos: AnyValRep*) = { tos.toList map (to => - """implicit def %s2%s(x: %s): %s = x.to%s""".format(javaEquiv, to, name, to.capitalize, to.capitalize) + """implicit def @javaequiv@2%s(x: @name@): %s = x.to%s""".format(to.javaEquiv, to.name, to.name) ) } - def implicitCoercions: List[String] = javaEquiv match { - case "byte" => companionCoercions("short", "int", "long", "float", "double") - case "short" | "char" => companionCoercions("int", "long", "float", "double") - case "int" => companionCoercions("long", "float", "double") - case "long" => companionCoercions("float", "double") - case "float" => companionCoercions("double") - case _ => Nil + def coercionCommentExtra = "" + def coercionComment = """ + /** Language mandated coercions from @name@ to "wider" types.%s + */""".format(coercionCommentExtra) + + def implicitCoercions: List[String] = { + val coercions = this match { + case B => companionCoercions(S, I, L, F, D) + case S | C => companionCoercions(I, L, F, D) + case I => companionCoercions(L, F, D) + case L => companionCoercions(F, D) + case F => companionCoercions(D) + case _ => Nil + } + if (coercions.isEmpty) Nil + else coercionComment :: coercions } def isCardinal: Boolean = isIntegerType(this) @@ -174,7 +183,7 @@ trait AnyValReps { } def objectLines = { val comp = if (isCardinal) cardinalCompanion else floatingCompanion - ((comp + allCompanions).trim.lines map interpolate).toList ++ implicitCoercions + (comp + allCompanions + "\n" + nonUnitCompanions).trim.lines.toList ++ implicitCoercions map interpolate } /** Makes a set of binary operations based on the given set of ops, args, and resultFn. @@ -238,8 +247,9 @@ trait AnyValReps { def classDoc = interpolate(classDocTemplate) def objectDoc = "" def mkImports = "" - def mkClass = assemble("final class", "private", "AnyVal", classLines) + "\n" - def mkObject = assemble("object", "", "AnyValCompanion", objectLines) + "\n" + + def mkClass = assemble("final class " + name + " private extends AnyVal", classLines) + def mkObject = assemble("object " + name + " extends AnyValCompanion", objectLines) def make() = List[String]( headerTemplate, mkImports, @@ -249,11 +259,10 @@ trait AnyValReps { mkObject ) mkString "" - def assemble(what: String, ctor: String, parent: String, lines: List[String]): String = { - val decl = "%s %s %s extends %s ".format(what, name, ctor, parent) - val body = if (lines.isEmpty) "{ }\n\n" else lines map indent mkString ("{\n", "\n", "\n}\n") + def assemble(decl: String, lines: List[String]): String = { + val body = if (lines.isEmpty) " { }\n\n" else lines map indent mkString (" {\n", "\n", "\n}\n") - decl + body + decl + body + "\n" } override def toString = name } @@ -310,6 +319,8 @@ def unbox(x: java.lang.Object): @name@ = @unboxImpl@ override def toString = "object scala.@name@" """ + def nonUnitCompanions = "" // todo + def cardinalCompanion = """ /** The smallest value representable as a @name@. */ @@ -446,7 +457,7 @@ def ^(x: Boolean): Boolean = sys.error("stub") override def getClass(): Class[Boolean] = sys.error("stub") """.trim.lines.toList - def objectLines = interpolate(allCompanions).lines.toList + def objectLines = interpolate(allCompanions + "\n" + nonUnitCompanions).lines.toList } object U extends AnyValRep("Unit", None, "void") { override def classDoc = """ -- cgit v1.2.3