Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

zstd: Improve throughput of SpeedBestCompression encoder #699

Merged
merged 1 commit into from Nov 26, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 17 additions & 15 deletions zstd/enc_best.go
Expand Up @@ -38,27 +38,29 @@ type match struct {
const highScore = 25000

// estBits will estimate output bits from predefined tables.
func (m *match) estBits(bitsPerByte int32) {
mlc := mlCode(uint32(m.length - zstdMinMatch))
var ofc uint8
if m.rep < 0 {
ofc = ofCode(uint32(m.s-m.offset) + 3)
} else {
ofc = ofCode(uint32(m.rep))
}
func (m *match) estBits(bitsPerByte int32, ofc, mlc uint8) {
// Cost, excluding
ofTT, mlTT := fsePredefEnc[tableOffsets].ct.symbolTT[ofc], fsePredefEnc[tableMatchLengths].ct.symbolTT[mlc]

// Add cost of match encoding...
m.est = int32(ofTT.outBits + mlTT.outBits)
m.est += int32(ofTT.deltaNbBits>>16 + mlTT.deltaNbBits>>16)
est := int32(ofTT.outBits + mlTT.outBits)
est += int32(ofTT.deltaNbBits>>16 + mlTT.deltaNbBits>>16)
// Subtract savings compared to literal encoding...
m.est -= (m.length * bitsPerByte) >> 10
if m.est > 0 {
length := m.length
est -= (length * bitsPerByte) >> 10
if est > 0 {
// Unlikely gain..
m.length = 0
m.est = highScore
est, length = highScore, 0
}
m.est, m.length = est, length
}

func (m *match) ofCode() uint8 {
offset := uint32(m.rep)
if offset < 0 {
offset = uint32(m.s-m.offset) + 3
}
return ofCode(offset)
}

// bestFastEncoder uses 2 tables, one for short matches (5 bytes) and one for long matches.
Expand Down Expand Up @@ -216,7 +218,7 @@ encodeLoop:
}
}
m := match{offset: offset, s: s, length: 4 + e.matchlen(s+4, offset+4, src), rep: rep}
m.estBits(bitsPerByte)
m.estBits(bitsPerByte, m.ofCode(), mlCode(uint32(m.length-zstdMinMatch)))
return m
}

Expand Down