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,23 +67,41 @@ func TestEncoderEmptyWrite(t *testing.T) {
} }
func TestWriter(t *testing.T) { func TestWriter(t *testing.T) {
// Test basic encoder usage. for level := BestSpeed; level <= BestCompression; level++ {
input := []byte("<html><body><H1>Hello world</H1></body></html>") // Test basic encoder usage.
out := bytes.Buffer{} input := []byte("<html><body><H1>Hello world</H1></body></html>")
e := NewWriterOptions(&out, WriterOptions{Quality: 1}) out := bytes.Buffer{}
in := bytes.NewReader([]byte(input)) e := NewWriterOptions(&out, WriterOptions{Quality: level})
n, err := io.Copy(e, in) in := bytes.NewReader([]byte(input))
if err != nil { n, err := io.Copy(e, in)
t.Errorf("Copy Error: %v", err) if err != nil {
} t.Errorf("Copy Error: %v", err)
if int(n) != len(input) { }
t.Errorf("Copy() n=%v, want %v", n, len(input)) if int(n) != len(input) {
} t.Errorf("Copy() n=%v, want %v", n, len(input))
if err := e.Close(); err != nil { }
t.Errorf("Close Error after copied %d bytes: %v", n, err) if err := e.Close(); err != nil {
} t.Errorf("Close Error after copied %d bytes: %v", n, err)
if err := checkCompressedData(out.Bytes(), input); err != nil { }
t.Error(err) 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")
}
} }
} }
@ -119,24 +137,42 @@ func TestEncoderStreams(t *testing.T) {
} }
func TestEncoderLargeInput(t *testing.T) { func TestEncoderLargeInput(t *testing.T) {
input := make([]byte, 1000000) for level := BestSpeed; level <= BestCompression; level++ {
rand.Read(input) input := make([]byte, 1000000)
out := bytes.Buffer{} rand.Read(input)
e := NewWriterOptions(&out, WriterOptions{Quality: 5}) out := bytes.Buffer{}
in := bytes.NewReader(input) e := NewWriterOptions(&out, WriterOptions{Quality: level})
in := bytes.NewReader(input)
n, err := io.Copy(e, in) n, err := io.Copy(e, in)
if err != nil { if err != nil {
t.Errorf("Copy Error: %v", err) t.Errorf("Copy Error: %v", err)
} }
if int(n) != len(input) { if int(n) != len(input) {
t.Errorf("Copy() n=%v, want %v", n, len(input)) t.Errorf("Copy() n=%v, want %v", n, len(input))
} }
if err := e.Close(); err != nil { if err := e.Close(); err != nil {
t.Errorf("Close Error after copied %d bytes: %v", n, err) t.Errorf("Close Error after copied %d bytes: %v", n, err)
} }
if err := checkCompressedData(out.Bytes(), input); err != nil { if err := checkCompressedData(out.Bytes(), input); err != nil {
t.Error(err) 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")
}
} }
} }

View File

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

View File

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