Skip to content

Commit 71b73c9

Browse files
committed
compress/flate: simplify sorting in huffman_code
Replace custom sort implementations with slices.SortFunc and cmp.Compare for clarity and simplicity. This change removes the previously defined byLiteral and byFreq types and their associated methods, leveraging the standard library's generic functions to achieve the same functionality. The update makes use of "slices" and "cmp" packages for sorting, streamlining the codebase and improving readability.
1 parent 2b794ed commit 71b73c9

File tree

2 files changed

+13
-54
lines changed

2 files changed

+13
-54
lines changed

src/cmd/compile/internal/test/inl_test.go

+2-16
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,6 @@ func TestIntendedInlining(t *testing.T) {
114114
},
115115
"internal/runtime/sys": {},
116116
"compress/flate": {
117-
"byLiteral.Len",
118-
"byLiteral.Less",
119-
"byLiteral.Swap",
120117
"(*dictDecoder).tryWriteCopy",
121118
},
122119
"encoding/base64": {
@@ -282,13 +279,6 @@ func TestIntendedInlining(t *testing.T) {
282279
}
283280
}
284281

285-
// Functions that must actually be inlined; they must have actual callers.
286-
must := map[string]bool{
287-
"compress/flate.byLiteral.Len": true,
288-
"compress/flate.byLiteral.Less": true,
289-
"compress/flate.byLiteral.Swap": true,
290-
}
291-
292282
notInlinedReason := make(map[string]string)
293283
pkgs := make([]string, 0, len(want))
294284
for pname, fnames := range want {
@@ -330,12 +320,8 @@ func TestIntendedInlining(t *testing.T) {
330320
}
331321
if m := canInline.FindStringSubmatch(line); m != nil {
332322
fname := m[1]
333-
fullname := curPkg + "." + fname
334-
// If function must be inlined somewhere, being inlinable is not enough
335-
if _, ok := must[fullname]; !ok {
336-
delete(notInlinedReason, fullname)
337-
continue
338-
}
323+
delete(notInlinedReason, curPkg+"."+fname)
324+
continue
339325
}
340326
if m := cannotInline.FindStringSubmatch(line); m != nil {
341327
fname, reason := m[1], m[2]

src/compress/flate/huffman_code.go

+11-38
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55
package flate
66

77
import (
8+
"cmp"
89
"math"
910
"math/bits"
10-
"sort"
11+
"slices"
1112
)
1213

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

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

259-
h.lns.sort(chunk)
258+
slices.SortFunc(chunk, func(a, b literalNode) int {
259+
return cmp.Compare(a.literal, b.literal)
260+
})
260261
for _, node := range chunk {
261262
h.codes[node.literal] = hcode{code: reverseBits(code, uint8(n)), len: uint16(n)}
262263
code++
@@ -299,47 +300,19 @@ func (h *huffmanEncoder) generate(freq []int32, maxBits int32) {
299300
}
300301
return
301302
}
302-
h.lfs.sort(list)
303+
slices.SortFunc(list, func(a, b literalNode) int {
304+
if c := cmp.Compare(a.freq, b.freq); c != 0 {
305+
return c
306+
}
307+
return cmp.Compare(a.literal, b.literal)
308+
})
303309

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

310-
type byLiteral []literalNode
311-
312-
func (s *byLiteral) sort(a []literalNode) {
313-
*s = byLiteral(a)
314-
sort.Sort(s)
315-
}
316-
317-
func (s byLiteral) Len() int { return len(s) }
318-
319-
func (s byLiteral) Less(i, j int) bool {
320-
return s[i].literal < s[j].literal
321-
}
322-
323-
func (s byLiteral) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
324-
325-
type byFreq []literalNode
326-
327-
func (s *byFreq) sort(a []literalNode) {
328-
*s = byFreq(a)
329-
sort.Sort(s)
330-
}
331-
332-
func (s byFreq) Len() int { return len(s) }
333-
334-
func (s byFreq) Less(i, j int) bool {
335-
if s[i].freq == s[j].freq {
336-
return s[i].literal < s[j].literal
337-
}
338-
return s[i].freq < s[j].freq
339-
}
340-
341-
func (s byFreq) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
342-
343316
func reverseBits(number uint16, bitLength byte) uint16 {
344317
return bits.Reverse16(number << (16 - bitLength))
345318
}

0 commit comments

Comments
 (0)