Skip to content

Commit

Permalink
flate: Simplify matchlen (remove asm)
Browse files Browse the repository at this point in the history
With unsafe, there is no benefit from matchlen assembly. Remove it.
  • Loading branch information
klauspost committed Jan 21, 2025
1 parent dbaa9c1 commit 8b2e695
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 119 deletions.
52 changes: 47 additions & 5 deletions flate/fast_encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package flate

import (
"fmt"
"math/bits"

"github.com/klauspost/compress/internal/le"
)
Expand Down Expand Up @@ -150,13 +151,33 @@ func (e *fastGen) matchlen(s, t int32, src []byte) int32 {
panic(fmt.Sprint(s, "-", t, "(", s-t, ") > maxMatchLength (", maxMatchOffset, ")"))
}
}
s1 := int(s) + maxMatchLength - 4
if s1 > len(src) {
s1 = len(src)
s1 := int32(s) + maxMatchLength - 4
if s1 > int32(len(src)) {
s1 = int32(len(src))
}

left := s1 - s
n := 0
for left >= 8 {
diff := le.Load64(src, s) ^ le.Load64(src, t)
if diff != 0 {
return int32(n + bits.TrailingZeros64(diff)>>3)
}
s += 8
t += 8
left -= 8
}

a := src[s:s1]
b := src[t:]
for i := range a {
if a[i] != b[i] {
break
}
n++
}
return int32(n)
// Extend the match to be as long as possible.
return int32(matchLen(src[s:s1], src[t:]))
}

// matchlenLong will return the match length between offsets and t in src.
Expand All @@ -177,7 +198,28 @@ func (e *fastGen) matchlenLong(s, t int32, src []byte) int32 {
}
}
// Extend the match to be as long as possible.
return int32(matchLen(src[s:], src[t:]))
left := int32(len(src)) - s
n := int32(0)
for left >= 8 {
diff := le.Load64(src, s) ^ le.Load64(src, t)
if diff != 0 {
return n + int32(bits.TrailingZeros64(diff)>>3)
}
s += 8
t += 8
n += 8
left -= 8
}

a := src[s:]
b := src[t:]
for i := range a {
if a[i] != b[i] {
break
}
n++
}
return n
}

// Reset the encoding table.
Expand Down
30 changes: 1 addition & 29 deletions flate/level1.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ package flate

import (
"fmt"
"math/bits"

"github.com/klauspost/compress/internal/le"
)

// fastGen maintains the table for matches,
Expand Down Expand Up @@ -122,32 +119,7 @@ func (e *fastEncL1) Encode(dst *tokens, src []byte) {

// Extend the 4-byte match as long as possible.
t := candidate.offset - e.cur
var l = int32(4)
if false {
l = e.matchlenLong(s+4, t+4, src) + 4
} else {
// inlined:
a := src[s:]
b := src[t:]
left := len(a) - 4
for left >= 8 {
if diff := le.Load64(a, l) ^ le.Load64(b, l); diff != 0 {
l += int32(bits.TrailingZeros64(diff) >> 3)
goto endMatch
}
l += 8
left -= 8
}
a = a[l:]
b = b[l:]
for i := range a {
if a[i] != b[i] {
break
}
l++
}
endMatch:
}
l := e.matchlenLong(s+4, t+4, src) + 4

// Extend backwards
for t > 0 && s > nextEmit && src[t-1] == src[s-1] {
Expand Down
16 changes: 0 additions & 16 deletions flate/matchlen_amd64.go

This file was deleted.

66 changes: 0 additions & 66 deletions flate/matchlen_amd64.s

This file was deleted.

3 changes: 0 additions & 3 deletions flate/matchlen_generic.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
//go:build !amd64 || appengine || !gc || noasm
// +build !amd64 appengine !gc noasm

// Copyright 2019+ Klaus Post. All rights reserved.
// License information can be found in the LICENSE file.

Expand Down

0 comments on commit 8b2e695

Please sign in to comment.