Skip to content

Commit

Permalink
Don't copy value (#221)
Browse files Browse the repository at this point in the history
  • Loading branch information
outofforest authored Nov 21, 2024
1 parent 10aac0e commit a037a97
Showing 1 changed file with 15 additions and 19 deletions.
34 changes: 15 additions & 19 deletions space/space.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func (s *Space[K, V]) Find(key K, hashBuff []byte, hashMatches []uint64) *Entry[
initBytes := unsafe.Slice((*byte)(unsafe.Pointer(v)), s.initSize)
copy(initBytes, s.defaultInit)
v.keyHash = hashKey(&key, nil, 0)
v.item.Key = key
v.key = key
v.dataItemIndex = dataItemIndex(v.keyHash, s.numOfDataItems)

s.find(v, hashBuff, hashMatches)
Expand Down Expand Up @@ -210,7 +210,11 @@ func (s *Space[K, V]) readValue(v *Entry[K, V], hashBuff []byte, hashMatches []u

s.find(v, hashBuff, hashMatches)

return v.item.Value
if v.keyHashP == nil || *v.keyHashP == 0 {
return s.defaultValue
}

return v.itemP.Value
}

func (s *Space[K, V]) deleteValue(
Expand Down Expand Up @@ -245,7 +249,7 @@ func (s *Space[K, V]) setValue(
hashBuff []byte,
hashMatches []uint64,
) error {
v.item.Value = value
v.value = value

detectUpdate(v)

Expand All @@ -259,19 +263,11 @@ func (s *Space[K, V]) find(v *Entry[K, V], hashBuff []byte, hashMatches []uint64
v.keyHashP = nil
v.itemP = nil
v.exists = false
v.item.Value = s.defaultValue

return
}

s.walkDataItems(v, hashMatches)

if v.keyHashP != nil && *v.keyHashP != 0 {
v.item.Value = v.itemP.Value
return
}

v.item.Value = s.defaultValue
}

func (s *Space[K, V]) set(
Expand Down Expand Up @@ -300,10 +296,9 @@ func (s *Space[K, V]) set(
if v.keyHashP != nil {
if *v.keyHashP == 0 {
*v.keyHashP = v.keyHash
*v.itemP = v.item
} else {
v.itemP.Value = v.item.Value
v.itemP.Key = v.key
}
v.itemP.Value = v.value

tx.AddStoreRequest(&v.storeRequest)

Expand Down Expand Up @@ -531,7 +526,7 @@ var pointerHops = [NumOfPointers][]uint64{
func (s *Space[K, V]) walkPointers(v *Entry[K, V], hashBuff []byte) {
for v.storeRequest.Store[v.storeRequest.PointersToStore-1].Pointer.State == types.StatePointer {
if v.storeRequest.Store[v.storeRequest.PointersToStore-1].Pointer.Flags.IsSet(types.FlagHashMod) {
v.keyHash = hashKey(&v.item.Key, hashBuff, v.level)
v.keyHash = hashKey(&v.key, hashBuff, v.level)
}

pointerNode := ProjectPointerNode(s.config.State.Node(
Expand Down Expand Up @@ -612,7 +607,7 @@ func (s *Space[K, V]) walkDataItems(v *Entry[K, V], hashMatches []uint64) bool {
for i := range numOfMatches {
index := hashMatches[i]
item := s.config.DataNodeAssistant.Item(node, s.config.DataNodeAssistant.ItemOffset(index))
if item.Key == v.item.Key {
if item.Key == v.key {
v.exists = true
v.keyHashP = &keyHashes[index]
v.itemP = item
Expand All @@ -639,8 +634,9 @@ type Entry[K, V comparable] struct {

itemP *types.DataItem[K, V]
keyHashP *types.KeyHash
item types.DataItem[K, V]
keyHash types.KeyHash
key K
value V
nextDataNodeState *types.State
parentIndex uint64
dataItemIndex uint64
Expand All @@ -655,7 +651,7 @@ func (v *Entry[K, V]) Value(hashBuff []byte, hashMatches []uint64) V {

// Key returns the key from entry.
func (v *Entry[K, V]) Key() K {
return v.item.Key
return v.key
}

// Exists returns true if entry exists in the space.
Expand Down Expand Up @@ -723,7 +719,7 @@ func detectUpdate[K, V comparable](v *Entry[K, V]) {
v.keyHashP = nil
v.itemP = nil
case v.keyHashP != nil && (pointer.State != types.StateData ||
(*v.keyHashP != 0 && (*v.keyHashP != v.keyHash || v.itemP.Key != v.item.Key))):
(*v.keyHashP != 0 && (*v.keyHashP != v.keyHash || v.itemP.Key != v.key))):
v.keyHashP = nil
v.itemP = nil
}
Expand Down

0 comments on commit a037a97

Please sign in to comment.