ericmckean / snappy

Automatically exported from code.google.com/p/snappy
Other
0 stars 0 forks source link

Unnecessary memory allocation in snappy.cc:Compress #93

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
In the Compress function in snappy.cc, the scratch_output buffer is allocated 
once per Compress call, but it is never read or written to. This is because the 
only implementation of the Sink interface used by Compress is the 
UncheckedByteArraySink class which never uses the scratch buffer argument in 
GetAppendBuffer.

--- snappy.cc --- Line 943 ---
    // Need a scratch buffer for the output, in case the byte sink doesn't
    // have room for us directly.
    if (scratch_output == NULL) {
!=== ALLOCATED HERE ===!
======>      scratch_output = new char[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.
    }
!==== IGNORED HERE ====!
======>    char* dest = writer->GetAppendBuffer(max_output, scratch_output);
------------------------------

What version of the product are you using? On what operating system?
Snappy 1.1.2, Windows 8.1

Original issue reported on code.google.com by mark.ben...@10gen.com on 20 Feb 2015 at 12:30

GoogleCodeExporter commented 9 years ago
Copying Sanjay's answer, which came by email and thus was not included in the 
bug:

“People are allowed to provide their own implementations of Sink where they 
may use the scratch_output buffer. There are libraries out there that use 
snappy and do so.”

Original comment by se...@google.com on 24 Feb 2015 at 2:13