[clang] [clang][modules] Avoid allocations when reading blob paths (PR #113984)

Ben Langmuir via cfe-commits cfe-commits at lists.llvm.org
Wed Oct 30 08:31:18 PDT 2024


================
@@ -1342,19 +1343,47 @@ class ASTReader
                                         bool Complain = true);
 
   /// Buffer we use as temporary storage backing resolved paths.
-  SmallString<256> PathBuf;
+  std::optional<SmallString<256>> PathBuf{{}};
+
+  /// A RAII wrapper around \c StringRef that temporarily takes ownership of the
+  /// underlying buffer and gives it back on destruction.
+  class TemporarilyOwnedStringRef {
+    StringRef String;
+    llvm::SaveAndRestore<std::optional<SmallString<256>>> TemporaryLoan;
----------------
benlangmuir wrote:

I think we should switch to `SmallString<0>` so that save and restore is just swapping a pointer instead of copying 256 bytes and we don't waste the extra stack space in every caller. If you `reserve(250)` it will only have a single allocation for the whole compilation.

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


More information about the cfe-commits mailing list