Skip to content

Commit 0bf3ecb

Browse files
authored
flate: Cleanup & reduce casts (#1050)
Small improvement by reducing casts for matchlen.
1 parent e0f89a9 commit 0bf3ecb

9 files changed

+54
-35
lines changed

flate/fast_encoder.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ func hashLen(u uint64, length, mls uint8) uint32 {
136136
// matchlen will return the match length between offsets and t in src.
137137
// The maximum length returned is maxMatchLength - 4.
138138
// It is assumed that s > t, that t >=0 and s < len(src).
139-
func (e *fastGen) matchlen(s, t int32, src []byte) int32 {
139+
func (e *fastGen) matchlen(s, t int, src []byte) int32 {
140140
if debugDeflate {
141141
if t >= s {
142142
panic(fmt.Sprint("t >=s:", t, s))
@@ -151,7 +151,7 @@ func (e *fastGen) matchlen(s, t int32, src []byte) int32 {
151151
panic(fmt.Sprint(s, "-", t, "(", s-t, ") > maxMatchLength (", maxMatchOffset, ")"))
152152
}
153153
}
154-
s1 := min(s+maxMatchLength-4, int32(len(src)))
154+
s1 := min(s+maxMatchLength-4, len(src))
155155
left := s1 - s
156156
n := int32(0)
157157
for left >= 8 {
@@ -178,7 +178,7 @@ func (e *fastGen) matchlen(s, t int32, src []byte) int32 {
178178

179179
// matchlenLong will return the match length between offsets and t in src.
180180
// It is assumed that s > t, that t >=0 and s < len(src).
181-
func (e *fastGen) matchlenLong(s, t int32, src []byte) int32 {
181+
func (e *fastGen) matchlenLong(s, t int, src []byte) int32 {
182182
if debugDeflate {
183183
if t >= s {
184184
panic(fmt.Sprint("t >=s:", t, s))
@@ -194,7 +194,7 @@ func (e *fastGen) matchlenLong(s, t int32, src []byte) int32 {
194194
}
195195
}
196196
// Extend the match to be as long as possible.
197-
left := int32(len(src)) - s
197+
left := len(src) - s
198198
n := int32(0)
199199
for left >= 8 {
200200
diff := le.Load64(src, s) ^ le.Load64(src, t)

flate/level1.go

+11-10
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package flate
22

33
import (
44
"fmt"
5+
6+
"github.com/klauspost/compress/internal/le"
57
)
68

79
// fastGen maintains the table for matches,
@@ -75,6 +77,7 @@ func (e *fastEncL1) Encode(dst *tokens, src []byte) {
7577

7678
nextS := s
7779
var candidate tableEntry
80+
var t int32
7881
for {
7982
nextHash := hashLen(cv, tableBits, hashBytes)
8083
candidate = e.table[nextHash]
@@ -86,9 +89,8 @@ func (e *fastEncL1) Encode(dst *tokens, src []byte) {
8689
now := load6432(src, nextS)
8790
e.table[nextHash] = tableEntry{offset: s + e.cur}
8891
nextHash = hashLen(now, tableBits, hashBytes)
89-
90-
offset := s - (candidate.offset - e.cur)
91-
if offset < maxMatchOffset && uint32(cv) == load3232(src, candidate.offset-e.cur) {
92+
t = candidate.offset - e.cur
93+
if s-t < maxMatchOffset && uint32(cv) == load3232(src, t) {
9294
e.table[nextHash] = tableEntry{offset: nextS + e.cur}
9395
break
9496
}
@@ -101,8 +103,8 @@ func (e *fastEncL1) Encode(dst *tokens, src []byte) {
101103
now >>= 8
102104
e.table[nextHash] = tableEntry{offset: s + e.cur}
103105

104-
offset = s - (candidate.offset - e.cur)
105-
if offset < maxMatchOffset && uint32(cv) == load3232(src, candidate.offset-e.cur) {
106+
t = candidate.offset - e.cur
107+
if s-t < maxMatchOffset && uint32(cv) == load3232(src, t) {
106108
e.table[nextHash] = tableEntry{offset: nextS + e.cur}
107109
break
108110
}
@@ -118,11 +120,10 @@ func (e *fastEncL1) Encode(dst *tokens, src []byte) {
118120
// literal bytes prior to s.
119121

120122
// Extend the 4-byte match as long as possible.
121-
t := candidate.offset - e.cur
122-
l := e.matchlenLong(s+4, t+4, src) + 4
123+
l := e.matchlenLong(int(s+4), int(t+4), src) + 4
123124

124125
// Extend backwards
125-
for t > 0 && s > nextEmit && src[t-1] == src[s-1] {
126+
for t > 0 && s > nextEmit && le.Load8(src, t-1) == le.Load8(src, s-1) {
126127
s--
127128
t--
128129
l++
@@ -194,8 +195,8 @@ func (e *fastEncL1) Encode(dst *tokens, src []byte) {
194195
candidate = e.table[currHash]
195196
e.table[currHash] = tableEntry{offset: o + 2}
196197

197-
offset := s - (candidate.offset - e.cur)
198-
if offset > maxMatchOffset || uint32(x) != load3232(src, candidate.offset-e.cur) {
198+
t = candidate.offset - e.cur
199+
if s-t > maxMatchOffset || uint32(x) != load3232(src, t) {
199200
cv = x >> 8
200201
s++
201202
break

flate/level2.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ func (e *fastEncL2) Encode(dst *tokens, src []byte) {
126126

127127
// Extend the 4-byte match as long as possible.
128128
t := candidate.offset - e.cur
129-
l := e.matchlenLong(s+4, t+4, src) + 4
129+
l := e.matchlenLong(int(s+4), int(t+4), src) + 4
130130

131131
// Extend backwards
132132
for t > 0 && s > nextEmit && src[t-1] == src[s-1] {

flate/level3.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ func (e *fastEncL3) Encode(dst *tokens, src []byte) {
135135
// Extend the 4-byte match as long as possible.
136136
//
137137
t := candidate.offset - e.cur
138-
l := e.matchlenLong(s+4, t+4, src) + 4
138+
l := e.matchlenLong(int(s+4), int(t+4), src) + 4
139139

140140
// Extend backwards
141141
for t > 0 && s > nextEmit && src[t-1] == src[s-1] {

flate/level4.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ func (e *fastEncL4) Encode(dst *tokens, src []byte) {
127127
// them as literal bytes.
128128

129129
// Extend the 4-byte match as long as possible.
130-
l := e.matchlenLong(s+4, t+4, src) + 4
130+
l := e.matchlenLong(int(s+4), int(t+4), src) + 4
131131

132132
// Extend backwards
133133
for t > 0 && s > nextEmit && src[t-1] == src[s-1] {

flate/level5.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,8 @@ func (e *fastEncL5) Encode(dst *tokens, src []byte) {
119119

120120
t2 := lCandidate.Prev.offset - e.cur
121121
if s-t2 < maxMatchOffset && uint32(cv) == load3232(src, t2) {
122-
l = e.matchlen(s+4, t+4, src) + 4
123-
ml1 := e.matchlen(s+4, t2+4, src) + 4
122+
l = e.matchlen(int(s+4), int(t+4), src) + 4
123+
ml1 := e.matchlen(int(s+4), int(t2+4), src) + 4
124124
if ml1 > l {
125125
t = t2
126126
l = ml1
@@ -142,7 +142,7 @@ func (e *fastEncL5) Encode(dst *tokens, src []byte) {
142142
t = sCandidate.offset - e.cur
143143
if s-t < maxMatchOffset && uint32(cv) == load3232(src, t) {
144144
// Found a 4 match...
145-
l = e.matchlen(s+4, t+4, src) + 4
145+
l = e.matchlen(int(s+4), int(t+4), src) + 4
146146
lCandidate = e.bTable[nextHashL]
147147
// Store the next match
148148

@@ -154,7 +154,7 @@ func (e *fastEncL5) Encode(dst *tokens, src []byte) {
154154
t2 := lCandidate.Cur.offset - e.cur
155155
if nextS-t2 < maxMatchOffset {
156156
if load3232(src, t2) == uint32(next) {
157-
ml := e.matchlen(nextS+4, t2+4, src) + 4
157+
ml := e.matchlen(int(nextS+4), int(t2+4), src) + 4
158158
if ml > l {
159159
t = t2
160160
s = nextS
@@ -165,7 +165,7 @@ func (e *fastEncL5) Encode(dst *tokens, src []byte) {
165165
// If the previous long is a candidate, use that...
166166
t2 = lCandidate.Prev.offset - e.cur
167167
if nextS-t2 < maxMatchOffset && load3232(src, t2) == uint32(next) {
168-
ml := e.matchlen(nextS+4, t2+4, src) + 4
168+
ml := e.matchlen(int(nextS+4), int(t2+4), src) + 4
169169
if ml > l {
170170
t = t2
171171
s = nextS
@@ -185,9 +185,9 @@ func (e *fastEncL5) Encode(dst *tokens, src []byte) {
185185

186186
if l == 0 {
187187
// Extend the 4-byte match as long as possible.
188-
l = e.matchlenLong(s+4, t+4, src) + 4
188+
l = e.matchlenLong(int(s+4), int(t+4), src) + 4
189189
} else if l == maxMatchLength {
190-
l += e.matchlenLong(s+l, t+l, src)
190+
l += e.matchlenLong(int(s+l), int(t+l), src)
191191
}
192192

193193
// Try to locate a better match by checking the end of best match...
@@ -203,7 +203,7 @@ func (e *fastEncL5) Encode(dst *tokens, src []byte) {
203203
s2 := s + skipBeginning
204204
off := s2 - t2
205205
if t2 >= 0 && off < maxMatchOffset && off > 0 {
206-
if l2 := e.matchlenLong(s2, t2, src); l2 > l {
206+
if l2 := e.matchlenLong(int(s2), int(t2), src); l2 > l {
207207
t = t2
208208
l = l2
209209
s = s2

flate/level6.go

+10-10
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,8 @@ func (e *fastEncL6) Encode(dst *tokens, src []byte) {
124124
// Check the previous long candidate as well.
125125
t2 := lCandidate.Prev.offset - e.cur
126126
if s-t2 < maxMatchOffset && uint32(cv) == load3232(src, t2) {
127-
l = e.matchlen(s+4, t+4, src) + 4
128-
ml1 := e.matchlen(s+4, t2+4, src) + 4
127+
l = e.matchlen(int(s+4), int(t+4), src) + 4
128+
ml1 := e.matchlen(int(s+4), int(t2+4), src) + 4
129129
if ml1 > l {
130130
t = t2
131131
l = ml1
@@ -148,7 +148,7 @@ func (e *fastEncL6) Encode(dst *tokens, src []byte) {
148148
t = sCandidate.offset - e.cur
149149
if s-t < maxMatchOffset && uint32(cv) == load3232(src, t) {
150150
// Found a 4 match...
151-
l = e.matchlen(s+4, t+4, src) + 4
151+
l = e.matchlen(int(s+4), int(t+4), src) + 4
152152

153153
// Look up next long candidate (at nextS)
154154
lCandidate = e.bTable[nextHashL]
@@ -162,7 +162,7 @@ func (e *fastEncL6) Encode(dst *tokens, src []byte) {
162162
const repOff = 1
163163
t2 := s - repeat + repOff
164164
if load3232(src, t2) == uint32(cv>>(8*repOff)) {
165-
ml := e.matchlen(s+4+repOff, t2+4, src) + 4
165+
ml := e.matchlen(int(s+4+repOff), int(t2+4), src) + 4
166166
if ml > l {
167167
t = t2
168168
l = ml
@@ -176,7 +176,7 @@ func (e *fastEncL6) Encode(dst *tokens, src []byte) {
176176
t2 = lCandidate.Cur.offset - e.cur
177177
if nextS-t2 < maxMatchOffset {
178178
if load3232(src, t2) == uint32(next) {
179-
ml := e.matchlen(nextS+4, t2+4, src) + 4
179+
ml := e.matchlen(int(nextS+4), int(t2+4), src) + 4
180180
if ml > l {
181181
t = t2
182182
s = nextS
@@ -187,7 +187,7 @@ func (e *fastEncL6) Encode(dst *tokens, src []byte) {
187187
// If the previous long is a candidate, use that...
188188
t2 = lCandidate.Prev.offset - e.cur
189189
if nextS-t2 < maxMatchOffset && load3232(src, t2) == uint32(next) {
190-
ml := e.matchlen(nextS+4, t2+4, src) + 4
190+
ml := e.matchlen(int(nextS+4), int(t2+4), src) + 4
191191
if ml > l {
192192
t = t2
193193
s = nextS
@@ -207,9 +207,9 @@ func (e *fastEncL6) Encode(dst *tokens, src []byte) {
207207

208208
// Extend the 4-byte match as long as possible.
209209
if l == 0 {
210-
l = e.matchlenLong(s+4, t+4, src) + 4
210+
l = e.matchlenLong(int(s+4), int(t+4), src) + 4
211211
} else if l == maxMatchLength {
212-
l += e.matchlenLong(s+l, t+l, src)
212+
l += e.matchlenLong(int(s+l), int(t+l), src)
213213
}
214214

215215
// Try to locate a better match by checking the end-of-match...
@@ -227,7 +227,7 @@ func (e *fastEncL6) Encode(dst *tokens, src []byte) {
227227
off := s2 - t2
228228
if off < maxMatchOffset {
229229
if off > 0 && t2 >= 0 {
230-
if l2 := e.matchlenLong(s2, t2, src); l2 > l {
230+
if l2 := e.matchlenLong(int(s2), int(t2), src); l2 > l {
231231
t = t2
232232
l = l2
233233
s = s2
@@ -237,7 +237,7 @@ func (e *fastEncL6) Encode(dst *tokens, src []byte) {
237237
t2 = eLong.Prev.offset - e.cur - l + skipBeginning
238238
off := s2 - t2
239239
if off > 0 && off < maxMatchOffset && t2 >= 0 {
240-
if l2 := e.matchlenLong(s2, t2, src); l2 > l {
240+
if l2 := e.matchlenLong(int(s2), int(t2), src); l2 > l {
241241
t = t2
242242
l = l2
243243
s = s2

internal/le/unsafe_disabled.go

+11
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,37 @@ import (
66
"encoding/binary"
77
)
88

9+
// Load8 will load from b at index i.
10+
func Load8[I Indexer](b []byte, i I) byte {
11+
return b[i]
12+
}
13+
14+
// Load16 will load from b at index i.
915
func Load16[I Indexer](b []byte, i I) uint16 {
1016
return binary.LittleEndian.Uint16(b[i:])
1117
}
1218

19+
// Load32 will load from b at index i.
1320
func Load32[I Indexer](b []byte, i I) uint32 {
1421
return binary.LittleEndian.Uint32(b[i:])
1522
}
1623

24+
// Load64 will load from b at index i.
1725
func Load64[I Indexer](b []byte, i I) uint64 {
1826
return binary.LittleEndian.Uint64(b[i:])
1927
}
2028

29+
// Store16 will store v at b.
2130
func Store16(b []byte, v uint16) {
2231
binary.LittleEndian.PutUint16(b, v)
2332
}
2433

34+
// Store32 will store v at b.
2535
func Store32(b []byte, v uint32) {
2636
binary.LittleEndian.PutUint32(b, v)
2737
}
2838

39+
// Store64 will store v at b.
2940
func Store64(b []byte, v uint64) {
3041
binary.LittleEndian.PutUint64(b, v)
3142
}

internal/le/unsafe_enabled.go

+7
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@ import (
88
"unsafe"
99
)
1010

11+
// Load8 will load from b at index i.
12+
func Load8[I Indexer](b []byte, i I) byte {
13+
//return binary.LittleEndian.Uint16(b[i:])
14+
//return *(*uint16)(unsafe.Pointer(&b[i]))
15+
return *(*byte)(unsafe.Add(unsafe.Pointer(unsafe.SliceData(b)), i))
16+
}
17+
1118
// Load16 will load from b at index i.
1219
func Load16[I Indexer](b []byte, i I) uint16 {
1320
//return binary.LittleEndian.Uint16(b[i:])

0 commit comments

Comments
 (0)