From 0915ae6ba8f7c13bc2ee30bc431e233c4605c95b Mon Sep 17 00:00:00 2001 From: Geza Lore Date: Sat, 31 Jan 2026 16:05:35 +0000 Subject: [PATCH] Optimize string temporaries - do not localize (#6969) Minor performance improvement, especially for assertions heavy code. Strings are often used as temporaries in unlikely branches. Do not localize them to avoid an unnecessary initialization on function entry. --- src/V3Localize.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/V3Localize.cpp b/src/V3Localize.cpp index ce8dbd5d2..edafffdee 100644 --- a/src/V3Localize.cpp +++ b/src/V3Localize.cpp @@ -69,6 +69,11 @@ class LocalizeVisitor final : public VNVisitor { bool isOptimizable(AstVarScope* nodep) { // Don't want to malloc/free the backing store all the time if (VN_IS(nodep->dtypep(), NBACommitQueueDType)) return false; + // Do not localize strings. They result in unnecessary initialization + // and bloated code size due to destructor calls when unused. + // TODO: Local variables should be pushed into the narrowest scope rather + // than emitted at the top of the function. See discussion in #6969. + if (nodep->dtypep()->skipRefp()->isString()) return false; // Variables used in super constructor call can't be localized, because // in C++ there is no way to declare them before base class constructor call if (nodep->user4()) return false;