From a505f2479e98e50999376e572f905d93428bb93d Mon Sep 17 00:00:00 2001 From: David Blaikie Date: Sat, 5 Apr 2014 21:26:44 +0000 Subject: [PATCH] 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. llvm-svn: 205676 --- llvm/include/llvm/Support/Compression.h | 3 ++- llvm/lib/DebugInfo/DWARFContext.cpp | 11 ++++++----- llvm/lib/DebugInfo/DWARFContext.h | 2 +- llvm/lib/Support/Compression.cpp | 17 ++++++----------- llvm/unittests/Support/CompressionTest.cpp | 7 +++---- 5 files changed, 18 insertions(+), 22 deletions(-) diff --git a/llvm/include/llvm/Support/Compression.h b/llvm/include/llvm/Support/Compression.h index 80eff5c6285a..262980928663 100644 --- a/llvm/include/llvm/Support/Compression.h +++ b/llvm/include/llvm/Support/Compression.h @@ -16,6 +16,7 @@ #include "llvm/Support/DataTypes.h" #include +#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 &UncompressedBuffer, + SmallVectorImpl &UncompressedBuffer, size_t UncompressedSize); uint32_t crc32(StringRef Buffer); diff --git a/llvm/lib/DebugInfo/DWARFContext.cpp b/llvm/lib/DebugInfo/DWARFContext.cpp index 60c5f6ab56d7..a287cf9edb2d 100644 --- a/llvm/lib/DebugInfo/DWARFContext.cpp +++ b/llvm/lib/DebugInfo/DWARFContext.cpp @@ -637,14 +637,15 @@ DWARFContextInMemory::DWARFContextInMemory(object::ObjectFile *Obj) if (!zlib::isAvailable() || !consumeCompressedDebugSectionHeader(data, OriginalSize)) continue; - std::unique_ptr 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 = diff --git a/llvm/lib/DebugInfo/DWARFContext.h b/llvm/lib/DebugInfo/DWARFContext.h index ad6841ae9e31..6d1ae921cec5 100644 --- a/llvm/lib/DebugInfo/DWARFContext.h +++ b/llvm/lib/DebugInfo/DWARFContext.h @@ -242,7 +242,7 @@ class DWARFContextInMemory : public DWARFContext { StringRef RangeDWOSection; StringRef AddrSection; - SmallVector, 4> UncompressedSections; + SmallVector, 4> UncompressedSections; public: DWARFContextInMemory(object::ObjectFile *); diff --git a/llvm/lib/Support/Compression.cpp b/llvm/lib/Support/Compression.cpp index 5e5336144ab1..329a402a0704 100644 --- a/llvm/lib/Support/Compression.cpp +++ b/llvm/lib/Support/Compression.cpp @@ -65,18 +65,13 @@ zlib::Status zlib::compress(StringRef InputBuffer, } zlib::Status zlib::uncompress(StringRef InputBuffer, - std::unique_ptr &UncompressedBuffer, + SmallVectorImpl &UncompressedBuffer, size_t UncompressedSize) { - std::unique_ptr 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; } diff --git a/llvm/unittests/Support/CompressionTest.cpp b/llvm/unittests/Support/CompressionTest.cpp index db6a8bb14638..30df0509cce0 100644 --- a/llvm/unittests/Support/CompressionTest.cpp +++ b/llvm/unittests/Support/CompressionTest.cpp @@ -25,14 +25,13 @@ namespace { void TestZlibCompression(StringRef Input, zlib::CompressionLevel Level) { std::unique_ptr Compressed; - std::unique_ptr 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,