diff --git a/snappy-internal.h b/snappy-internal.h index 4b53d59..1e1c307 100644 --- a/snappy-internal.h +++ b/snappy-internal.h @@ -36,19 +36,26 @@ namespace snappy { namespace internal { +// Working memory performs a single allocation to hold all scratch space +// required for compression. class WorkingMemory { public: - WorkingMemory() : large_table_(NULL) { } - ~WorkingMemory() { delete[] large_table_; } + explicit WorkingMemory(size_t input_size); + ~WorkingMemory(); // Allocates and clears a hash table using memory in "*this", // stores the number of buckets in "*table_size" and returns a pointer to // the base of the hash table. - uint16* GetHashTable(size_t input_size, int* table_size); + uint16* GetHashTable(size_t fragment_size, int* table_size) const; + char* GetScratchInput() const { return input_; } + char* GetScratchOutput() const { return output_; } private: - uint16 small_table_[1<<10]; // 2KB - uint16* large_table_; // Allocated only when needed + char* mem_; // the allocated memory, never nullptr + size_t size_; // the size of the allocated memory, never 0 + uint16* table_; // the pointer to the hashtable + char* input_; // the pointer to the input scratch buffer + char* output_; // the pointer to the output scratch buffer // No copying WorkingMemory(const WorkingMemory&); diff --git a/snappy.cc b/snappy.cc index afb9f6f..34a6346 100644 --- a/snappy.cc +++ b/snappy.cc @@ -418,31 +418,41 @@ bool GetUncompressedLength(const char* start, size_t n, size_t* result) { } } -namespace internal { -uint16* WorkingMemory::GetHashTable(size_t input_size, int* table_size) { - // Use smaller hash table when input.size() is smaller, since we - // fill the table, incurring O(hash table size) overhead for - // compression, and if the input is short, we won't need that - // many hash table entries anyway. +namespace { +uint32 CalculateTableSize(uint32 input_size) { assert(kMaxHashTableSize >= 256); - size_t htsize = 256; - while (htsize < kMaxHashTableSize && htsize < input_size) { - htsize <<= 1; + if (input_size > kMaxHashTableSize) { + return kMaxHashTableSize; } - - uint16* table; - if (htsize <= ARRAYSIZE(small_table_)) { - table = small_table_; - } else { - if (large_table_ == NULL) { - large_table_ = new uint16[kMaxHashTableSize]; - } - table = large_table_; + if (input_size < 256) { + return 256; } + return 1u << (32 - __builtin_clz(input_size - 1)); +} +} // namespace +namespace internal { +WorkingMemory::WorkingMemory(size_t input_size) { + const size_t max_fragment_size = std::min(input_size, kBlockSize); + const size_t table_size = CalculateTableSize(max_fragment_size); + size_ = table_size * sizeof(*table_) + max_fragment_size + + MaxCompressedLength(max_fragment_size); + mem_ = std::allocator().allocate(size_); + table_ = reinterpret_cast(mem_); + input_ = mem_ + table_size * sizeof(*table_); + output_ = input_ + max_fragment_size; +} + +WorkingMemory::~WorkingMemory() { + std::allocator().deallocate(mem_, size_); +} + +uint16* WorkingMemory::GetHashTable(size_t fragment_size, + int* table_size) const { + const size_t htsize = CalculateTableSize(fragment_size); + memset(table_, 0, htsize * sizeof(*table_)); *table_size = htsize; - memset(table, 0, htsize * sizeof(*table)); - return table; + return table_; } } // end namespace internal @@ -942,17 +952,6 @@ bool GetUncompressedLength(Source* source, uint32* result) { return decompressor.ReadUncompressedLength(result); } -struct Deleter { - Deleter() : size_(0) {} - explicit Deleter(size_t size) : size_(size) {} - - void operator()(char* ptr) const { - std::allocator().deallocate(ptr, size_); - } - - size_t size_; -}; - size_t Compress(Source* reader, Sink* writer) { size_t written = 0; size_t N = reader->Available(); @@ -962,9 +961,7 @@ size_t Compress(Source* reader, Sink* writer) { writer->Append(ulength, p-ulength); written += (p - ulength); - internal::WorkingMemory wmem; - std::unique_ptr scratch; - std::unique_ptr scratch_output; + internal::WorkingMemory wmem(N); while (N > 0) { // Get next block to compress (without copying if possible) @@ -980,26 +977,19 @@ size_t Compress(Source* reader, Sink* writer) { pending_advance = num_to_read; fragment_size = num_to_read; } else { - // Read into scratch buffer - if (scratch == NULL) { - // If this is the last iteration, we want to allocate N bytes - // of space, otherwise the max possible kBlockSize space. - // num_to_read contains exactly the correct value - scratch = { - std::allocator().allocate(num_to_read), Deleter(num_to_read)}; - } - memcpy(scratch.get(), fragment, bytes_read); + char* scratch = wmem.GetScratchInput(); + memcpy(scratch, fragment, bytes_read); reader->Skip(bytes_read); while (bytes_read < num_to_read) { fragment = reader->Peek(&fragment_size); size_t n = std::min(fragment_size, num_to_read - bytes_read); - memcpy(scratch.get() + bytes_read, fragment, n); + memcpy(scratch + bytes_read, fragment, n); bytes_read += n; reader->Skip(n); } assert(bytes_read == num_to_read); - fragment = scratch.get(); + fragment = scratch; fragment_size = num_to_read; } assert(fragment_size == num_to_read); @@ -1013,17 +1003,13 @@ size_t Compress(Source* reader, Sink* writer) { // Need a scratch buffer for the output, in case the byte sink doesn't // have room for us directly. - if (scratch_output == NULL) { - scratch_output = - {std::allocator().allocate(max_output), Deleter(max_output)}; - } else { - // Since we encode kBlockSize regions followed by a region - // which is <= kBlockSize in length, a previously allocated - // scratch_output[] region is big enough for this iteration. - } - char* dest = writer->GetAppendBuffer(max_output, scratch_output.get()); - char* end = internal::CompressFragment(fragment, fragment_size, - dest, table, table_size); + + // Since we encode kBlockSize regions followed by a region + // which is <= kBlockSize in length, a previously allocated + // scratch_output[] region is big enough for this iteration. + char* dest = writer->GetAppendBuffer(max_output, wmem.GetScratchOutput()); + char* end = internal::CompressFragment(fragment, fragment_size, dest, table, + table_size); writer->Append(dest, end - dest); written += (end - dest); diff --git a/snappy_unittest.cc b/snappy_unittest.cc index 0548d2a..67535ca 100644 --- a/snappy_unittest.cc +++ b/snappy_unittest.cc @@ -445,7 +445,7 @@ static void VerifyNonBlockedCompression(const string& input) { Varint::Append32(&prefix, input.size()); // Setup compression table - snappy::internal::WorkingMemory wmem; + snappy::internal::WorkingMemory wmem(input.size()); int table_size; uint16* table = wmem.GetHashTable(input.size(), &table_size);