diff options
| author | Donggyu | 2016-09-13 18:32:30 -0700 |
|---|---|---|
| committer | Jack Koenig | 2016-09-13 18:32:30 -0700 |
| commit | d832d6d1e36be43c958c81b1ca347f2c413eed49 (patch) | |
| tree | d66bebc86b5125fd759f4f8a5634575e985e6546 /src | |
| parent | 7c38199ce7a5d9dd7e27ffbb9b2b2770b972ed94 (diff) | |
cache IntWidths to avoid redudant object creations (#290)
Diffstat (limited to 'src')
| -rw-r--r-- | src/main/scala/firrtl/ir/IR.scala | 33 |
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 = "" |
