From cf55fb3041b3806a22940bb9779a9204262590d8 Mon Sep 17 00:00:00 2001 From: greatroar <61184462+greatroar@users.noreply.github.com> Date: Fri, 20 Jan 2023 09:59:01 +0100 Subject: [PATCH] zstd: Handle dicts by pointer, always dict structures were mostly handled by pointer, except in one place. Since this doesn't make them immutable (they contain pointers to other structs), just copy pointers always to save some memory. --- zstd/decoder.go | 7 +++---- zstd/decoder_options.go | 6 +++--- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/zstd/decoder.go b/zstd/decoder.go index 2aeb953ca4..7113e69ee3 100644 --- a/zstd/decoder.go +++ b/zstd/decoder.go @@ -40,8 +40,7 @@ type Decoder struct { frame *frameDec // Custom dictionaries. - // Always uses copies. - dicts map[uint32]dict + dicts map[uint32]*dict // streamWg is the waitgroup for all streams streamWg sync.WaitGroup @@ -103,7 +102,7 @@ func NewReader(r io.Reader, opts ...DOption) (*Decoder, error) { } // Transfer option dicts. - d.dicts = make(map[uint32]dict, len(d.o.dicts)) + d.dicts = make(map[uint32]*dict, len(d.o.dicts)) for _, dc := range d.o.dicts { d.dicts[dc.id] = dc } @@ -942,7 +941,7 @@ func (d *Decoder) setDict(frame *frameDec) (err error) { if debugDecoder { println("setting dict", frame.DictionaryID) } - frame.history.setDict(&dict) + frame.history.setDict(dict) } else if frame.DictionaryID != 0 { // A zero or missing dictionary id is ambiguous: // either dictionary zero, or no dictionary. In particular, diff --git a/zstd/decoder_options.go b/zstd/decoder_options.go index c36853d660..07a90dd7af 100644 --- a/zstd/decoder_options.go +++ b/zstd/decoder_options.go @@ -20,7 +20,7 @@ type decoderOptions struct { concurrent int maxDecodedSize uint64 maxWindowSize uint64 - dicts []dict + dicts []*dict ignoreChecksum bool limitToCap bool decodeBufsBelow int @@ -101,7 +101,7 @@ func WithDecoderDicts(dicts ...[]byte) DOption { if err != nil { return err } - o.dicts = append(o.dicts, *d) + o.dicts = append(o.dicts, d) } return nil } @@ -114,7 +114,7 @@ func WithDecoderDictRaw(id uint32, content []byte) DOption { if bits.UintSize > 32 && uint(len(content)) > dictMaxLength { return fmt.Errorf("dictionary of size %d > 2GiB too large", len(content)) } - o.dicts = append(o.dicts, dict{id: id, content: content, offsets: [3]int{1, 4, 8}}) + o.dicts = append(o.dicts, &dict{id: id, content: content, offsets: [3]int{1, 4, 8}}) return nil } }