From d9b3a3f7bb6f054181d587298b9ca3f19fa816b1 Mon Sep 17 00:00:00 2001 From: Drew Lewis Date: Tue, 22 Jul 2025 15:38:02 +0000 Subject: [PATCH] Improve the performance of `concat_name` in the flattening pass - Make IdString parameter pass by const ref to avoid IdString ref counting in the constructor - Remove extra std::string allocation to remove prefix - Avoid using `stringf` for concatenation --- passes/techmap/flatten.cc | 32 ++++++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/passes/techmap/flatten.cc b/passes/techmap/flatten.cc index 6363b3432..299b18006 100644 --- a/passes/techmap/flatten.cc +++ b/passes/techmap/flatten.cc @@ -24,20 +24,36 @@ #include #include #include +#include USING_YOSYS_NAMESPACE PRIVATE_NAMESPACE_BEGIN -IdString concat_name(RTLIL::Cell *cell, IdString object_name, const std::string &separator = ".") +template +[[nodiscard]] std::string concat_views(const Args&... views) { + static_assert((std::is_convertible_v && ...), + "All arguments must be convertible to std::string_view."); + const std::size_t total_size = (std::string_view(views).size() + ... + 0); + + std::string result; + result.reserve(total_size); + + (result.append(views), ...); + return result; +} + +IdString concat_name(RTLIL::Cell *cell, IdString const &object_name, const std::string &separator = ".") { - if (object_name[0] == '\\') - return stringf("%s%s%s", cell->name.c_str(), separator.c_str(), object_name.c_str() + 1); - else { - std::string object_name_str = object_name.str(); - if (object_name_str.substr(0, 8) == "$flatten") - object_name_str.erase(0, 8); - return stringf("$flatten%s%s%s", cell->name.c_str(), separator.c_str(), object_name_str.c_str()); + std::string_view object_name_view(object_name.c_str()); + if (object_name_view[0] == '\\'){ + return concat_views(cell->name.c_str(), separator, object_name_view.substr(1)); } + + constexpr std::string_view prefix = "$flatten"; + if (object_name_view.substr(0, prefix.size()) == prefix){ + object_name_view.remove_prefix(prefix.size()); + } + return concat_views(prefix, cell->name.c_str(), separator, object_name_view); } template