[clang] [clang] Avoid per-builtin std::string allocation in initializeBuiltins (PR #205162)

Jan Svoboda via cfe-commits cfe-commits at lists.llvm.org
Mon Jun 22 12:47:52 PDT 2026


================
@@ -71,8 +72,21 @@ Builtin::Context::getShardAndInfo(unsigned ID) const {
   llvm_unreachable("Invalid target builtin shard structure!");
 }
 
+/// Return a non-owning StringRef of the builtin's name, reconstructed into Buf.
+static StringRef getBuiltinNameInto(const Builtin::InfosShard &Shard,
+                                    const Builtin::Info &BuiltinInfo,
+                                    SmallVectorImpl<char> &Buf) {
+  StringRef Name = (*Shard.Strings)[BuiltinInfo.Offsets.Name];
+  if (Shard.NamePrefix.empty())
+    return Name;
+  Buf.assign(Shard.NamePrefix.begin(), Shard.NamePrefix.end());
+  Buf.append(Name.begin(), Name.end());
+  return StringRef(Buf.data(), Buf.size());
+}
+
 std::string Builtin::Info::getName(const Builtin::InfosShard &Shard) const {
-  return (Twine(Shard.NamePrefix) + (*Shard.Strings)[Offsets.Name]).str();
+  SmallString<256> Buf;
+  return getBuiltinNameInto(Shard, *this, Buf).str();
 }
----------------
jansvoboda11 wrote:

Would it make sense for callers of `Builtin::Info::getName()` to provide the `Buf` and get a `StringRef` from this function? That would reduce allocations even further.

https://github.com/llvm/llvm-project/pull/205162


More information about the cfe-commits mailing list