Skip to content

Commit

Permalink
zstd: Load source value at start of loop (#794)
Browse files Browse the repository at this point in the history
There were four places were this value was loaded: before the loop, at
the end, and before two continues. Doing it at the start reduces the
code size. It also seems to give a tiny speedup:

name                              old speed      new speed      delta
Encoder_EncodeAllSimple/best-8    16.8MB/s ± 2%  16.9MB/s ± 1%  +0.47%  (p=0.025 n=8+10)
Encoder_EncodeAllSimple4K/best-8  16.0MB/s ± 2%  16.2MB/s ± 1%  +1.67%  (p=0.000 n=10+10)

name                              old alloc/op   new alloc/op   delta
Encoder_EncodeAllSimple/best-8       19.0B ± 5%     18.0B ± 0%  -5.26%  (p=0.002 n=9+9)
Encoder_EncodeAllSimple4K/best-8     1.00B ± 0%     1.00B ± 0%    ~     (all equal)
  • Loading branch information
greatroar authored Mar 26, 2023
1 parent 2a02ad2 commit 6c7dd07
Showing 1 changed file with 2 additions and 6 deletions.
8 changes: 2 additions & 6 deletions zstd/enc_best.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,6 @@ func (e *bestFastEncoder) Encode(blk *blockEnc, src []byte) {

// nextEmit is where in src the next emitLiteral should start from.
nextEmit := s
cv := load6432(src, s)

// Relative offsets
offset1 := int32(blk.recentOffsets[0])
Expand All @@ -173,7 +172,6 @@ func (e *bestFastEncoder) Encode(blk *blockEnc, src []byte) {
blk.literals = append(blk.literals, src[nextEmit:until]...)
s.litLen = uint32(until - nextEmit)
}
_ = addLiterals

if debugEncoder {
println("recent offsets:", blk.recentOffsets)
Expand All @@ -190,6 +188,8 @@ encodeLoop:

const goodEnough = 250

cv := load6432(src, s)

nextHashL := hashLen(cv, bestLongTableBits, bestLongLen)
nextHashS := hashLen(cv, bestShortTableBits, bestShortLen)
candidateL := e.longTable[nextHashL]
Expand Down Expand Up @@ -290,7 +290,6 @@ encodeLoop:
if s >= sLimit {
break encodeLoop
}
cv = load6432(src, s)
continue
}

Expand Down Expand Up @@ -364,7 +363,6 @@ encodeLoop:
if s >= sLimit {
if debugEncoder {
println("repeat ended", s, best.length)

}
break encodeLoop
}
Expand All @@ -387,7 +385,6 @@ encodeLoop:
case 4 | 3:
offset1, offset2, offset3 = offset1-1, offset1, offset2
}
cv = load6432(src, s)
continue
}

Expand Down Expand Up @@ -435,7 +432,6 @@ encodeLoop:
e.table[h1] = prevEntry{offset: off, prev: e.table[h1].offset}
index0++
}
cv = load6432(src, s)
}

if int(nextEmit) < len(src) {
Expand Down

0 comments on commit 6c7dd07

Please sign in to comment.