[lld] 223f9de - [ELF] maybeCompress: replace vector<uint8_t> with unique_ptr<uint8_t[]>. NFC

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Tue Jan 25 22:15:48 PST 2022


Author: Fangrui Song
Date: 2022-01-25T22:15:44-08:00
New Revision: 223f9dea3d8b46847c635a1ce864917e48da01ae

URL: https://github.com/llvm/llvm-project/commit/223f9dea3d8b46847c635a1ce864917e48da01ae
DIFF: https://github.com/llvm/llvm-project/commit/223f9dea3d8b46847c635a1ce864917e48da01ae.diff

LOG: [ELF] maybeCompress: replace vector<uint8_t> with unique_ptr<uint8_t[]>. NFC

And mention that it is zero-initialized. I do not notice a speed-up if
changed to be uninitialized by forcing the zero filler in writeTo.

Added: 
    

Modified: 
    lld/ELF/OutputSections.cpp

Removed: 
    


################################################################################
diff  --git a/lld/ELF/OutputSections.cpp b/lld/ELF/OutputSections.cpp
index cffde5d61ac91..f01f74f44d22c 100644
--- a/lld/ELF/OutputSections.cpp
+++ b/lld/ELF/OutputSections.cpp
@@ -337,9 +337,9 @@ template <class ELFT> void OutputSection::maybeCompress() {
   hdr->ch_size = size;
   hdr->ch_addralign = alignment;
 
-  // Write section contents to a temporary buffer and compress it.
-  std::vector<uint8_t> buf(size);
-  writeTo<ELFT>(buf.data());
+  // Write uncompressed data to a temporary zero-initialized buffer.
+  auto buf = std::make_unique<uint8_t[]>(size);
+  writeTo<ELFT>(buf.get());
   // We chose 1 as the default compression level because it is the fastest. If
   // -O2 is given, we use level 6 to compress debug info more by ~15%. We found
   // that level 7 to 9 doesn't make much 
diff erence (~1% more compression) while
@@ -350,9 +350,9 @@ template <class ELFT> void OutputSection::maybeCompress() {
   constexpr size_t shardSize = 1 << 20;
   const size_t numShards = (size + shardSize - 1) / shardSize;
   auto shardsIn = std::make_unique<ArrayRef<uint8_t>[]>(numShards);
-  for (size_t i = 0, start = 0, end; start != buf.size(); ++i, start = end) {
-    end = std::min(start + shardSize, buf.size());
-    shardsIn[i] = makeArrayRef<uint8_t>(buf.data() + start, end - start);
+  for (size_t i = 0, start = 0, end; start != size; ++i, start = end) {
+    end = std::min(start + shardSize, size);
+    shardsIn[i] = makeArrayRef<uint8_t>(buf.get() + start, end - start);
   }
 
   // Compress shards and compute Alder-32 checksums. Use Z_SYNC_FLUSH for all


        


More information about the llvm-commits mailing list