Internals: Optimize temporary memory allocations (#6517)

This patch gets rid of over 80% of temporary dynamic memory allocations
(when a malloced node is immediately freed with no other malloc in
between). It also gets rid of over 20% of all calls to malloc.

It's worth ~3% average verilation speed up with tcmalloc, and more
without tcmalloc.
This commit is contained in:
Geza Lore
2025-10-01 15:01:30 +01:00
committed by GitHub
parent 435e1149d5
commit e9c48cd1ce
19 changed files with 218 additions and 216 deletions
+11 -3
View File
@@ -651,10 +651,18 @@ V3OutFormatter::V3OutFormatter(V3OutFormatter::Language lang)
//----------------------------------------------------------------------
string V3OutFormatter::indentSpaces(int num) {
static constexpr int MAXSPACE = 80; // After this indent, stop indenting more
// Table of spaces, so we don't have to alloc/free them all the time
static const std::array<std::string, MAXSPACE + 1> s_indentSpaces = []() {
std::array<std::string, MAXSPACE + 1> table;
for (int i = 0; i <= MAXSPACE; ++i) table[i] = std::string(static_cast<size_t>(i), ' ');
return table;
}();
const std::string& V3OutFormatter::indentSpaces(int num) {
// Indent the specified number of spaces.
if (num <= 0) return std::string{};
return std::string(std::min<size_t>(num, MAXSPACE), ' ');
return s_indentSpaces[std::max(0, std::min(num, MAXSPACE))];
}
bool V3OutFormatter::tokenMatch(const char* cp, const char* cmp) {