Skip to content

Commit

Permalink
inflate: Reduce memory use (#178)
Browse files Browse the repository at this point in the history
Use uint16 for chunks. Not 100% sure this is safe, so testing a bit.

No measurable performance difference.
  • Loading branch information
klauspost committed Nov 11, 2019
1 parent 64d2747 commit bc6fcd8
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions flate/inflate.go
Expand Up @@ -107,8 +107,8 @@ const (

type huffmanDecoder struct {
min int // the minimum code length
chunks *[huffmanNumChunks]uint32 // chunks as described above
links [][]uint32 // overflow links
chunks *[huffmanNumChunks]uint16 // chunks as described above
links [][]uint16 // overflow links
linkMask uint32 // mask the width of the link table
}

Expand All @@ -124,7 +124,7 @@ func (h *huffmanDecoder) init(lengths []int) bool {
const sanity = false

if h.chunks == nil {
h.chunks = &[huffmanNumChunks]uint32{}
h.chunks = &[huffmanNumChunks]uint16{}
}
if h.min != 0 {
*h = huffmanDecoder{chunks: h.chunks, links: h.links}
Expand Down Expand Up @@ -191,7 +191,7 @@ func (h *huffmanDecoder) init(lengths []int) bool {
// create link tables
link := nextcode[huffmanChunkBits+1] >> 1
if cap(h.links) < huffmanNumChunks-link {
h.links = make([][]uint32, huffmanNumChunks-link)
h.links = make([][]uint16, huffmanNumChunks-link)
} else {
h.links = h.links[:huffmanNumChunks-link]
}
Expand All @@ -202,9 +202,9 @@ func (h *huffmanDecoder) init(lengths []int) bool {
if sanity && h.chunks[reverse] != 0 {
panic("impossible: overwriting existing chunk")
}
h.chunks[reverse] = uint32(off<<huffmanValueShift | (huffmanChunkBits + 1))
h.chunks[reverse] = uint16(off<<huffmanValueShift | (huffmanChunkBits + 1))
if cap(h.links[off]) < numLinks {
h.links[off] = make([]uint32, numLinks)
h.links[off] = make([]uint16, numLinks)
} else {
links := h.links[off][:0]
h.links[off] = links[:numLinks]
Expand All @@ -220,7 +220,7 @@ func (h *huffmanDecoder) init(lengths []int) bool {
}
code := nextcode[n]
nextcode[n]++
chunk := uint32(i<<huffmanValueShift | n)
chunk := uint16(i<<huffmanValueShift | n)
reverse := int(bits.Reverse16(uint16(code)))
reverse >>= uint(16 - n)
if n <= huffmanChunkBits {
Expand Down

0 comments on commit bc6fcd8

Please sign in to comment.