Skip to content

Commit

Permalink
s2: Improve Better compression slightly (#663)
Browse files Browse the repository at this point in the history
* Do forward hashing of second index.
  • Loading branch information
klauspost committed May 4, 2023
1 parent aa3702a commit 9a951be
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 111 deletions.
14 changes: 10 additions & 4 deletions s2/_generate/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -1417,24 +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)

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(U8(2), index0)
SUBQ(U8(2), index1)
ADDQ(U8(2), index2)
JMP(LabelRef("index_loop_" + name))
} else {
lHasher := hashN(lHashBytes, lTableBits)
Expand Down
41 changes: 25 additions & 16 deletions s2/encode_better.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,18 +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)

// 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 @@ -458,12 +461,14 @@ func encodeBlockBetterSnappyGo(dst, src []byte) (d int) {
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 @@ -863,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 @@ -1076,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 9a951be

Please sign in to comment.