Fix excessive allocations if sortHuffmanTreeItems.

Fixes #11
This commit is contained in:
Andy Balholm 2020-05-04 15:04:20 -07:00
parent 9fc263f565
commit ac459dee64
2 changed files with 7 additions and 8 deletions

View File

@ -411,7 +411,7 @@ func buildAndStoreHuffmanTree(histogram []uint32, histogram_length uint, alphabe
} }
} }
func sortHuffmanTree1(v0 *huffmanTree, v1 *huffmanTree) bool { func sortHuffmanTree1(v0 huffmanTree, v1 huffmanTree) bool {
return v0.total_count_ < v1.total_count_ return v0.total_count_ < v1.total_count_
} }

View File

@ -24,7 +24,7 @@ func initHuffmanTree(self *huffmanTree, count uint32, left int16, right int16) {
} }
/* Input size optimized Shell sort. */ /* Input size optimized Shell sort. */
type huffmanTreeComparator func(*huffmanTree, *huffmanTree) bool type huffmanTreeComparator func(huffmanTree, huffmanTree) bool
var sortHuffmanTreeItems_gaps = []uint{132, 57, 23, 10, 4, 1} var sortHuffmanTreeItems_gaps = []uint{132, 57, 23, 10, 4, 1}
@ -36,14 +36,13 @@ func sortHuffmanTreeItems(items []huffmanTree, n uint, comparator huffmanTreeCom
var tmp huffmanTree = items[i] var tmp huffmanTree = items[i]
var k uint = i var k uint = i
var j uint = i - 1 var j uint = i - 1
for comparator(&tmp, &items[j]) { for comparator(tmp, items[j]) {
items[k] = items[j] items[k] = items[j]
k = j k = j
tmp10 := j if j == 0 {
j--
if tmp10 == 0 {
break break
} }
j--
} }
items[k] = tmp items[k] = tmp
@ -63,7 +62,7 @@ func sortHuffmanTreeItems(items []huffmanTree, n uint, comparator huffmanTreeCom
for i = gap; i < n; i++ { for i = gap; i < n; i++ {
var j uint = i var j uint = i
var tmp huffmanTree = items[i] var tmp huffmanTree = items[i]
for ; j >= gap && comparator(&tmp, &items[j-gap]); j -= gap { for ; j >= gap && comparator(tmp, items[j-gap]); j -= gap {
items[j] = items[j-gap] items[j] = items[j-gap]
} }
@ -105,7 +104,7 @@ func setDepth(p0 int, pool []huffmanTree, depth []byte, max_depth int) bool {
} }
/* Sort the root nodes, least popular first. */ /* Sort the root nodes, least popular first. */
func sortHuffmanTree(v0 *huffmanTree, v1 *huffmanTree) bool { func sortHuffmanTree(v0 huffmanTree, v1 huffmanTree) bool {
if v0.total_count_ != v1.total_count_ { if v0.total_count_ != v1.total_count_ {
return v0.total_count_ < v1.total_count_ return v0.total_count_ < v1.total_count_
} }