llvmorg-github-actions[bot] wrote:

<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Jan Svoboda (jansvoboda11)

<details>
<summary>Changes</summary>

`CompilerInstance::compileModule()` serializes the PCM into an empty 
`SmallString&lt;0&gt;` in a single append, which allocates exactly as much 
memory as the PCM needs. Wrapping it in a `SmallVectorMemoryBuffer` that 
requires a null terminator then pushes '\0' onto the full vector, which grows 
it to twice the size and copies the PCM into the new allocation.

None of the consumers of the buffer need the null terminator: it only gets 
written to the module cache, and the AST file gets extracted from it and copied 
into the `InMemoryModuleCache`. PCMs read from disk aren't null-terminated 
either.

This PR stops requesting the null terminator, which avoids reallocating and 
copying every built PCM, and halves the memory allocated for it.

---
Full diff: https://github.com/llvm/llvm-project/pull/242089.diff


1 Files Affected:

- (modified) clang/lib/Frontend/CompilerInstance.cpp (+4-1) 


``````````diff
diff --git a/clang/lib/Frontend/CompilerInstance.cpp 
b/clang/lib/Frontend/CompilerInstance.cpp
index f122208d6b4c8..7e9d43c418a0f 100644
--- a/clang/lib/Frontend/CompilerInstance.cpp
+++ b/clang/lib/Frontend/CompilerInstance.cpp
@@ -1396,8 +1396,11 @@ CompilerInstance::compileModule(SourceLocation 
ImportLoc, StringRef ModuleName,
       !Instance.getFrontendOpts().AllowPCMWithCompilerErrors)
     return nullptr;
 
+  // PCMs don't need to be null-terminated. Since the buffer typically has no
+  // spare capacity, adding the terminator would reallocate and copy it.
   return std::make_unique<llvm::SmallVectorMemoryBuffer>(
-      std::move(Buffer), Instance.getFrontendOpts().OutputFile);
+      std::move(Buffer), Instance.getFrontendOpts().OutputFile,
+      /*RequiresNullTerminator=*/false);
 }
 
 static OptionalFileEntryRef getPublicModuleMap(FileEntryRef File,

``````````

</details>


https://github.com/llvm/llvm-project/pull/242089
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to