Skip to content

Commit

Permalink
Remove unsafe usage
Browse files Browse the repository at this point in the history
  • Loading branch information
akrylysov committed Apr 4, 2020
1 parent 359a3e1 commit e7fefaf
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 19 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## [0.9.1] - 2020-04-03
## Changed
- Improve Go 1.14 compatibility (remove "unsafe" usage).

## [0.9.0] - 2020-03-08
## Changed
- Replace the unstructured data file for storing key-value pairs with a write-ahead log.
Expand Down
32 changes: 13 additions & 19 deletions internal/hash/murmurhash32.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package hash

import (
"unsafe"
"math/bits"
)

const (
Expand All @@ -12,44 +12,38 @@ const (
// Sum32WithSeed is a port of MurmurHash3_x86_32 function.
func Sum32WithSeed(data []byte, seed uint32) uint32 {
h1 := seed
dlen := len(data)

nblocks := len(data) / 4
var p uintptr
if len(data) > 0 {
p = uintptr(unsafe.Pointer(&data[0]))
}
p1 := p + uintptr(4*nblocks)
for ; p < p1; p += 4 {
k1 := *(*uint32)(unsafe.Pointer(p))
for len(data) >= 4 {
k1 := uint32(data[0]) | uint32(data[1])<<8 | uint32(data[2])<<16 | uint32(data[3])<<24
data = data[4:]

k1 *= c1
k1 = (k1 << 15) | (k1 >> 17) // rotl32(k1, 15)
k1 = bits.RotateLeft32(k1, 15)
k1 *= c2

h1 ^= k1
h1 = (h1 << 13) | (h1 >> 19) // rotl32(h1, 13)
h1 = bits.RotateLeft32(h1, 13)
h1 = h1*5 + 0xe6546b64
}

tail := data[nblocks*4:]

var k1 uint32
switch len(tail) & 3 {
switch len(data) {
case 3:
k1 ^= uint32(tail[2]) << 16
k1 ^= uint32(data[2]) << 16
fallthrough
case 2:
k1 ^= uint32(tail[1]) << 8
k1 ^= uint32(data[1]) << 8
fallthrough
case 1:
k1 ^= uint32(tail[0])
k1 ^= uint32(data[0])
k1 *= c1
k1 = (k1 << 15) | (k1 >> 17) // rotl32(k1, 15)
k1 = bits.RotateLeft32(k1, 15)
k1 *= c2
h1 ^= k1
}

h1 ^= uint32(len(data))
h1 ^= uint32(dlen)

h1 ^= h1 >> 16
h1 *= 0x85ebca6b
Expand Down
22 changes: 22 additions & 0 deletions internal/hash/murmurhash32_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package hash

import (
"testing"
)

func TestSum32WithSeed(t *testing.T) {
// TODO: test all code paths
actual := Sum32WithSeed([]byte{1, 2, 3, 4, 5}, 0)
expected := uint32(2727459272)
if actual != expected {
t.Fatalf("expected: %d, actual: %d", expected, actual)
}
}

func BenchmarkSum32WithSeed(b *testing.B) {
data := []byte("pogreb_Sum32WithSeed_bench")
b.SetBytes(int64(len(data)))
for n := 0; n < b.N; n++ {
Sum32WithSeed(data, 0)
}
}

0 comments on commit e7fefaf

Please sign in to comment.