Skip to content

Commit dbaa9c1

Browse files
authored
flate: Simplify l4-6 loading (#1043)
We already calculate the buffer offset. Reuse in case the compiler doesn't pick it up.
1 parent 4fa2036 commit dbaa9c1

File tree

3 files changed

+22
-22
lines changed

3 files changed

+22
-22
lines changed

flate/level4.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -98,19 +98,19 @@ func (e *fastEncL4) Encode(dst *tokens, src []byte) {
9898
e.bTable[nextHashL] = entry
9999

100100
t = lCandidate.offset - e.cur
101-
if s-t < maxMatchOffset && uint32(cv) == load3232(src, lCandidate.offset-e.cur) {
101+
if s-t < maxMatchOffset && uint32(cv) == load3232(src, t) {
102102
// We got a long match. Use that.
103103
break
104104
}
105105

106106
t = sCandidate.offset - e.cur
107-
if s-t < maxMatchOffset && uint32(cv) == load3232(src, sCandidate.offset-e.cur) {
107+
if s-t < maxMatchOffset && uint32(cv) == load3232(src, t) {
108108
// Found a 4 match...
109109
lCandidate = e.bTable[hash7(next, tableBits)]
110110

111111
// If the next long is a candidate, check if we should use that instead...
112-
lOff := nextS - (lCandidate.offset - e.cur)
113-
if lOff < maxMatchOffset && load3232(src, lCandidate.offset-e.cur) == uint32(next) {
112+
lOff := lCandidate.offset - e.cur
113+
if nextS-lOff < maxMatchOffset && load3232(src, lOff) == uint32(next) {
114114
l1, l2 := matchLen(src[s+4:], src[t+4:]), matchLen(src[nextS+4:], src[nextS-lOff+4:])
115115
if l2 > l1 {
116116
s = nextS

flate/level5.go

+12-12
Original file line numberDiff line numberDiff line change
@@ -111,14 +111,14 @@ func (e *fastEncL5) Encode(dst *tokens, src []byte) {
111111

112112
t = lCandidate.Cur.offset - e.cur
113113
if s-t < maxMatchOffset {
114-
if uint32(cv) == load3232(src, lCandidate.Cur.offset-e.cur) {
114+
if uint32(cv) == load3232(src, t) {
115115
// Store the next match
116116
e.table[nextHashS] = tableEntry{offset: nextS + e.cur}
117117
eLong := &e.bTable[nextHashL]
118118
eLong.Cur, eLong.Prev = tableEntry{offset: nextS + e.cur}, eLong.Cur
119119

120120
t2 := lCandidate.Prev.offset - e.cur
121-
if s-t2 < maxMatchOffset && uint32(cv) == load3232(src, lCandidate.Prev.offset-e.cur) {
121+
if s-t2 < maxMatchOffset && uint32(cv) == load3232(src, t2) {
122122
l = e.matchlen(s+4, t+4, src) + 4
123123
ml1 := e.matchlen(s+4, t2+4, src) + 4
124124
if ml1 > l {
@@ -130,7 +130,7 @@ func (e *fastEncL5) Encode(dst *tokens, src []byte) {
130130
break
131131
}
132132
t = lCandidate.Prev.offset - e.cur
133-
if s-t < maxMatchOffset && uint32(cv) == load3232(src, lCandidate.Prev.offset-e.cur) {
133+
if s-t < maxMatchOffset && uint32(cv) == load3232(src, t) {
134134
// Store the next match
135135
e.table[nextHashS] = tableEntry{offset: nextS + e.cur}
136136
eLong := &e.bTable[nextHashL]
@@ -140,7 +140,7 @@ func (e *fastEncL5) Encode(dst *tokens, src []byte) {
140140
}
141141

142142
t = sCandidate.offset - e.cur
143-
if s-t < maxMatchOffset && uint32(cv) == load3232(src, sCandidate.offset-e.cur) {
143+
if s-t < maxMatchOffset && uint32(cv) == load3232(src, t) {
144144
// Found a 4 match...
145145
l = e.matchlen(s+4, t+4, src) + 4
146146
lCandidate = e.bTable[nextHashL]
@@ -153,7 +153,7 @@ func (e *fastEncL5) Encode(dst *tokens, src []byte) {
153153
// If the next long is a candidate, use that...
154154
t2 := lCandidate.Cur.offset - e.cur
155155
if nextS-t2 < maxMatchOffset {
156-
if load3232(src, lCandidate.Cur.offset-e.cur) == uint32(next) {
156+
if load3232(src, t2) == uint32(next) {
157157
ml := e.matchlen(nextS+4, t2+4, src) + 4
158158
if ml > l {
159159
t = t2
@@ -164,7 +164,7 @@ func (e *fastEncL5) Encode(dst *tokens, src []byte) {
164164
}
165165
// If the previous long is a candidate, use that...
166166
t2 = lCandidate.Prev.offset - e.cur
167-
if nextS-t2 < maxMatchOffset && load3232(src, lCandidate.Prev.offset-e.cur) == uint32(next) {
167+
if nextS-t2 < maxMatchOffset && load3232(src, t2) == uint32(next) {
168168
ml := e.matchlen(nextS+4, t2+4, src) + 4
169169
if ml > l {
170170
t = t2
@@ -423,14 +423,14 @@ func (e *fastEncL5Window) Encode(dst *tokens, src []byte) {
423423

424424
t = lCandidate.Cur.offset - e.cur
425425
if s-t < maxMatchOffset {
426-
if uint32(cv) == load3232(src, lCandidate.Cur.offset-e.cur) {
426+
if uint32(cv) == load3232(src, t) {
427427
// Store the next match
428428
e.table[nextHashS] = tableEntry{offset: nextS + e.cur}
429429
eLong := &e.bTable[nextHashL]
430430
eLong.Cur, eLong.Prev = tableEntry{offset: nextS + e.cur}, eLong.Cur
431431

432432
t2 := lCandidate.Prev.offset - e.cur
433-
if s-t2 < maxMatchOffset && uint32(cv) == load3232(src, lCandidate.Prev.offset-e.cur) {
433+
if s-t2 < maxMatchOffset && uint32(cv) == load3232(src, t2) {
434434
l = e.matchlen(s+4, t+4, src) + 4
435435
ml1 := e.matchlen(s+4, t2+4, src) + 4
436436
if ml1 > l {
@@ -442,7 +442,7 @@ func (e *fastEncL5Window) Encode(dst *tokens, src []byte) {
442442
break
443443
}
444444
t = lCandidate.Prev.offset - e.cur
445-
if s-t < maxMatchOffset && uint32(cv) == load3232(src, lCandidate.Prev.offset-e.cur) {
445+
if s-t < maxMatchOffset && uint32(cv) == load3232(src, t) {
446446
// Store the next match
447447
e.table[nextHashS] = tableEntry{offset: nextS + e.cur}
448448
eLong := &e.bTable[nextHashL]
@@ -452,7 +452,7 @@ func (e *fastEncL5Window) Encode(dst *tokens, src []byte) {
452452
}
453453

454454
t = sCandidate.offset - e.cur
455-
if s-t < maxMatchOffset && uint32(cv) == load3232(src, sCandidate.offset-e.cur) {
455+
if s-t < maxMatchOffset && uint32(cv) == load3232(src, t) {
456456
// Found a 4 match...
457457
l = e.matchlen(s+4, t+4, src) + 4
458458
lCandidate = e.bTable[nextHashL]
@@ -465,7 +465,7 @@ func (e *fastEncL5Window) Encode(dst *tokens, src []byte) {
465465
// If the next long is a candidate, use that...
466466
t2 := lCandidate.Cur.offset - e.cur
467467
if nextS-t2 < maxMatchOffset {
468-
if load3232(src, lCandidate.Cur.offset-e.cur) == uint32(next) {
468+
if load3232(src, t2) == uint32(next) {
469469
ml := e.matchlen(nextS+4, t2+4, src) + 4
470470
if ml > l {
471471
t = t2
@@ -476,7 +476,7 @@ func (e *fastEncL5Window) Encode(dst *tokens, src []byte) {
476476
}
477477
// If the previous long is a candidate, use that...
478478
t2 = lCandidate.Prev.offset - e.cur
479-
if nextS-t2 < maxMatchOffset && load3232(src, lCandidate.Prev.offset-e.cur) == uint32(next) {
479+
if nextS-t2 < maxMatchOffset && load3232(src, t2) == uint32(next) {
480480
ml := e.matchlen(nextS+4, t2+4, src) + 4
481481
if ml > l {
482482
t = t2

flate/level6.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ func (e *fastEncL6) Encode(dst *tokens, src []byte) {
113113

114114
t = lCandidate.Cur.offset - e.cur
115115
if s-t < maxMatchOffset {
116-
if uint32(cv) == load3232(src, lCandidate.Cur.offset-e.cur) {
116+
if uint32(cv) == load3232(src, t) {
117117
// Long candidate matches at least 4 bytes.
118118

119119
// Store the next match
@@ -123,7 +123,7 @@ func (e *fastEncL6) Encode(dst *tokens, src []byte) {
123123

124124
// Check the previous long candidate as well.
125125
t2 := lCandidate.Prev.offset - e.cur
126-
if s-t2 < maxMatchOffset && uint32(cv) == load3232(src, lCandidate.Prev.offset-e.cur) {
126+
if s-t2 < maxMatchOffset && uint32(cv) == load3232(src, t2) {
127127
l = e.matchlen(s+4, t+4, src) + 4
128128
ml1 := e.matchlen(s+4, t2+4, src) + 4
129129
if ml1 > l {
@@ -136,7 +136,7 @@ func (e *fastEncL6) Encode(dst *tokens, src []byte) {
136136
}
137137
// Current value did not match, but check if previous long value does.
138138
t = lCandidate.Prev.offset - e.cur
139-
if s-t < maxMatchOffset && uint32(cv) == load3232(src, lCandidate.Prev.offset-e.cur) {
139+
if s-t < maxMatchOffset && uint32(cv) == load3232(src, t) {
140140
// Store the next match
141141
e.table[nextHashS] = tableEntry{offset: nextS + e.cur}
142142
eLong := &e.bTable[nextHashL]
@@ -146,7 +146,7 @@ func (e *fastEncL6) Encode(dst *tokens, src []byte) {
146146
}
147147

148148
t = sCandidate.offset - e.cur
149-
if s-t < maxMatchOffset && uint32(cv) == load3232(src, sCandidate.offset-e.cur) {
149+
if s-t < maxMatchOffset && uint32(cv) == load3232(src, t) {
150150
// Found a 4 match...
151151
l = e.matchlen(s+4, t+4, src) + 4
152152

@@ -175,7 +175,7 @@ func (e *fastEncL6) Encode(dst *tokens, src []byte) {
175175
// If the next long is a candidate, use that...
176176
t2 = lCandidate.Cur.offset - e.cur
177177
if nextS-t2 < maxMatchOffset {
178-
if load3232(src, lCandidate.Cur.offset-e.cur) == uint32(next) {
178+
if load3232(src, t2) == uint32(next) {
179179
ml := e.matchlen(nextS+4, t2+4, src) + 4
180180
if ml > l {
181181
t = t2
@@ -186,7 +186,7 @@ func (e *fastEncL6) Encode(dst *tokens, src []byte) {
186186
}
187187
// If the previous long is a candidate, use that...
188188
t2 = lCandidate.Prev.offset - e.cur
189-
if nextS-t2 < maxMatchOffset && load3232(src, lCandidate.Prev.offset-e.cur) == uint32(next) {
189+
if nextS-t2 < maxMatchOffset && load3232(src, t2) == uint32(next) {
190190
ml := e.matchlen(nextS+4, t2+4, src) + 4
191191
if ml > l {
192192
t = t2

0 commit comments

Comments
 (0)