Skip to content

Commit

Permalink
Micro-optimize BitPat.rawString (#2577)
Browse files Browse the repository at this point in the history
BitPat.rawString is called a lot when decoding and is used for certain
BitPat operations. We should use it less but this is at least a bandaid.
  • Loading branch information
jackkoenig authored Jun 10, 2022
1 parent b2eca86 commit c11af20
Showing 1 changed file with 22 additions and 8 deletions.
30 changes: 22 additions & 8 deletions src/main/scala/chisel3/util/BitPat.scala
Original file line number Diff line number Diff line change
Expand Up @@ -421,15 +421,29 @@ sealed class BitPat(val value: BigInt, val mask: BigInt, val width: Int)
override def isEmpty: Boolean = false

/** Generate raw string of a [[BitPat]]. */
def rawString: String = Seq
.tabulate(width) { i =>
(value.testBit(width - i - 1), mask.testBit(width - i - 1)) match {
case (true, true) => "1"
case (false, true) => "0"
case (_, false) => "?"
}
def rawString: String = _rawString

// This is micro-optimized and memoized because it is used for lots of BitPat operations
private lazy val _rawString: String = {
val sb = new StringBuilder(width)
var i = 0
while (i < width) {
val bitIdx = width - i - 1
val char =
if (mask.testBit(bitIdx)) {
if (value.testBit(bitIdx)) {
'1'
} else {
'0'
}
} else {
'?'
}
sb += char
i += 1
}
.mkString
sb.result()
}

override def toString = s"BitPat($rawString)"
}

0 comments on commit c11af20

Please sign in to comment.