[go] compress/flate: reuse huffman link table allocations on reinit

2 views
Skip to first unread message

Gavin Li (Gerrit)

unread,
May 20, 2026, 8:41:14 PMMay 20
to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

Gavin Li has uploaded the change for review

Commit message

compress/flate: reuse huffman link table allocations on reinit

huffmanDecoder.init runs repeatedly while inflating whenever a new
dynamic Huffman table is built, so reuse existing link slice capacity
instead of allocating new slices each time.

BenchmarkDecode results:

│ old.txt │ new.txt │
│ allocs/op │ allocs/op vs base │
Decode/Digits/Huffman/1e4-24 5.000 ± 0% 5.000 ± 0% ~ (p=1.000 n=50) ¹
Decode/Digits/Huffman/1e5-24 5.000 ± 0% 5.000 ± 0% ~ (p=1.000 n=50) ¹
Decode/Digits/Huffman/1e6-24 5.000 ± 0% 5.000 ± 0% ~ (p=1.000 n=50) ¹
Decode/Digits/Speed/1e4-24 5.000 ± 0% 5.000 ± 0% ~ (p=1.000 n=50) ¹
Decode/Digits/Speed/1e5-24 13.00 ± 0% 11.00 ± 0% -15.38% (n=50)
Decode/Digits/Speed/1e6-24 60.00 ± 0% 14.00 ± 0% -76.67% (n=50)
Decode/Digits/Default/1e4-24 8.000 ± 0% 8.000 ± 0% ~ (p=1.000 n=50) ¹
Decode/Digits/Default/1e5-24 10.00 ± 0% 10.00 ± 0% ~ (p=1.000 n=50) ¹
Decode/Digits/Default/1e6-24 16.00 ± 0% 13.00 ± 0% -18.75% (n=50)
Decode/Digits/Compression/1e4-24 7.000 ± 0% 7.000 ± 0% ~ (p=1.000 n=50) ¹
Decode/Digits/Compression/1e5-24 11.000 ± 0% 9.000 ± 0% -18.18% (n=50)
Decode/Digits/Compression/1e6-24 93.000 ± 0% 9.000 ± 0% -90.32% (n=50)
Decode/Newton/Huffman/1e4-24 14.00 ± 0% 14.00 ± 0% ~ (p=1.000 n=50) ¹
Decode/Newton/Huffman/1e5-24 25.00 ± 0% 15.00 ± 0% -40.00% (n=50)
Decode/Newton/Huffman/1e6-24 268.00 ± 0% 27.00 ± 0% -89.93% (n=50)
Decode/Newton/Speed/1e4-24 14.00 ± 0% 14.00 ± 0% ~ (p=1.000 n=50) ¹
Decode/Newton/Speed/1e5-24 18.00 ± 0% 18.00 ± 0% ~ (p=1.000 n=50) ¹
Decode/Newton/Speed/1e6-24 224.00 ± 0% 58.00 ± 0% -74.11% (n=50)
Decode/Newton/Default/1e4-24 18.00 ± 0% 18.00 ± 0% ~ (p=1.000 n=50) ¹
Decode/Newton/Default/1e5-24 29.00 ± 0% 18.00 ± 0% -37.93% (n=50)
Decode/Newton/Default/1e6-24 209.00 ± 0% 58.00 ± 0% -72.25% (n=50)
Decode/Newton/Compression/1e4-24 16.00 ± 0% 16.00 ± 0% ~ (p=1.000 n=50) ¹
Decode/Newton/Compression/1e5-24 19.00 ± 0% 19.00 ± 0% ~ (p=1.000 n=50) ¹
Decode/Newton/Compression/1e6-24 83.00 ± 0% 41.00 ± 0% -50.60% (n=50)
geomean 21.16 13.39 -36.73%
¹ all samples are equal
Change-Id: I185a2f881a23718fc64e7dfaa4ea79d698c842f9

Change diff

diff --git a/src/compress/flate/inflate.go b/src/compress/flate/inflate.go
index 4ed6aad..36eea2b 100644
--- a/src/compress/flate/inflate.go
+++ b/src/compress/flate/inflate.go
@@ -120,7 +120,9 @@
const sanity = false

if h.min != 0 {
+ links := h.links
*h = huffmanDecoder{}
+ h.links = links[:0] // try to use the allocation
}

// Count number of codes of each length,
@@ -175,7 +177,11 @@

// create link tables
link := nextcode[huffmanChunkBits+1] >> 1
- h.links = make([][]uint32, huffmanNumChunks-link)
+ if n := huffmanNumChunks - link; cap(h.links) >= n {
+ h.links = h.links[:n]
+ } else {
+ h.links = make([][]uint32, n)
+ }
for j := uint(link); j < huffmanNumChunks; j++ {
reverse := int(bits.Reverse16(uint16(j)))
reverse >>= uint(16 - huffmanChunkBits)
@@ -184,7 +190,12 @@
panic("impossible: overwriting existing chunk")
}
h.chunks[reverse] = uint32(off<<huffmanValueShift | (huffmanChunkBits + 1))
- h.links[off] = make([]uint32, numLinks)
+ if cap(h.links[off]) >= numLinks {
+ h.links[off] = h.links[off][:numLinks]
+ clear(h.links[off])
+ } else {
+ h.links[off] = make([]uint32, numLinks)
+ }
}
}

Change information

Files:
  • M src/compress/flate/inflate.go
Change size: S
Delta: 1 file changed, 13 insertions(+), 2 deletions(-)
Open in Gerrit

Related details

Attention set is empty
Submit Requirements:
  • requirement is not satisfiedCode-Review
  • requirement satisfiedNo-Unresolved-Comments
  • requirement is not satisfiedReview-Enforcement
  • requirement is not satisfiedTryBots-Pass
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
Gerrit-MessageType: newchange
Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I185a2f881a23718fc64e7dfaa4ea79d698c842f9
Gerrit-Change-Number: 780880
Gerrit-PatchSet: 1
Gerrit-Owner: Gavin Li <gfl...@gmail.com>
unsatisfied_requirement
satisfied_requirement
open
diffy

Gavin Li (Gerrit)

unread,
May 20, 2026, 8:42:00 PMMay 20
to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

Gavin Li uploaded new patchset

Gavin Li uploaded patch set #2 to this change.
Open in Gerrit

Related details

Attention set is empty
Submit Requirements:
  • requirement is not satisfiedCode-Review
  • requirement satisfiedNo-Unresolved-Comments
  • requirement is not satisfiedReview-Enforcement
  • requirement is not satisfiedTryBots-Pass
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
Gerrit-MessageType: newpatchset
Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I185a2f881a23718fc64e7dfaa4ea79d698c842f9
Gerrit-Change-Number: 780880
Gerrit-PatchSet: 2
Gerrit-Owner: Gavin Li <gfl...@gmail.com>
unsatisfied_requirement
satisfied_requirement
open
diffy

Sean Liao (Gerrit)

unread,
6:01 PM (5 hours ago) 6:01 PM
to Gavin Li, goph...@pubsubhelper.golang.org, Joseph Tsai, Matthew Dempsky, Gopher Robot, golang-co...@googlegroups.com
Attention needed from Gavin Li and Joseph Tsai

Sean Liao voted Commit-Queue+1

Commit-Queue+1
Open in Gerrit

Related details

Attention is currently required from:
  • Gavin Li
  • Joseph Tsai
Submit Requirements:
  • requirement is not satisfiedCode-Review
  • requirement satisfiedNo-Unresolved-Comments
  • requirement is not satisfiedReview-Enforcement
  • requirement is not satisfiedTryBots-Pass
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
Gerrit-MessageType: comment
Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I185a2f881a23718fc64e7dfaa4ea79d698c842f9
Gerrit-Change-Number: 780880
Gerrit-PatchSet: 2
Gerrit-Owner: Gavin Li <gfl...@gmail.com>
Gerrit-Reviewer: Joseph Tsai <joe...@digital-static.net>
Gerrit-Reviewer: Sean Liao <se...@liao.dev>
Gerrit-CC: Gopher Robot <go...@golang.org>
Gerrit-CC: Matthew Dempsky <mat...@go.dev>
Gerrit-Attention: Gavin Li <gfl...@gmail.com>
Gerrit-Attention: Joseph Tsai <joe...@digital-static.net>
Gerrit-Comment-Date: Fri, 17 Jul 2026 22:01:11 +0000
Gerrit-HasComments: No
Gerrit-Has-Labels: Yes
unsatisfied_requirement
satisfied_requirement
open
diffy

Sean Liao (Gerrit)

unread,
6:56 PM (4 hours ago) 6:56 PM
to Gavin Li, goph...@pubsubhelper.golang.org, golang...@luci-project-accounts.iam.gserviceaccount.com, Joseph Tsai, Matthew Dempsky, Gopher Robot, golang-co...@googlegroups.com
Attention needed from Gavin Li and Joseph Tsai

Sean Liao voted Commit-Queue+1

Commit-Queue+1
Open in Gerrit

Related details

Attention is currently required from:
  • Gavin Li
  • Joseph Tsai
Submit Requirements:
  • requirement is not satisfiedCode-Review
  • requirement satisfiedNo-Unresolved-Comments
  • requirement is not satisfiedReview-Enforcement
  • requirement is not satisfiedTryBots-Pass
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
Gerrit-MessageType: comment
Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I185a2f881a23718fc64e7dfaa4ea79d698c842f9
Gerrit-Change-Number: 780880
Gerrit-PatchSet: 3
Gerrit-Owner: Gavin Li <gfl...@gmail.com>
Gerrit-Reviewer: Joseph Tsai <joe...@digital-static.net>
Gerrit-Reviewer: Sean Liao <se...@liao.dev>
Gerrit-CC: Gopher Robot <go...@golang.org>
Gerrit-CC: Matthew Dempsky <mat...@go.dev>
Gerrit-Attention: Gavin Li <gfl...@gmail.com>
Gerrit-Attention: Joseph Tsai <joe...@digital-static.net>
Gerrit-Comment-Date: Fri, 17 Jul 2026 22:55:58 +0000
Gerrit-HasComments: No
Gerrit-Has-Labels: Yes
unsatisfied_requirement
satisfied_requirement
open
diffy
Reply all
Reply to author
Forward
0 new messages