Skip to content

Commit

Permalink
[feat:lindb#59][pkg:hashers]: allocate-free Fnv-1a
Browse files Browse the repository at this point in the history
  • Loading branch information
CodingCrush committed Jul 5, 2019
1 parent c20838f commit f569bdc
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 24 deletions.
31 changes: 31 additions & 0 deletions pkg/hashers/fnv.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package hashers

import (
"unsafe"
)

// copy from hash/fnv/fnv.go
const (
offset32 uint32 = 2166136261
offset64 uint64 = 14695981039346656037
prime32 uint32 = 16777619
prime64 uint64 = 1099511628211
)

// Fnv32a returns a 32-bit FNV-1a hash of a string.
func Fnv32a(s string) uint32 {
hash := offset32
for _, b := range *(*[]byte)(unsafe.Pointer(&s)) {
hash = (hash ^ uint32(b)) * prime32
}
return hash
}

// Fnv64a returns a 64-bit FNV-1a hash of a string.
func Fnv64a(s string) uint64 {
hash := offset64
for _, b := range *(*[]byte)(unsafe.Pointer(&s)) {
hash = (hash ^ uint64(b)) * prime64
}
return hash
}
59 changes: 59 additions & 0 deletions pkg/hashers/fnv_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package hashers

import (
"hash/fnv"
"strconv"
"testing"

"github.com/bmizerany/assert"
)

var _testString = "abcdefghijklmnopqrstuvwxyz"

func Test_Fnv32a(t *testing.T) {
for i := 0; i < 100; i++ {
assert.Equal(t, Fnv32a(strconv.Itoa(i)), fnv32a(strconv.Itoa(i)))
}
}

func Test_Fnv64a(t *testing.T) {
for i := 0; i < 100; i++ {
assert.Equal(t, Fnv64a(strconv.Itoa(i)), fnv64a(strconv.Itoa(i)))
}
}

func Benchmark_Allocate_Free_FNV32a(b *testing.B) {
for i := 0; i < b.N; i++ {
Fnv32a(_testString)
}
}

func Benchmark_fnv32a(b *testing.B) {
for i := 0; i < b.N; i++ {
fnv32a(_testString)
}
}

func Benchmark_Allocate_Free_FNV64(b *testing.B) {
for i := 0; i < b.N; i++ {
Fnv64a(_testString)
}
}

func Benchmark_fnv64a(b *testing.B) {
for i := 0; i < b.N; i++ {
fnv64a(_testString)
}
}

func fnv32a(s string) uint32 {
h := fnv.New32a()
_, _ = h.Write([]byte(s))
return h.Sum32()
}

func fnv64a(s string) (hash uint64) {
h := fnv.New64a()
_, _ = h.Write([]byte(s))
return h.Sum64()
}
21 changes: 0 additions & 21 deletions pkg/util/fnv.go

This file was deleted.

6 changes: 3 additions & 3 deletions tsdb/memdb/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import (
"sync"
"time"

"github.com/eleme/lindb/pkg/hashers"

radix "github.com/armon/go-radix"

"github.com/eleme/lindb/models"

"github.com/eleme/lindb/pkg/util"
)

// MemoryDatabase is a database-like concept of Shard as memTable in cassandra.
Expand Down Expand Up @@ -69,7 +69,7 @@ func newMemoryDatabase(ctx context.Context) *memoryDatabase {

// getBucket returns the mStoresBucket by measurement-name.
func (md *memoryDatabase) getBucket(measurement string) *mStoresBucket {
return md.mStoresList[shardingCountMask&util.Fnv32(measurement)]
return md.mStoresList[shardingCountMask&hashers.Fnv32a(measurement)]
}

// getMeasurementStore returns a TimeSeriesStore by measurement + tags.
Expand Down

0 comments on commit f569bdc

Please sign in to comment.