From ac459dee64f90b971e607d1838b66f8939c95e4c Mon Sep 17 00:00:00 2001 From: Andy Balholm Date: Mon, 4 May 2020 15:04:20 -0700 Subject: [PATCH] Fix excessive allocations if sortHuffmanTreeItems. Fixes #11 --- brotli_bit_stream.go | 2 +- entropy_encode.go | 13 ++++++------- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/brotli_bit_stream.go b/brotli_bit_stream.go index 395f604..23d63f1 100644 --- a/brotli_bit_stream.go +++ b/brotli_bit_stream.go @@ -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_ } diff --git a/entropy_encode.go b/entropy_encode.go index d0c1dca..3f469a3 100644 --- a/entropy_encode.go +++ b/entropy_encode.go @@ -24,7 +24,7 @@ func initHuffmanTree(self *huffmanTree, count uint32, left int16, right int16) { } /* 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} @@ -36,14 +36,13 @@ func sortHuffmanTreeItems(items []huffmanTree, n uint, comparator huffmanTreeCom var tmp huffmanTree = items[i] var k uint = i var j uint = i - 1 - for comparator(&tmp, &items[j]) { + for comparator(tmp, items[j]) { items[k] = items[j] k = j - tmp10 := j - j-- - if tmp10 == 0 { + if j == 0 { break } + j-- } items[k] = tmp @@ -63,7 +62,7 @@ func sortHuffmanTreeItems(items []huffmanTree, n uint, comparator huffmanTreeCom for i = gap; i < n; i++ { var j uint = 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] } @@ -105,7 +104,7 @@ func setDepth(p0 int, pool []huffmanTree, depth []byte, max_depth int) bool { } /* 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_ { return v0.total_count_ < v1.total_count_ }