Skip to content

Commit

Permalink
Do forward hashing of second index
Browse files Browse the repository at this point in the history
  • Loading branch information
klauspost committed May 4, 2023
1 parent a712800 commit ce6e8a3
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 126 deletions.
23 changes: 11 additions & 12 deletions s2/_generate/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -1417,31 +1417,30 @@ func (o options) genEncodeBetterBlockAsm(name string, lTableBits, sTableBits, sk
MOVL(plusone0.As32(), sTab.Idx(hash0s, 4))
MOVL(plusone1.As32(), sTab.Idx(hash1s, 4))

// index2 := (index0 + index1 + 1) >> 1
index2 := GP64()
LEAQ(Mem{Base: index1, Disp: 1, Index: index0, Scale: 1}, index2)
SHRQ(U8(1), index2)

ADDQ(U8(1), index0)
SUBQ(U8(1), index1)

// forward := bits.Len(uint(index1 - index0))
forward := GP64()
MOVQ(index1, forward)
SUBQ(index0, forward)
BSRQ(forward, forward)
ADDQ(U8(1), forward)

Label("index_loop_" + name)
CMPQ(index0, index1)
// for index2 < index1
CMPQ(index2, index1)
JAE(LabelRef("search_loop_" + name))
hash0l, hash1l = GP64(), GP64()
MOVQ(Mem{Base: src, Index: index0, Scale: 1, Disp: 0}, hash0l)
MOVQ(Mem{Base: src, Index: index1, Scale: 1, Disp: 0}, hash1l)
MOVQ(Mem{Base: src, Index: index2, Scale: 1, Disp: 0}, hash1l)

lHasher.hash(hash0l)
lHasher.hash(hash1l)

MOVL(index0.As32(), lTab.Idx(hash0l, 4))
MOVL(index1.As32(), lTab.Idx(hash1l, 4))
MOVL(index2.As32(), lTab.Idx(hash1l, 4))

ADDQ(forward, index0)
SUBQ(forward, index1)
ADDQ(U8(2), index0)
ADDQ(U8(2), index2)
JMP(LabelRef("index_loop_" + name))
} else {
lHasher := hashN(lHashBytes, lTableBits)
Expand Down
51 changes: 27 additions & 24 deletions s2/encode_better.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,21 +268,21 @@ func encodeBlockBetterGo(dst, src []byte) (d int) {
lTable[hash7(cv0, lTableBits)] = uint32(index0)
sTable[hash4(cv0>>8, sTableBits)] = uint32(index0 + 1)

// lTable could be postponed, but very minor difference.
lTable[hash7(cv1, lTableBits)] = uint32(index1)
sTable[hash4(cv1>>8, sTableBits)] = uint32(index1 + 1)
index0 += 1
index1 -= 1
cv = load64(src, s)

// Add less for longer matches.
// Will only be 0 when equal (thus failing loop)
forward := bits.Len(uint(index1 - index0))

for index0 < index1 {
// Index large values sparsely in between.
// We do two starting from different offsets for speed.
index2 := (index0 + index1 + 1) >> 1
for index2 < index1 {
lTable[hash7(load64(src, index0), lTableBits)] = uint32(index0)
lTable[hash7(load64(src, index1), lTableBits)] = uint32(index1)
index0 += forward
index1 -= forward
lTable[hash7(load64(src, index2), lTableBits)] = uint32(index2)
index0 += 2
index2 += 2
}
}

Expand Down Expand Up @@ -461,15 +461,14 @@ func encodeBlockBetterSnappyGo(dst, src []byte) (d int) {
index1 -= 1
cv = load64(src, s)

// Add less for longer matches.
// Will only be 0 when equal (thus failing loop)
forward := bits.Len(uint(index1 - index0))

for index0 < index1 {
// Index large values sparsely in between.
// We do two starting from different offsets for speed.
index2 := (index0 + index1 + 1) >> 1
for index2 < index1 {
lTable[hash7(load64(src, index0), lTableBits)] = uint32(index0)
lTable[hash7(load64(src, index1), lTableBits)] = uint32(index1)
index0 += forward
index1 -= forward
lTable[hash7(load64(src, index2), lTableBits)] = uint32(index2)
index0 += 2
index2 += 2
}
}

Expand Down Expand Up @@ -869,12 +868,14 @@ searchDict:
index1 -= 1
cv = load64(src, s)

// index every second long in between.
for index0 < index1 {
// Index large values sparsely in between.
// We do two starting from different offsets for speed.
index2 := (index0 + index1 + 1) >> 1
for index2 < index1 {
lTable[hash7(load64(src, index0), lTableBits)] = uint32(index0)
lTable[hash7(load64(src, index1), lTableBits)] = uint32(index1)
lTable[hash7(load64(src, index2), lTableBits)] = uint32(index2)
index0 += 2
index1 -= 2
index2 += 2
}
}

Expand Down Expand Up @@ -1082,12 +1083,14 @@ searchDict:
index1 -= 1
cv = load64(src, s)

// index every second long in between.
for index0 < index1 {
// Index large values sparsely in between.
// We do two starting from different offsets for speed.
index2 := (index0 + index1 + 1) >> 1
for index2 < index1 {
lTable[hash7(load64(src, index0), lTableBits)] = uint32(index0)
lTable[hash7(load64(src, index1), lTableBits)] = uint32(index1)
lTable[hash7(load64(src, index2), lTableBits)] = uint32(index2)
index0 += 2
index1 -= 2
index2 += 2
}
}

Expand Down
Loading

0 comments on commit ce6e8a3

Please sign in to comment.