Skip to content
This repository
Browse code

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...
commit 14d8c222aa5f810fea34d35dc5f50d7f17ddc091 1 parent 32c0a2e
Jason Zaugg authored January 22, 2013 adriaanm committed January 30, 2013
19  src/compiler/scala/tools/nsc/typechecker/PatternMatching.scala
@@ -1897,17 +1897,24 @@ trait PatternMatching extends Transform with TypingTransformers with ast.TreeDSL
1897 1897
     case object False extends Prop
1898 1898
 
1899 1899
     // symbols are propositions
1900  
-    case class Sym(val variable: Var, val const: Const) extends Prop {
1901  
-      private[this] val id = nextSymId
  1900
+    abstract case class Sym(val variable: Var, val const: Const) extends Prop {
  1901
+      private[this] val id = Sym.nextSymId
  1902
+
1902 1903
       override def toString = variable +"="+ const +"#"+ id
1903 1904
     }
1904  
-    private def nextSymId = {_symId += 1; _symId}; private var _symId = 0
1905  
-
  1905
+    class UniqueSym(variable: Var, const: Const) extends Sym(variable, const)
  1906
+    object Sym {
  1907
+      private var uniques: util.HashSet[Sym] = new util.HashSet("uniques", 512)
  1908
+      def apply(variable: Var, const: Const): Sym = {
  1909
+        val newSym = new UniqueSym(variable, const)
  1910
+        (uniques findEntryOrUpdate newSym)
  1911
+      }
  1912
+      private def nextSymId = {_symId += 1; _symId}; private var _symId = 0
  1913
+    }
1906 1914
 
1907 1915
     def /\(props: Iterable[Prop]) = if (props.isEmpty) True else props.reduceLeft(And(_, _))
1908 1916
     def \/(props: Iterable[Prop]) = if (props.isEmpty) False else props.reduceLeft(Or(_, _))
1909 1917
 
1910  
-
1911 1918
     trait PropTraverser {
1912 1919
       def apply(x: Prop): Unit = x match {
1913 1920
         case And(a, b) => apply(a); apply(b)
@@ -2167,7 +2174,7 @@ trait PatternMatching extends Transform with TypingTransformers with ast.TreeDSL
2167 2174
     class Lit(val sym: Sym, val pos: Boolean) {
2168 2175
       override def toString = if (!pos) "-"+ sym.toString else sym.toString
2169 2176
       override def equals(o: Any) = o match {
2170  
-        case o: Lit => (o.sym == sym) && (o.pos == pos)
  2177
+        case o: Lit => (o.sym eq sym) && (o.pos == pos)
2171 2178
         case _ => false
2172 2179
       }
2173 2180
       override def hashCode = sym.hashCode + pos.hashCode
3  src/reflect/scala/reflect/api/Symbols.scala
@@ -351,7 +351,8 @@ trait Symbols { self: Universe =>
351 351
     def asFreeType: FreeTypeSymbol = throw new ScalaReflectionException(s"$this is not a free type")
352 352
 
353 353
     /** @group Constructors */
354  
-    def newTermSymbol(name: TermName, pos: Position = NoPosition, flags: FlagSet = NoFlags): TermSymbol
  354
+    def
  355
+    newTermSymbol(name: TermName, pos: Position = NoPosition, flags: FlagSet = NoFlags): TermSymbol
355 356
     /** @group Constructors */
356 357
     def newModuleAndClassSymbol(name: Name, pos: Position = NoPosition, flags: FlagSet = NoFlags): (ModuleSymbol, ClassSymbol)
357 358
     /** @group Constructors */

0 notes on commit 14d8c22

Please sign in to comment.
Something went wrong with that request. Please try again.