[go] internal/zstd: refactor huffman symbol decoding

0 views
Skip to first unread message

Garrick Gross (Gerrit)

unread,
Dec 28, 2025, 8:51:03 PM (7 hours ago) Dec 28
to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

Garrick Gross has uploaded the change for review

Commit message

internal/zstd: refactor huffman symbol decoding

Refactor duplicated decoding logic in readLiteralsOneStream and readLiteralsFourStreams into a shared decodeSymbol helper. No behavior changes

all.bash tests pass successfully after change.

Fixes #64031
Change-Id: Id26eb64de41b3fa2ab1d021b5905e50d524fb231

Change diff

diff --git a/src/internal/zstd/literals.go b/src/internal/zstd/literals.go
index 11ef859..af2ad5d 100644
--- a/src/internal/zstd/literals.go
+++ b/src/internal/zstd/literals.go
@@ -175,6 +175,18 @@
return roff, outbuf, nil
}

+func decodeSymbol(huffBits, huffMask uint32, huffTable []uint16, rbr *reverseBitReader, outbuf []byte, off int) (int, error) {
+ if !rbr.fetch(uint8(huffBits)) {
+ return 0, rbr.makeError("literals Huffman stream out of bits")
+ }
+ idx := (rbr.bits >> (rbr.cnt - huffBits)) & huffMask
+ t := huffTable[idx]
+ outbuf[off] = byte(t >> 8)
+ off++
+ rbr.cnt -= uint32(t & 0xff)
+ return off, nil
+}
+
// readLiteralsOneStream reads a single stream of compressed literals.
func (r *Reader) readLiteralsOneStream(data block, off, compressedSize, regeneratedSize int, outbuf []byte) ([]byte, error) {
// We let the reverse bit reader read earlier bytes,
@@ -188,16 +200,14 @@
huffBits := uint32(r.huffmanTableBits)
huffMask := (uint32(1) << huffBits) - 1

- for i := 0; i < regeneratedSize; i++ {
- if !rbr.fetch(uint8(huffBits)) {
- return nil, rbr.makeError("literals Huffman stream out of bits")
- }
+ out := len(outbuf)
+ outbuf = append(outbuf, make([]byte, regeneratedSize)...)

- var t uint16
- idx := (rbr.bits >> (rbr.cnt - huffBits)) & huffMask
- t = huffTable[idx]
- outbuf = append(outbuf, byte(t>>8))
- rbr.cnt -= uint32(t & 0xff)
+ for i := 0; i < regeneratedSize; i++ {
+ out, err = decodeSymbol(huffBits, huffMask, huffTable, &rbr, outbuf, out)
+ if err != nil {
+ return nil, err
+ }
}

return outbuf, nil
@@ -286,50 +296,25 @@
for i := 0; i < regeneratedStreamSize; i++ {
use4 := i < regeneratedStreamSize4

- fetchHuff := func(rbr *reverseBitReader) (uint16, error) {
- if !rbr.fetch(uint8(huffBits)) {
- return 0, rbr.makeError("literals Huffman stream out of bits")
- }
- idx := (rbr.bits >> (rbr.cnt - huffBits)) & huffMask
- return huffTable[idx], nil
- }
-
- t1, err := fetchHuff(&rbr1)
+ var err error
+ out1, err = decodeSymbol(huffBits, huffMask, huffTable, &rbr1, outbuf, out1)
if err != nil {
return nil, err
}
-
- t2, err := fetchHuff(&rbr2)
+ out2, err = decodeSymbol(huffBits, huffMask, huffTable, &rbr2, outbuf, out2)
if err != nil {
return nil, err
}
-
- t3, err := fetchHuff(&rbr3)
+ out3, err = decodeSymbol(huffBits, huffMask, huffTable, &rbr3, outbuf, out3)
if err != nil {
return nil, err
}
-
if use4 {
- t4, err := fetchHuff(&rbr4)
+ out4, err = decodeSymbol(huffBits, huffMask, huffTable, &rbr4, outbuf, out4)
if err != nil {
return nil, err
}
- outbuf[out4] = byte(t4 >> 8)
- out4++
- rbr4.cnt -= uint32(t4 & 0xff)
}
-
- outbuf[out1] = byte(t1 >> 8)
- out1++
- rbr1.cnt -= uint32(t1 & 0xff)
-
- outbuf[out2] = byte(t2 >> 8)
- out2++
- rbr2.cnt -= uint32(t2 & 0xff)
-
- outbuf[out3] = byte(t3 >> 8)
- out3++
- rbr3.cnt -= uint32(t3 & 0xff)
}

return outbuf, nil

Change information

Files:
  • M src/internal/zstd/literals.go
Change size: M
Delta: 1 file changed, 24 insertions(+), 39 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: Id26eb64de41b3fa2ab1d021b5905e50d524fb231
Gerrit-Change-Number: 733000
Gerrit-PatchSet: 1
Gerrit-Owner: Garrick Gross <ggro...@gmail.com>
unsatisfied_requirement
satisfied_requirement
open
diffy

Gopher Robot (Gerrit)

unread,
Dec 28, 2025, 8:54:50 PM (7 hours ago) Dec 28
to Garrick Gross, goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

Message from Gopher Robot

Congratulations on opening your first change. Thank you for your contribution!

Next steps:
A maintainer will review your change and provide feedback. See
https://go.dev/doc/contribute#review for more info and tips to get your
patch through code review.

Most changes in the Go project go through a few rounds of revision. This can be
surprising to people new to the project. The careful, iterative review process
is our way of helping mentor contributors and ensuring that their contributions
have a lasting impact.

During May-July and Nov-Jan the Go project is in a code freeze, during which
little code gets reviewed or merged. If a reviewer responds with a comment like
R=go1.11 or adds a tag like "wait-release", it means that this CL will be
reviewed as part of the next development cycle. See https://go.dev/s/release
for more details.

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: comment
Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: Id26eb64de41b3fa2ab1d021b5905e50d524fb231
Gerrit-Change-Number: 733000
Gerrit-PatchSet: 1
Gerrit-Owner: Garrick Gross <ggro...@gmail.com>
Gerrit-CC: Gopher Robot <go...@golang.org>
Gerrit-Comment-Date: Mon, 29 Dec 2025 01:54:46 +0000
Gerrit-HasComments: No
Gerrit-Has-Labels: No
unsatisfied_requirement
satisfied_requirement
open
diffy

Ian Lance Taylor (Gerrit)

unread,
Dec 28, 2025, 9:02:16 PM (7 hours ago) Dec 28
to Garrick Gross, goph...@pubsubhelper.golang.org, Ian Lance Taylor, Gopher Robot, golang-co...@googlegroups.com
Attention needed from Garrick Gross

Ian Lance Taylor voted Commit-Queue+1

Commit-Queue+1
Open in Gerrit

Related details

Attention is currently required from:
  • Garrick Gross
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: Id26eb64de41b3fa2ab1d021b5905e50d524fb231
Gerrit-Change-Number: 733000
Gerrit-PatchSet: 1
Gerrit-Owner: Garrick Gross <ggro...@gmail.com>
Gerrit-Reviewer: Ian Lance Taylor <ia...@golang.org>
Gerrit-CC: Gopher Robot <go...@golang.org>
Gerrit-Attention: Garrick Gross <ggro...@gmail.com>
Gerrit-Comment-Date: Mon, 29 Dec 2025 02:02:12 +0000
Gerrit-HasComments: No
Gerrit-Has-Labels: Yes
unsatisfied_requirement
satisfied_requirement
open
diffy
Reply all
Reply to author
Forward
0 new messages