Skip to content

Commit

Permalink
start at min size in gear function
Browse files Browse the repository at this point in the history
  • Loading branch information
nasdf committed Jan 28, 2024
1 parent 9bc6c0e commit c6aeaca
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
10 changes: 7 additions & 3 deletions chunker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,17 @@ func TestChunkerNext(t *testing.T) {
require.NoError(t, err)

var output []byte
for chunker.HasNext() {
for {
chunk, err := chunker.Next()
require.NoError(t, err)
output = append(output, chunk...)

assert.True(t, len(chunk) <= options.maxSize)
assert.True(t, len(chunk) >= options.minSize || !chunker.HasNext())
if !chunker.HasNext() {
break
}

assert.True(t, len(chunk) <= options.maxSize, "maxSize=%d len=%d", options.maxSize, len(chunk))
assert.True(t, len(chunk) >= options.minSize, "minSize=%d len=%d", options.minSize, len(chunk))
}

expect, err := io.ReadAll(&input)
Expand Down
8 changes: 4 additions & 4 deletions fastcdc.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,19 @@ func init() {
// Boundary returns the next chunk boundary for the given bytes.
func Boundary(src []byte, options *Options) int {
size := min(len(src), options.maxSize)
if size < options.minSize {
if size <= options.minSize {
return size
}

hash := uint64(0)
norm := min(options.avgSize, size)

for i := 0; i < (norm / 2); i++ {
for i := options.minSize; i < (norm / 2); i++ {
hash = (hash << 2) + gearL[src[i*2]]
if (hash & (options.maskS << 1)) == 0 {
return i * 2
}
hash += gearN[src[i*2+1]]
hash = hash + gearN[src[i*2+1]]
if (hash & options.maskS) == 0 {
return (i * 2) + 1
}
Expand All @@ -50,7 +50,7 @@ func Boundary(src []byte, options *Options) int {
if (hash & (options.maskL << 1)) == 0 {
return i * 2
}
hash += gearN[src[i*2+1]]
hash = hash + gearN[src[i*2+1]]
if (hash & options.maskL) == 0 {
return (i * 2) + 1
}
Expand Down

0 comments on commit c6aeaca

Please sign in to comment.