Reuse more memory when a Writer is Reset.

This commit is contained in:
Andy Balholm 2020-05-05 17:18:33 -07:00
parent 00ca370ce2
commit cb9be97eb7
3 changed files with 73 additions and 44 deletions

View File

@ -67,10 +67,11 @@ func TestEncoderEmptyWrite(t *testing.T) {
}
func TestWriter(t *testing.T) {
for level := BestSpeed; level <= BestCompression; level++ {
// Test basic encoder usage.
input := []byte("<html><body><H1>Hello world</H1></body></html>")
out := bytes.Buffer{}
e := NewWriterOptions(&out, WriterOptions{Quality: 1})
e := NewWriterOptions(&out, WriterOptions{Quality: level})
in := bytes.NewReader([]byte(input))
n, err := io.Copy(e, in)
if err != nil {
@ -85,6 +86,23 @@ func TestWriter(t *testing.T) {
if err := checkCompressedData(out.Bytes(), input); err != nil {
t.Error(err)
}
out2 := bytes.Buffer{}
e.Reset(&out2)
n2, err := e.Write(input)
if err != nil {
t.Errorf("Write error after Reset: %v", err)
}
if n2 != len(input) {
t.Errorf("Write() after Reset n=%d, want %d", n2, len(input))
}
if err := e.Close(); err != nil {
t.Errorf("Close error after Reset (copied %d) bytes: %v", n2, err)
}
if !bytes.Equal(out.Bytes(), out2.Bytes()) {
t.Error("Compressed data after Reset doesn't equal first time")
}
}
}
func TestEncoderStreams(t *testing.T) {
@ -119,10 +137,11 @@ func TestEncoderStreams(t *testing.T) {
}
func TestEncoderLargeInput(t *testing.T) {
for level := BestSpeed; level <= BestCompression; level++ {
input := make([]byte, 1000000)
rand.Read(input)
out := bytes.Buffer{}
e := NewWriterOptions(&out, WriterOptions{Quality: 5})
e := NewWriterOptions(&out, WriterOptions{Quality: level})
in := bytes.NewReader(input)
n, err := io.Copy(e, in)
@ -138,6 +157,23 @@ func TestEncoderLargeInput(t *testing.T) {
if err := checkCompressedData(out.Bytes(), input); err != nil {
t.Error(err)
}
out2 := bytes.Buffer{}
e.Reset(&out2)
n2, err := e.Write(input)
if err != nil {
t.Errorf("Write error after Reset: %v", err)
}
if n2 != len(input) {
t.Errorf("Write() after Reset n=%d, want %d", n2, len(input))
}
if err := e.Close(); err != nil {
t.Errorf("Close error after Reset (copied %d) bytes: %v", n2, err)
}
if !bytes.Equal(out.Bytes(), out2.Bytes()) {
t.Error("Compressed data after Reset doesn't equal first time")
}
}
}
func TestEncoderFlush(t *testing.T) {

View File

@ -1088,14 +1088,10 @@ func encoderInitState(s *Writer) {
s.last_processed_pos_ = 0
s.prev_byte_ = 0
s.prev_byte2_ = 0
s.storage_size_ = 0
s.storage_ = nil
s.hasher_ = nil
s.large_table_ = nil
s.large_table_size_ = 0
if s.hasher_ != nil {
s.hasher_.Common().is_prepared_ = false
}
s.cmd_code_numbits_ = 0
s.command_buf_ = nil
s.literal_buf_ = nil
s.next_out_ = nil
s.available_out_ = 0
s.total_out_ = 0

View File

@ -27,10 +27,7 @@ type ringBuffer struct {
}
func ringBufferInit(rb *ringBuffer) {
rb.cur_size_ = 0
rb.pos_ = 0
rb.data_ = nil
rb.buffer_ = nil
}
func ringBufferSetup(params *encoderParams, rb *ringBuffer) {