Skip to content

compress/flate: simplify sorting in huffman_code #66816

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 2 additions & 16 deletions src/cmd/compile/internal/test/inl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,6 @@ func TestIntendedInlining(t *testing.T) {
},
"internal/runtime/sys": {},
"compress/flate": {
"byLiteral.Len",
"byLiteral.Less",
"byLiteral.Swap",
"(*dictDecoder).tryWriteCopy",
},
"encoding/base64": {
Expand Down Expand Up @@ -282,13 +279,6 @@ func TestIntendedInlining(t *testing.T) {
}
}

// Functions that must actually be inlined; they must have actual callers.
must := map[string]bool{
"compress/flate.byLiteral.Len": true,
"compress/flate.byLiteral.Less": true,
"compress/flate.byLiteral.Swap": true,
}

notInlinedReason := make(map[string]string)
pkgs := make([]string, 0, len(want))
for pname, fnames := range want {
Expand Down Expand Up @@ -330,12 +320,8 @@ func TestIntendedInlining(t *testing.T) {
}
if m := canInline.FindStringSubmatch(line); m != nil {
fname := m[1]
fullname := curPkg + "." + fname
// If function must be inlined somewhere, being inlinable is not enough
if _, ok := must[fullname]; !ok {
delete(notInlinedReason, fullname)
continue
}
delete(notInlinedReason, curPkg+"."+fname)
continue
}
if m := cannotInline.FindStringSubmatch(line); m != nil {
fname, reason := m[1], m[2]
Expand Down
49 changes: 11 additions & 38 deletions src/compress/flate/huffman_code.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
package flate

import (
"cmp"
"math"
"math/bits"
"sort"
"slices"
)

// hcode is a huffman code with a bit code and bit length.
Expand All @@ -19,8 +20,6 @@ type huffmanEncoder struct {
codes []hcode
freqcache []literalNode
bitCount [17]int32
lns byLiteral // stored to avoid repeated allocation in generate
lfs byFreq // stored to avoid repeated allocation in generate
}

type literalNode struct {
Expand Down Expand Up @@ -256,7 +255,9 @@ func (h *huffmanEncoder) assignEncodingAndSize(bitCount []int32, list []literalN
// assigned in literal order (not frequency order).
chunk := list[len(list)-int(bits):]

h.lns.sort(chunk)
slices.SortFunc(chunk, func(a, b literalNode) int {
return cmp.Compare(a.literal, b.literal)
})
for _, node := range chunk {
h.codes[node.literal] = hcode{code: reverseBits(code, uint8(n)), len: uint16(n)}
code++
Expand Down Expand Up @@ -299,47 +300,19 @@ func (h *huffmanEncoder) generate(freq []int32, maxBits int32) {
}
return
}
h.lfs.sort(list)
slices.SortFunc(list, func(a, b literalNode) int {
if c := cmp.Compare(a.freq, b.freq); c != 0 {
return c
}
return cmp.Compare(a.literal, b.literal)
})

// Get the number of literals for each bit count
bitCount := h.bitCounts(list, maxBits)
// And do the assignment
h.assignEncodingAndSize(bitCount, list)
}

type byLiteral []literalNode

func (s *byLiteral) sort(a []literalNode) {
*s = byLiteral(a)
sort.Sort(s)
}

func (s byLiteral) Len() int { return len(s) }

func (s byLiteral) Less(i, j int) bool {
return s[i].literal < s[j].literal
}

func (s byLiteral) Swap(i, j int) { s[i], s[j] = s[j], s[i] }

type byFreq []literalNode

func (s *byFreq) sort(a []literalNode) {
*s = byFreq(a)
sort.Sort(s)
}

func (s byFreq) Len() int { return len(s) }

func (s byFreq) Less(i, j int) bool {
if s[i].freq == s[j].freq {
return s[i].literal < s[j].literal
}
return s[i].freq < s[j].freq
}

func (s byFreq) Swap(i, j int) { s[i], s[j] = s[j], s[i] }

func reverseBits(number uint16, bitLength byte) uint16 {
return bits.Reverse16(number << (16 - bitLength))
}