Permalink
Browse files

SI-6726 Hash consing for Pattern matching Sym-s

For the test case: https://issues.scala-lang.org/browse/SI-6726?focusedCommentId=61207&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-61207

time scalac -Ystatistics -Ystop-after:patmat sandbox/gen.scala

Before:

    real    1m47.737s
    user    2m14.314s
    sys 0m2.783s

After:

    real    1m23.574s
    user    1m51.795s
    sys 0m2.634s
  • Loading branch information...
1 parent 32c0a2e commit 14d8c222aa5f810fea34d35dc5f50d7f17ddc091 @retronym retronym committed with adriaanm Jan 22, 2013
View
19 src/compiler/scala/tools/nsc/typechecker/PatternMatching.scala
@@ -1897,17 +1897,24 @@ trait PatternMatching extends Transform with TypingTransformers with ast.TreeDSL
case object False extends Prop
// symbols are propositions
- case class Sym(val variable: Var, val const: Const) extends Prop {
- private[this] val id = nextSymId
+ abstract case class Sym(val variable: Var, val const: Const) extends Prop {
+ private[this] val id = Sym.nextSymId
+
override def toString = variable +"="+ const +"#"+ id
}
- private def nextSymId = {_symId += 1; _symId}; private var _symId = 0
-
+ class UniqueSym(variable: Var, const: Const) extends Sym(variable, const)
+ object Sym {
+ private var uniques: util.HashSet[Sym] = new util.HashSet("uniques", 512)
+ def apply(variable: Var, const: Const): Sym = {
+ val newSym = new UniqueSym(variable, const)
+ (uniques findEntryOrUpdate newSym)
+ }
+ private def nextSymId = {_symId += 1; _symId}; private var _symId = 0
+ }
def /\(props: Iterable[Prop]) = if (props.isEmpty) True else props.reduceLeft(And(_, _))
def \/(props: Iterable[Prop]) = if (props.isEmpty) False else props.reduceLeft(Or(_, _))
-
trait PropTraverser {
def apply(x: Prop): Unit = x match {
case And(a, b) => apply(a); apply(b)
@@ -2167,7 +2174,7 @@ trait PatternMatching extends Transform with TypingTransformers with ast.TreeDSL
class Lit(val sym: Sym, val pos: Boolean) {
override def toString = if (!pos) "-"+ sym.toString else sym.toString
override def equals(o: Any) = o match {
- case o: Lit => (o.sym == sym) && (o.pos == pos)
+ case o: Lit => (o.sym eq sym) && (o.pos == pos)
case _ => false
}
override def hashCode = sym.hashCode + pos.hashCode
View
3 src/reflect/scala/reflect/api/Symbols.scala
@@ -351,7 +351,8 @@ trait Symbols { self: Universe =>
def asFreeType: FreeTypeSymbol = throw new ScalaReflectionException(s"$this is not a free type")
/** @group Constructors */
- def newTermSymbol(name: TermName, pos: Position = NoPosition, flags: FlagSet = NoFlags): TermSymbol
+ def
+ newTermSymbol(name: TermName, pos: Position = NoPosition, flags: FlagSet = NoFlags): TermSymbol
/** @group Constructors */
def newModuleAndClassSymbol(name: Name, pos: Position = NoPosition, flags: FlagSet = NoFlags): (ModuleSymbol, ClassSymbol)
/** @group Constructors */

0 comments on commit 14d8c22

Please sign in to comment.