https://github.com/jansvoboda11 created https://github.com/llvm/llvm-project/pull/242089
`CompilerInstance::compileModule()` serializes the PCM into an empty `SmallString<0>` 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. >From ab86c11d41d14bfe0ef5f04d0c68bf2be521de72 Mon Sep 17 00:00:00 2001 From: Jan Svoboda <[email protected]> Date: Thu, 12 Nov 2026 14:31:25 -0800 Subject: [PATCH] [clang] Don't null-terminate built PCMs `CompilerInstance::compileModule()` serializes the PCM into an empty `SmallString<0>` 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. --- clang/lib/Frontend/CompilerInstance.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) 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, _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
