Skip to content

Commit

Permalink
Optimize memory use of naming prefixes (#2471)
Browse files Browse the repository at this point in the history
* Use a single field instead of two in HasId (4-bytes per HasId)
* Set the prefix to Nil after setting ref to free up memory

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
(cherry picked from commit 3aa179f)
  • Loading branch information
jackkoenig authored and mergify-bot committed Apr 12, 2022
1 parent 898142b commit fdbe81f
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions core/src/main/scala/chisel3/internal/Builder.scala
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,10 @@ private[chisel3] trait HasId extends InstanceId {
// Contains the seed computed automatically by the compiler plugin
private var auto_seed: Option[String] = None

// Prefix at time when this class is constructed
private val construction_prefix: Prefix = Builder.getPrefix

// Prefix when the latest [[suggestSeed]] or [[autoSeed]] is called
private var prefix_seed: Prefix = Nil
// Prefix for use in naming
// - Defaults to prefix at time when object is created
// - Overridden when [[suggestSeed]] or [[autoSeed]] is called
private var naming_prefix: Prefix = Builder.getPrefix

// Post-seed hooks called to carry the suggested seeds to other candidates as needed
private var suggest_postseed_hooks: List[String => Unit] = Nil
Expand All @@ -134,7 +133,7 @@ private[chisel3] trait HasId extends InstanceId {
private[chisel3] def forceAutoSeed(seed: String): this.type = {
auto_seed = Some(seed)
for (hook <- auto_postseed_hooks.reverse) { hook(seed) }
prefix_seed = Builder.getPrefix
naming_prefix = Builder.getPrefix
this
}

Expand All @@ -150,7 +149,7 @@ private[chisel3] trait HasId extends InstanceId {
*/
def suggestName(seed: => String): this.type = {
if (suggested_seed.isEmpty) suggested_seed = Some(seed)
prefix_seed = Builder.getPrefix
naming_prefix = Builder.getPrefix
for (hook <- suggest_postseed_hooks.reverse) { hook(seed) }
this
}
Expand Down Expand Up @@ -189,12 +188,12 @@ private[chisel3] trait HasId extends InstanceId {
}

if (hasSeed) {
Some(buildName(seedOpt.get, prefix_seed.reverse))
Some(buildName(seedOpt.get, naming_prefix.reverse))
} else {
defaultSeed.map { default =>
defaultPrefix match {
case Some(p) => buildName(default, p :: construction_prefix.reverse)
case None => buildName(default, construction_prefix.reverse)
case Some(p) => buildName(default, p :: naming_prefix.reverse)
case None => buildName(default, naming_prefix.reverse)
}
}
}
Expand Down Expand Up @@ -222,6 +221,8 @@ private[chisel3] trait HasId extends InstanceId {
val candidate_name = _computeName(prefix, Some(default)).get
val available_name = namespace.name(candidate_name)
setRef(Ref(available_name))
// Clear naming prefix to free memory
naming_prefix = Nil
}

private var _ref: Option[Arg] = None
Expand Down

0 comments on commit fdbe81f

Please sign in to comment.