[llvm] r205676 - Simplify compression API by decompressing into a SmallVector rather than a MemoryBuffer

David Blaikie dblaikie at gmail.com
Sat Apr 5 14:26:45 PDT 2014


Author: dblaikie
Date: Sat Apr  5 16:26:44 2014
New Revision: 205676

URL: http://llvm.org/viewvc/llvm-project?rev=205676&view=rev
Log:
Simplify compression API by decompressing into a SmallVector rather than a MemoryBuffer

This avoids an extra copy during decompression and avoids the use of
MemoryBuffer which is a weirdly esoteric device that includes unrelated
concepts like "file name" (its rather generic name is a bit misleading).

Similar refactoring of zlib::compress coming up.

Modified:
    llvm/trunk/include/llvm/Support/Compression.h
    llvm/trunk/lib/DebugInfo/DWARFContext.cpp
    llvm/trunk/lib/DebugInfo/DWARFContext.h
    llvm/trunk/lib/Support/Compression.cpp
    llvm/trunk/unittests/Support/CompressionTest.cpp

Modified: llvm/trunk/include/llvm/Support/Compression.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/Compression.h?rev=205676&r1=205675&r2=205676&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Support/Compression.h (original)
+++ llvm/trunk/include/llvm/Support/Compression.h Sat Apr  5 16:26:44 2014
@@ -16,6 +16,7 @@
 
 #include "llvm/Support/DataTypes.h"
 #include <memory>
+#include "llvm/ADT/SmallVector.h"
 
 namespace llvm {
 
@@ -47,7 +48,7 @@ Status compress(StringRef InputBuffer,
                 CompressionLevel Level = DefaultCompression);
 
 Status uncompress(StringRef InputBuffer,
-                  std::unique_ptr<MemoryBuffer> &UncompressedBuffer,
+                  SmallVectorImpl<char> &UncompressedBuffer,
                   size_t UncompressedSize);
 
 uint32_t crc32(StringRef Buffer);

Modified: llvm/trunk/lib/DebugInfo/DWARFContext.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/DebugInfo/DWARFContext.cpp?rev=205676&r1=205675&r2=205676&view=diff
==============================================================================
--- llvm/trunk/lib/DebugInfo/DWARFContext.cpp (original)
+++ llvm/trunk/lib/DebugInfo/DWARFContext.cpp Sat Apr  5 16:26:44 2014
@@ -637,14 +637,15 @@ DWARFContextInMemory::DWARFContextInMemo
       if (!zlib::isAvailable() ||
           !consumeCompressedDebugSectionHeader(data, OriginalSize))
         continue;
-      std::unique_ptr<MemoryBuffer> UncompressedSection;
-      if (zlib::uncompress(data, UncompressedSection, OriginalSize) !=
-          zlib::StatusOK)
+      UncompressedSections.resize(UncompressedSections.size() + 1);
+      if (zlib::uncompress(data, UncompressedSections.back(), OriginalSize) !=
+          zlib::StatusOK) {
+        UncompressedSections.pop_back();
         continue;
+      }
       // Make data point to uncompressed section contents and save its contents.
       name = name.substr(1);
-      data = UncompressedSection->getBuffer();
-      UncompressedSections.push_back(std::move(UncompressedSection));
+      data = UncompressedSections.back();
     }
 
     StringRef *SectionData =

Modified: llvm/trunk/lib/DebugInfo/DWARFContext.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/DebugInfo/DWARFContext.h?rev=205676&r1=205675&r2=205676&view=diff
==============================================================================
--- llvm/trunk/lib/DebugInfo/DWARFContext.h (original)
+++ llvm/trunk/lib/DebugInfo/DWARFContext.h Sat Apr  5 16:26:44 2014
@@ -242,7 +242,7 @@ class DWARFContextInMemory : public DWAR
   StringRef RangeDWOSection;
   StringRef AddrSection;
 
-  SmallVector<std::unique_ptr<MemoryBuffer>, 4> UncompressedSections;
+  SmallVector<SmallString<32>, 4> UncompressedSections;
 
 public:
   DWARFContextInMemory(object::ObjectFile *);

Modified: llvm/trunk/lib/Support/Compression.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/Compression.cpp?rev=205676&r1=205675&r2=205676&view=diff
==============================================================================
--- llvm/trunk/lib/Support/Compression.cpp (original)
+++ llvm/trunk/lib/Support/Compression.cpp Sat Apr  5 16:26:44 2014
@@ -65,18 +65,13 @@ zlib::Status zlib::compress(StringRef In
 }
 
 zlib::Status zlib::uncompress(StringRef InputBuffer,
-                              std::unique_ptr<MemoryBuffer> &UncompressedBuffer,
+                              SmallVectorImpl<char> &UncompressedBuffer,
                               size_t UncompressedSize) {
-  std::unique_ptr<char[]> TmpBuffer(new char[UncompressedSize]);
-  Status Res = encodeZlibReturnValue(
-      ::uncompress((Bytef *)TmpBuffer.get(), (uLongf *)&UncompressedSize,
-                   (const Bytef *)InputBuffer.data(), InputBuffer.size()));
-  if (Res == StatusOK) {
-    UncompressedBuffer.reset(MemoryBuffer::getMemBufferCopy(
-        StringRef(TmpBuffer.get(), UncompressedSize)));
-    // Tell MSan that memory initialized by zlib is valid.
-    __msan_unpoison(UncompressedBuffer->getBufferStart(), UncompressedSize);
-  }
+  UncompressedBuffer.resize(UncompressedSize);
+  Status Res = encodeZlibReturnValue(::uncompress(
+      (Bytef *)UncompressedBuffer.data(), (uLongf *)&UncompressedSize,
+      (const Bytef *)InputBuffer.data(), InputBuffer.size()));
+  UncompressedBuffer.resize(UncompressedSize);
   return Res;
 }
 

Modified: llvm/trunk/unittests/Support/CompressionTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/Support/CompressionTest.cpp?rev=205676&r1=205675&r2=205676&view=diff
==============================================================================
--- llvm/trunk/unittests/Support/CompressionTest.cpp (original)
+++ llvm/trunk/unittests/Support/CompressionTest.cpp Sat Apr  5 16:26:44 2014
@@ -25,14 +25,13 @@ namespace {
 
 void TestZlibCompression(StringRef Input, zlib::CompressionLevel Level) {
   std::unique_ptr<MemoryBuffer> Compressed;
-  std::unique_ptr<MemoryBuffer> Uncompressed;
+  SmallString<32> Uncompressed;
   EXPECT_EQ(zlib::StatusOK, zlib::compress(Input, Compressed, Level));
   // Check that uncompressed buffer is the same as original.
   EXPECT_EQ(zlib::StatusOK, zlib::uncompress(Compressed->getBuffer(),
                                              Uncompressed, Input.size()));
-  EXPECT_EQ(Input.size(), Uncompressed->getBufferSize());
-  EXPECT_EQ(0,
-            memcmp(Input.data(), Uncompressed->getBufferStart(), Input.size()));
+  EXPECT_EQ(Input.size(), Uncompressed.size());
+  EXPECT_EQ(0, memcmp(Input.data(), Uncompressed.data(), Input.size()));
   if (Input.size() > 0) {
     // Uncompression fails if expected length is too short.
     EXPECT_EQ(zlib::StatusBufferTooShort,





More information about the llvm-commits mailing list