Skip to content

Commit

Permalink
Pass node allocators by pointers (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
outofforest authored Oct 18, 2024
1 parent cbe605e commit db5f899
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 35 deletions.
4 changes: 2 additions & 2 deletions alloc/allocator.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ func NewSnapshotAllocator(
allocator types.Allocator,
deallocationListCache map[types.SnapshotID]ListToCommit,
availableSnapshots map[types.SnapshotID]struct{},
listNodeAllocator list.NodeAllocator,
listNodeAllocator *list.NodeAllocator,
) *SnapshotAllocator {
sa := &SnapshotAllocator{
allocator: allocator,
Expand All @@ -166,7 +166,7 @@ type SnapshotAllocator struct {
immediateAllocator types.SnapshotAllocator
deallocationListCache map[types.SnapshotID]ListToCommit
availableSnapshots map[types.SnapshotID]struct{}
listNodeAllocator list.NodeAllocator
listNodeAllocator *list.NodeAllocator
}

// SnapshotID returns snapshot ID.
Expand Down
2 changes: 1 addition & 1 deletion benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
func BenchmarkBalanceTransfer(b *testing.B) {
const (
spaceID = 0x00
numOfAddresses = 50_000_000
numOfAddresses = 10_000_000
txsPerCommit = 2000
balance = 100_000
)
Expand Down
10 changes: 5 additions & 5 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,11 @@ type DB struct {
spaces *space.Space[types.SpaceID, types.SpaceInfo]
deallocationLists *space.Space[types.SnapshotID, types.NodeAddress]

pointerNodeAllocator space.NodeAllocator[types.Pointer]
snapshotInfoNodeAllocator space.NodeAllocator[types.DataItem[types.SnapshotID, types.SnapshotInfo]]
spaceInfoNodeAllocator space.NodeAllocator[types.DataItem[types.SpaceID, types.SpaceInfo]]
snapshotToNodeNodeAllocator space.NodeAllocator[types.DataItem[types.SnapshotID, types.NodeAddress]]
listNodeAllocator list.NodeAllocator
pointerNodeAllocator *space.NodeAllocator[types.Pointer]
snapshotInfoNodeAllocator *space.NodeAllocator[types.DataItem[types.SnapshotID, types.SnapshotInfo]]
spaceInfoNodeAllocator *space.NodeAllocator[types.DataItem[types.SpaceID, types.SpaceInfo]]
snapshotToNodeNodeAllocator *space.NodeAllocator[types.DataItem[types.SnapshotID, types.NodeAddress]]
listNodeAllocator *list.NodeAllocator

spacesToCommit map[types.SpaceID]SpaceToCommit
deallocationListsToCommit map[types.SnapshotID]alloc.ListToCommit
Expand Down
16 changes: 8 additions & 8 deletions list/alloc.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,21 @@ import (
)

// NewNodeAllocator creates new list node allocator.
func NewNodeAllocator(allocator types.Allocator) (NodeAllocator, error) {
func NewNodeAllocator(allocator types.Allocator) (*NodeAllocator, error) {
nodeSize := allocator.NodeSize()

headerSize := uint64(unsafe.Sizeof(types.ListNodeHeader{}))
headerSize = (headerSize + types.UInt64Length - 1) / types.UInt64Length * types.UInt64Length // memory alignment
if headerSize >= nodeSize {
return NodeAllocator{}, errors.New("node size is too small")
return nil, errors.New("node size is too small")
}

numOfItems := (nodeSize - headerSize) / types.UInt64Length
if numOfItems < 2 {
return NodeAllocator{}, errors.New("node size is too small")
return nil, errors.New("node size is too small")
}

return NodeAllocator{
return &NodeAllocator{
allocator: allocator,
numOfItems: int(numOfItems),
itemOffset: headerSize,
Expand All @@ -40,12 +40,12 @@ type NodeAllocator struct {
}

// Get returns object for node.
func (na NodeAllocator) Get(nodeAddress types.NodeAddress) types.ListNode {
func (na *NodeAllocator) Get(nodeAddress types.NodeAddress) types.ListNode {
return na.project(na.allocator.Node(nodeAddress))
}

// Allocate allocates new object.
func (na NodeAllocator) Allocate(allocator types.SnapshotAllocator) (types.NodeAddress, types.ListNode, error) {
func (na *NodeAllocator) Allocate(allocator types.SnapshotAllocator) (types.NodeAddress, types.ListNode, error) {
n, node, err := allocator.Allocate()
if err != nil {
return 0, types.ListNode{}, err
Expand All @@ -54,7 +54,7 @@ func (na NodeAllocator) Allocate(allocator types.SnapshotAllocator) (types.NodeA
}

// Copy allocates copy of existing object.
func (na NodeAllocator) Copy(
func (na *NodeAllocator) Copy(
allocator types.SnapshotAllocator,
nodeAddress types.NodeAddress,
) (types.NodeAddress, types.ListNode, error) {
Expand All @@ -65,7 +65,7 @@ func (na NodeAllocator) Copy(
return n, na.project(node), nil
}

func (na NodeAllocator) project(node unsafe.Pointer) types.ListNode {
func (na *NodeAllocator) project(node unsafe.Pointer) types.ListNode {
return types.ListNode{
Header: photon.FromPointer[types.ListNodeHeader](node),
Items: photon.SliceFromPointer[types.NodeAddress](unsafe.Add(node, na.itemOffset), na.numOfItems),
Expand Down
2 changes: 1 addition & 1 deletion list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
// Config stores list configuration.
type Config struct {
Item *types.NodeAddress
NodeAllocator NodeAllocator
NodeAllocator *NodeAllocator
Allocator types.SnapshotAllocator
}

Expand Down
2 changes: 1 addition & 1 deletion list/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ type env struct {

snapshotID types.SnapshotID
snapshotAllocator types.SnapshotAllocator
nodeAllocator list.NodeAllocator
nodeAllocator *list.NodeAllocator
}

func (e *env) NextSnapshot() {
Expand Down
22 changes: 11 additions & 11 deletions space/alloc.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ import (
)

// NewNodeAllocator creates new space node allocator.
func NewNodeAllocator[T comparable](allocator types.Allocator) (NodeAllocator[T], error) {
func NewNodeAllocator[T comparable](allocator types.Allocator) (*NodeAllocator[T], error) {
nodeSize := allocator.NodeSize()

headerSize := uint64(unsafe.Sizeof(types.SpaceNodeHeader{}))
headerSize = (headerSize + types.UInt64Length - 1) / types.UInt64Length * types.UInt64Length // memory alignment
if headerSize >= allocator.NodeSize() {
return NodeAllocator[T]{}, errors.New("node size is too small")
return nil, errors.New("node size is too small")
}

spaceLeft := nodeSize - headerSize
Expand All @@ -27,17 +27,17 @@ func NewNodeAllocator[T comparable](allocator types.Allocator) (NodeAllocator[T]

numOfItems := spaceLeft / (itemSize + 1) // 1 is for slot state
if numOfItems == 0 {
return NodeAllocator[T]{}, errors.New("node size is too small")
return nil, errors.New("node size is too small")
}
stateSize := (numOfItems + types.UInt64Length - 1) / types.UInt64Length * types.UInt64Length
spaceLeft -= stateSize

numOfItems = spaceLeft / itemSize
if numOfItems == 0 {
return NodeAllocator[T]{}, errors.New("node size is too small")
return nil, errors.New("node size is too small")
}

return NodeAllocator[T]{
return &NodeAllocator[T]{
allocator: allocator,
itemSize: itemSize,
numOfItems: int(numOfItems),
Expand All @@ -57,12 +57,12 @@ type NodeAllocator[T comparable] struct {
}

// Get returns object for node.
func (na NodeAllocator[T]) Get(nodeAddress types.NodeAddress) types.SpaceNode[T] {
func (na *NodeAllocator[T]) Get(nodeAddress types.NodeAddress) types.SpaceNode[T] {
return na.project(na.allocator.Node(nodeAddress))
}

// Allocate allocates new object.
func (na NodeAllocator[T]) Allocate(allocator types.SnapshotAllocator) (types.NodeAddress, types.SpaceNode[T], error) {
func (na *NodeAllocator[T]) Allocate(allocator types.SnapshotAllocator) (types.NodeAddress, types.SpaceNode[T], error) {
n, node, err := allocator.Allocate()
if err != nil {
return 0, types.SpaceNode[T]{}, err
Expand All @@ -71,7 +71,7 @@ func (na NodeAllocator[T]) Allocate(allocator types.SnapshotAllocator) (types.No
}

// Copy allocates copy of existing object.
func (na NodeAllocator[T]) Copy(
func (na *NodeAllocator[T]) Copy(
allocator types.SnapshotAllocator,
nodeAddress types.NodeAddress,
) (types.NodeAddress, types.SpaceNode[T], error) {
Expand All @@ -83,16 +83,16 @@ func (na NodeAllocator[T]) Copy(
}

// Index returns element index based on hash.
func (na NodeAllocator[T]) Index(hash types.Hash) uint64 {
func (na *NodeAllocator[T]) Index(hash types.Hash) uint64 {
return uint64(hash) % uint64(na.numOfItems)
}

// Shift shifts bits in hash.
func (na NodeAllocator[T]) Shift(hash types.Hash) types.Hash {
func (na *NodeAllocator[T]) Shift(hash types.Hash) types.Hash {
return hash / types.Hash(na.numOfItems)
}

func (na NodeAllocator[T]) project(node unsafe.Pointer) types.SpaceNode[T] {
func (na *NodeAllocator[T]) project(node unsafe.Pointer) types.SpaceNode[T] {
return types.SpaceNode[T]{
Header: photon.FromPointer[types.SpaceNodeHeader](node),
States: photon.SliceFromPointer[types.State](unsafe.Add(node, na.stateOffset), na.numOfItems),
Expand Down
4 changes: 2 additions & 2 deletions space/space.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ const trials = 50
type Config[K, V comparable] struct {
HashMod *uint64
SpaceRoot types.ParentInfo
PointerNodeAllocator NodeAllocator[types.Pointer]
DataNodeAllocator NodeAllocator[types.DataItem[K, V]]
PointerNodeAllocator *NodeAllocator[types.Pointer]
DataNodeAllocator *NodeAllocator[types.DataItem[K, V]]
Allocator types.SnapshotAllocator
}

Expand Down
7 changes: 3 additions & 4 deletions space/space_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"github.com/stretchr/testify/require"

"github.com/outofforest/quantum/alloc"
"github.com/outofforest/quantum/list"
"github.com/outofforest/quantum/space"
"github.com/outofforest/quantum/test"
"github.com/outofforest/quantum/types"
Expand Down Expand Up @@ -454,7 +453,7 @@ func newEnv(requireT *require.Assertions) *env {
allocator,
map[types.SnapshotID]alloc.ListToCommit{},
map[types.SnapshotID]struct{}{},
list.NodeAllocator{},
nil,
)),
spaceRoot: types.ParentInfo{
State: lo.ToPtr(types.StateFree),
Expand Down Expand Up @@ -484,8 +483,8 @@ type env struct {
snapshotID types.SnapshotID
spaceRoot types.ParentInfo
spaceHashMod *uint64
pointerNodeAllocator space.NodeAllocator[types.Pointer]
dataNodeAllocator space.NodeAllocator[types.DataItem[int, int]]
pointerNodeAllocator *space.NodeAllocator[types.Pointer]
dataNodeAllocator *space.NodeAllocator[types.DataItem[int, int]]
}

func (e *env) NextSnapshot() {
Expand Down

0 comments on commit db5f899

Please sign in to comment.