Skip to content

Commit

Permalink
zstd: Handle dicts by pointer, always (#743)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
greatroar committed Jan 20, 2023
1 parent 7a74de4 commit 5f40643
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 7 deletions.
7 changes: 3 additions & 4 deletions zstd/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions zstd/decoder_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type decoderOptions struct {
concurrent int
maxDecodedSize uint64
maxWindowSize uint64
dicts []dict
dicts []*dict
ignoreChecksum bool
limitToCap bool
decodeBufsBelow int
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
}
Expand Down

0 comments on commit 5f40643

Please sign in to comment.