[PATCH] D50224: [ELF] Use zlib::compress which operates on std::unique_ptr<uint8_t[]>

Fangrui Song via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Aug 2 21:59:56 PDT 2018


MaskRay created this revision.
MaskRay added reviewers: ruiu, dblaikie.
Herald added subscribers: llvm-commits, JDevlieghere, arichardson, aprantl, emaste.
Herald added a reviewer: espindola.

compressBound allocates a compressed buffer larger then the source.
zlib::compress on SmallVectorImpl<char> zeroes the buffer and makes
every allocate page resident.

However, the compressed buffer is usually much smaller.
std::unique_ptr<uint8_t[]> backed buffer is uninitialized and avoids
redundant resident pages.

For a test binary with ~1.9GiB uncompressed debug info, this optimization
decreases max RSS by ~1.5GiB.


Repository:
  rLLD LLVM Linker

https://reviews.llvm.org/D50224

Files:
  ELF/OutputSections.cpp
  ELF/OutputSections.h


Index: ELF/OutputSections.h
===================================================================
--- ELF/OutputSections.h
+++ ELF/OutputSections.h
@@ -115,7 +115,8 @@
 private:
   // Used for implementation of --compress-debug-sections option.
   std::vector<uint8_t> ZDebugHeader;
-  llvm::SmallVector<char, 1> CompressedData;
+  std::unique_ptr<uint8_t[]> CompressedData;
+  size_t CompressedSize;
 
   uint32_t getFiller();
 };
Index: ELF/OutputSections.cpp
===================================================================
--- ELF/OutputSections.cpp
+++ ELF/OutputSections.cpp
@@ -197,11 +197,12 @@
   // Write section contents to a temporary buffer and compress it.
   std::vector<uint8_t> Buf(Size);
   writeTo<ELFT>(Buf.data());
-  if (Error E = zlib::compress(toStringRef(Buf), CompressedData))
+  if (Error E =
+          zlib::compress(toStringRef(Buf), CompressedData, CompressedSize))
     fatal("compress failed: " + llvm::toString(std::move(E)));
 
   // Update section headers.
-  Size = sizeof(Elf_Chdr) + CompressedData.size();
+  Size = sizeof(Elf_Chdr) + CompressedSize;
   Flags |= SHF_COMPRESSED;
 }
 
@@ -227,10 +228,9 @@
   // If -compress-debug-section is specified and if this is a debug seciton,
   // we've already compressed section contents. If that's the case,
   // just write it down.
-  if (!CompressedData.empty()) {
+  if (CompressedData) {
     memcpy(Buf, ZDebugHeader.data(), ZDebugHeader.size());
-    memcpy(Buf + ZDebugHeader.size(), CompressedData.data(),
-           CompressedData.size());
+    memcpy(Buf + ZDebugHeader.size(), CompressedData.get(), CompressedSize);
     return;
   }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D50224.158915.patch
Type: text/x-patch
Size: 1633 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180803/5210c7d1/attachment.bin>


More information about the llvm-commits mailing list