Skip to content

Commit

Permalink
chore: use camel case instead of snake case (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
godruoyi authored Nov 5, 2024
1 parent 5e177d7 commit 4f4522d
Show file tree
Hide file tree
Showing 18 changed files with 232 additions and 240 deletions.
20 changes: 10 additions & 10 deletions bytes_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,35 +28,35 @@ func NewBytesCache(shards uint8, shardsize uint32) *BytesCache {

// Get returns value for key.
func (c *BytesCache) Get(key []byte) (value []byte, ok bool) {
hash := uint32(wyhash_HashBytes(key, 0))
hash := uint32(wyhashHashbytes(key, 0))
// return c.shards[hash&c.mask].Get(hash, key)
return (*bytesshard)(unsafe.Add(unsafe.Pointer(&c.shards[0]), uintptr(hash&c.mask)*unsafe.Sizeof(c.shards[0]))).Get(hash, key)
}

// Peek returns value, but does not modify its recency.
func (c *BytesCache) Peek(key []byte) (value []byte, ok bool) {
hash := uint32(wyhash_HashBytes(key, 0))
hash := uint32(wyhashHashbytes(key, 0))
// return c.shards[hash&c.mask].Peek(hash, key)
return (*bytesshard)(unsafe.Add(unsafe.Pointer(&c.shards[0]), uintptr(hash&c.mask)*unsafe.Sizeof(c.shards[0]))).Peek(hash, key)
}

// Set inserts key value pair and returns previous value.
func (c *BytesCache) Set(key []byte, value []byte) (prev []byte, replaced bool) {
hash := uint32(wyhash_HashBytes(key, 0))
hash := uint32(wyhashHashbytes(key, 0))
// return c.shards[hash&c.mask].Set(hash, key, value)
return (*bytesshard)(unsafe.Add(unsafe.Pointer(&c.shards[0]), uintptr(hash&c.mask)*unsafe.Sizeof(c.shards[0]))).Set(hash, key, value)
}

// SetIfAbsent inserts key value pair and returns previous value, if key is absent in the cache.
func (c *BytesCache) SetIfAbsent(key []byte, value []byte) (prev []byte, replaced bool) {
hash := uint32(wyhash_HashBytes(key, 0))
hash := uint32(wyhashHashbytes(key, 0))
// return c.shards[hash&c.mask].SetIfAbsent(hash, key, value)
return (*bytesshard)(unsafe.Add(unsafe.Pointer(&c.shards[0]), uintptr(hash&c.mask)*unsafe.Sizeof(c.shards[0]))).SetIfAbsent(hash, key, value)
}

// Delete method deletes value associated with key and returns deleted value (or empty value if key was not in cache).
func (c *BytesCache) Delete(key []byte) (prev []byte) {
hash := uint32(wyhash_HashBytes(key, 0))
hash := uint32(wyhashHashbytes(key, 0))
// return c.shards[hash&c.mask].Delete(hash, key)
return (*bytesshard)(unsafe.Add(unsafe.Pointer(&c.shards[0]), uintptr(hash&c.mask)*unsafe.Sizeof(c.shards[0]))).Delete(hash, key)
}
Expand All @@ -83,16 +83,16 @@ func (c *BytesCache) Stats() (stats Stats) {
for i := uint32(0); i <= c.mask; i++ {
s := &c.shards[i]
s.mu.Lock()
stats.EntriesCount += uint64(s.table_length)
stats.GetCalls += s.stats_getcalls
stats.SetCalls += s.stats_setcalls
stats.Misses += s.stats_misses
stats.EntriesCount += uint64(s.tableLength)
stats.GetCalls += s.statsGetCalls
stats.SetCalls += s.statsSetCalls
stats.Misses += s.statsMisses
s.mu.Unlock()
}
return
}

func wyhash_HashBytes(data []byte, seed uint64) uint64 {
func wyhashHashbytes(data []byte, seed uint64) uint64 {
if len(data) == 0 {
return seed
}
Expand Down
2 changes: 1 addition & 1 deletion bytes_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"testing"
)

func TestBytesCacheDefaultkey(t *testing.T) {
func TestBytesCacheDefaultKey(t *testing.T) {
cache := NewBytesCache(1, 1)
var k []byte
var i = []byte("42")
Expand Down
62 changes: 31 additions & 31 deletions bytes_shard.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,44 +20,44 @@ type bytesbucket struct {
index uint32 // node index
}

// bytesshard is a LRU partition contains a list and a hash table.
// bytesshard is an LRU partition contains a list and a hash table.
type bytesshard struct {
mu sync.Mutex

// the hash table, with 20% extra space than the list for fewer conflicts.
table_buckets []uint64 // []bytesbucket
table_mask uint32
table_length uint32
// the hash table, with 20% extra spacer than the list for fewer conflicts.
tableBuckets []uint64 // []bytesbucket
tableMask uint32
tableLength uint32

// the list of nodes
list []bytesnode

// stats
stats_getcalls uint64
stats_setcalls uint64
stats_misses uint64
statsGetCalls uint64
statsSetCalls uint64
statsMisses uint64

// padding
_ [40]byte
}

func (s *bytesshard) Init(size uint32) {
s.list_Init(size)
s.table_Init(size)
s.listInit(size)
s.tableInit(size)
}

func (s *bytesshard) Get(hash uint32, key []byte) (value []byte, ok bool) {
s.mu.Lock()

s.stats_getcalls++
s.statsGetCalls++

if index, exists := s.table_Get(hash, key); exists {
s.list_MoveToFront(index)
if index, exists := s.tableGet(hash, key); exists {
s.listMoveToFront(index)
// value = s.list[index].value
value = (*bytesnode)(unsafe.Add(unsafe.Pointer(&s.list[0]), uintptr(index)*unsafe.Sizeof(s.list[0]))).value
ok = true
} else {
s.stats_misses++
s.statsMisses++
}

s.mu.Unlock()
Expand All @@ -68,7 +68,7 @@ func (s *bytesshard) Get(hash uint32, key []byte) (value []byte, ok bool) {
func (s *bytesshard) Peek(hash uint32, key []byte) (value []byte, ok bool) {
s.mu.Lock()

if index, exists := s.table_Get(hash, key); exists {
if index, exists := s.tableGet(hash, key); exists {
value = s.list[index].value
ok = true
}
Expand All @@ -81,25 +81,25 @@ func (s *bytesshard) Peek(hash uint32, key []byte) (value []byte, ok bool) {
func (s *bytesshard) SetIfAbsent(hash uint32, key []byte, value []byte) (prev []byte, replaced bool) {
s.mu.Lock()

if index, exists := s.table_Get(hash, key); exists {
if index, exists := s.tableGet(hash, key); exists {
prev = s.list[index].value
s.mu.Unlock()
return
}

s.stats_setcalls++
s.statsSetCalls++

// index := s.list_Back()
// node := &s.list[index]
index := s.list[0].prev
node := (*bytesnode)(unsafe.Add(unsafe.Pointer(&s.list[0]), uintptr(index)*unsafe.Sizeof(s.list[0])))
evictedValue := node.value
s.table_Delete(uint32(wyhash_HashBytes(node.key, 0)), node.key)
s.tableDelete(uint32(wyhashHashbytes(node.key, 0)), node.key)

node.key = key
node.value = value
s.table_Set(hash, key, index)
s.list_MoveToFront(index)
s.tableSet(hash, key, index)
s.listMoveToFront(index)
prev = evictedValue

s.mu.Unlock()
Expand All @@ -109,13 +109,13 @@ func (s *bytesshard) SetIfAbsent(hash uint32, key []byte, value []byte) (prev []
func (s *bytesshard) Set(hash uint32, key []byte, value []byte) (prev []byte, replaced bool) {
s.mu.Lock()

s.stats_setcalls++
s.statsSetCalls++

if index, exists := s.table_Get(hash, key); exists {
if index, exists := s.tableGet(hash, key); exists {
// node := &s.list[index]
node := (*bytesnode)(unsafe.Add(unsafe.Pointer(&s.list[0]), uintptr(index)*unsafe.Sizeof(s.list[0])))
previousValue := node.value
s.list_MoveToFront(index)
s.listMoveToFront(index)
node.value = value
prev = previousValue
replaced = true
Expand All @@ -129,12 +129,12 @@ func (s *bytesshard) Set(hash uint32, key []byte, value []byte) (prev []byte, re
index := s.list[0].prev
node := (*bytesnode)(unsafe.Add(unsafe.Pointer(&s.list[0]), uintptr(index)*unsafe.Sizeof(s.list[0])))
evictedValue := node.value
s.table_Delete(uint32(wyhash_HashBytes(node.key, 0)), node.key)
s.tableDelete(uint32(wyhashHashbytes(node.key, 0)), node.key)

node.key = key
node.value = value
s.table_Set(hash, key, index)
s.list_MoveToFront(index)
s.tableSet(hash, key, index)
s.listMoveToFront(index)
prev = evictedValue

s.mu.Unlock()
Expand All @@ -144,12 +144,12 @@ func (s *bytesshard) Set(hash uint32, key []byte, value []byte) (prev []byte, re
func (s *bytesshard) Delete(hash uint32, key []byte) (v []byte) {
s.mu.Lock()

if index, exists := s.table_Get(hash, key); exists {
if index, exists := s.tableGet(hash, key); exists {
node := &s.list[index]
value := node.value
s.list_MoveToBack(index)
s.listMoveToBack(index)
node.value = v
s.table_Delete(hash, key)
s.tableDelete(hash, key)
v = value
}

Expand All @@ -161,15 +161,15 @@ func (s *bytesshard) Delete(hash uint32, key []byte) (v []byte) {
func (s *bytesshard) Len() (n uint32) {
s.mu.Lock()
// inlining s.table_Len()
n = s.table_length
n = s.tableLength
s.mu.Unlock()

return
}

func (s *bytesshard) AppendKeys(dst [][]byte) [][]byte {
s.mu.Lock()
for _, bucket := range s.table_buckets {
for _, bucket := range s.tableBuckets {
b := (*bytesbucket)(unsafe.Pointer(&bucket))
if b.index == 0 {
continue
Expand Down
8 changes: 4 additions & 4 deletions bytes_shard_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"unsafe"
)

func (s *bytesshard) list_Init(size uint32) {
func (s *bytesshard) listInit(size uint32) {
size += 1
if len(s.list) == 0 {
s.list = make([]bytesnode, size)
Expand All @@ -17,11 +17,11 @@ func (s *bytesshard) list_Init(size uint32) {
}
}

func (s *bytesshard) list_Back() uint32 {
func (s *bytesshard) listBack() uint32 {
return s.list[0].prev
}

func (s *bytesshard) list_MoveToFront(i uint32) {
func (s *bytesshard) listMoveToFront(i uint32) {
root := &s.list[0]
if root.next == i {
return
Expand All @@ -40,7 +40,7 @@ func (s *bytesshard) list_MoveToFront(i uint32) {
((*bytesnode)(unsafe.Add(base, uintptr(nodei.next)*unsafe.Sizeof(s.list[0])))).prev = i
}

func (s *bytesshard) list_MoveToBack(i uint32) {
func (s *bytesshard) listMoveToBack(i uint32) {
j := s.list[0].prev
if i == j {
return
Expand Down
44 changes: 22 additions & 22 deletions bytes_shard_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ import (
"unsafe"
)

func (s *bytesshard) table_Init(size uint32) {
func (s *bytesshard) tableInit(size uint32) {
newsize := bytesNewTableSize(size)
if len(s.table_buckets) == 0 {
s.table_buckets = make([]uint64, newsize)
if len(s.tableBuckets) == 0 {
s.tableBuckets = make([]uint64, newsize)
}
s.table_mask = newsize - 1
s.table_length = 0
s.tableMask = newsize - 1
s.tableLength = 0
}

func bytesNewTableSize(size uint32) (newsize uint32) {
Expand All @@ -31,19 +31,19 @@ func bytesNewTableSize(size uint32) (newsize uint32) {

// Set assigns an index to a key.
// Returns the previous index, or false when no index was assigned.
func (s *bytesshard) table_Set(hash uint32, key []byte, index uint32) (prev uint32, ok bool) {
func (s *bytesshard) tableSet(hash uint32, key []byte, index uint32) (prev uint32, ok bool) {
subhash := hash >> dibBitSize
hdib := subhash<<dibBitSize | uint32(1)&maxDIB
mask := s.table_mask
mask := s.tableMask
i := (hdib >> dibBitSize) & mask
b0 := unsafe.Pointer(&s.table_buckets[0])
b0 := unsafe.Pointer(&s.tableBuckets[0])
l0 := unsafe.Pointer(&s.list[0])
for {
b := (*bytesbucket)(unsafe.Add(b0, uintptr(i)*8))
if b.hdib&maxDIB == 0 {
b.hdib = hdib
b.index = index
s.table_length++
s.tableLength++
return
}
if hdib>>dibBitSize == b.hdib>>dibBitSize && b2s((*bytesnode)(unsafe.Add(l0, uintptr(b.index)*unsafe.Sizeof(s.list[0]))).key) == b2s(key) {
Expand All @@ -62,13 +62,13 @@ func (s *bytesshard) table_Set(hash uint32, key []byte, index uint32) (prev uint
}
}

// table_Get returns an index for a key.
// tableGet returns an index for a key.
// Returns false when no index has been assign for key.
func (s *bytesshard) table_Get(hash uint32, key []byte) (index uint32, ok bool) {
func (s *bytesshard) tableGet(hash uint32, key []byte) (index uint32, ok bool) {
subhash := hash >> dibBitSize
mask := s.table_mask
mask := s.tableMask
i := subhash & mask
b0 := unsafe.Pointer(&s.table_buckets[0])
b0 := unsafe.Pointer(&s.tableBuckets[0])
l0 := unsafe.Pointer(&s.list[0])
for {
b := (*bytesbucket)(unsafe.Add(b0, uintptr(i)*8))
Expand All @@ -82,13 +82,13 @@ func (s *bytesshard) table_Get(hash uint32, key []byte) (index uint32, ok bool)
}
}

// table_Delete deletes an index for a key.
// tableDelete deletes an index for a key.
// Returns the deleted index, or false when no index was assigned.
func (s *bytesshard) table_Delete(hash uint32, key []byte) (index uint32, ok bool) {
func (s *bytesshard) tableDelete(hash uint32, key []byte) (index uint32, ok bool) {
subhash := hash >> dibBitSize
mask := s.table_mask
mask := s.tableMask
i := subhash & mask
b0 := unsafe.Pointer(&s.table_buckets[0])
b0 := unsafe.Pointer(&s.tableBuckets[0])
l0 := unsafe.Pointer(&s.list[0])
for {
b := (*bytesbucket)(unsafe.Add(b0, uintptr(i)*8))
Expand All @@ -97,16 +97,16 @@ func (s *bytesshard) table_Delete(hash uint32, key []byte) (index uint32, ok boo
}
if b.hdib>>dibBitSize == subhash && b2s((*bytesnode)(unsafe.Add(l0, uintptr(b.index)*unsafe.Sizeof(s.list[0]))).key) == b2s(key) {
old := b.index
s.table_delete(i)
s.tableDeleteByIndex(i)
return old, true
}
i = (i + 1) & mask
}
}

func (s *bytesshard) table_delete(i uint32) {
mask := s.table_mask
b0 := unsafe.Pointer(&s.table_buckets[0])
func (s *bytesshard) tableDeleteByIndex(i uint32) {
mask := s.tableMask
b0 := unsafe.Pointer(&s.tableBuckets[0])
bi := (*bytesbucket)(unsafe.Add(b0, uintptr(i)*8))
bi.hdib = bi.hdib>>dibBitSize<<dibBitSize | uint32(0)&maxDIB
for {
Expand All @@ -122,5 +122,5 @@ func (s *bytesshard) table_delete(i uint32) {
bpi.index = bi.index
bpi.hdib = bi.hdib>>dibBitSize<<dibBitSize | (bi.hdib&maxDIB-1)&maxDIB
}
s.table_length--
s.tableLength--
}
Loading

0 comments on commit 4f4522d

Please sign in to comment.