aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorDonggyu Kim2016-09-08 21:49:43 -0700
committerDonggyu Kim2016-09-13 17:27:19 -0700
commita8d7e31e5cbe6c387930f8d4ef48fda19e59f465 (patch)
treed66bebc86b5125fd759f4f8a5634575e985e6546 /src
parent144041a23329f258541e9d461b5bcc7f2284e686 (diff)
cache IntWidths to avoid redudant object creations
Diffstat (limited to 'src')
-rw-r--r--src/main/scala/firrtl/ir/IR.scala33
1 files changed, 32 insertions, 1 deletions
diff --git a/src/main/scala/firrtl/ir/IR.scala b/src/main/scala/firrtl/ir/IR.scala
index cdbf2fd7..87072eec 100644
--- a/src/main/scala/firrtl/ir/IR.scala
+++ b/src/main/scala/firrtl/ir/IR.scala
@@ -300,8 +300,39 @@ abstract class Width extends FirrtlNode {
}
}
/** Positive Integer Bit Width of a [[GroundType]] */
-case class IntWidth(width: BigInt) extends Width {
+object IntWidth {
+ private val maxCached = 1024
+ private val cache = new Array[IntWidth](maxCached + 1)
+ def apply(width: BigInt): IntWidth = {
+ if (0 <= width && width <= maxCached) {
+ val i = width.toInt
+ var w = cache(i)
+ if (w eq null) {
+ w = new IntWidth(width)
+ cache(i) = w
+ }
+ w
+ } else new IntWidth(width)
+ }
+ // For pattern matching
+ def unapply(w: IntWidth): Option[BigInt] = Some(w.width)
+}
+class IntWidth(val width: BigInt) extends Width with Product {
def serialize: String = s"<$width>"
+ override def equals(that: Any) = that match {
+ case w: IntWidth => width == w.width
+ case _ => false
+ }
+ override def hashCode = width.toInt
+ override def productPrefix = "IntWidth"
+ override def toString = s"$productPrefix($width)"
+ def copy(width: BigInt = width) = IntWidth(width)
+ def canEqual(that: Any) = that.isInstanceOf[Width]
+ def productArity = 1
+ def productElement(int: Int) = int match {
+ case 0 => width
+ case _ => throw new IndexOutOfBoundsException
+ }
}
case object UnknownWidth extends Width {
def serialize: String = ""