Skip to content

Commit

Permalink
arenaskl: remove node.allocSize field
Browse files Browse the repository at this point in the history
The field was added in c34894c and has been unused since c34894c.

The `node` struct remains the same size due to padding.

I found this while looking into cache misses in the memtable's arena-backed
skiplist and exploring ways (e.g. value separation, value + key suffix
separation) to improve memory locality. So far, I haven't found anything
that shows promise on microbenchmarks.
  • Loading branch information
nvanbenschoten committed Dec 10, 2024
1 parent 0f9bf63 commit ab9741a
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 10 deletions.
8 changes: 4 additions & 4 deletions internal/arenaskl/arena.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,27 +80,27 @@ func (a *Arena) Capacity() uint32 {
// If overflow is not 0, it also ensures that many bytes after the buffer are
// inside the arena (this is used for structures that are larger than the
// requested size but don't use those extra bytes).
func (a *Arena) alloc(size, alignment, overflow uint32) (uint32, uint32, error) {
func (a *Arena) alloc(size, alignment, overflow uint32) (uint32, error) {
if invariants.Enabled && (alignment&(alignment-1)) != 0 {
panic(errors.AssertionFailedf("invalid alignment %d", alignment))
}
// Verify that the arena isn't already full.
origSize := a.n.Load()
if int(origSize) > len(a.buf) {
return 0, 0, ErrArenaFull
return 0, ErrArenaFull
}

// Pad the allocation with enough bytes to ensure the requested alignment.
padded := uint64(size) + uint64(alignment) - 1

newSize := a.n.Add(padded)
if newSize+uint64(overflow) > uint64(len(a.buf)) {
return 0, 0, ErrArenaFull
return 0, ErrArenaFull
}

// Return the aligned offset.
offset := (uint32(newSize) - size) & ^(alignment - 1)
return offset, uint32(padded), nil
return offset, nil
}

func (a *Arena) getBytes(offset uint32, size uint32) []byte {
Expand Down
6 changes: 3 additions & 3 deletions internal/arenaskl/arena_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,19 @@ func TestArenaSizeOverflow(t *testing.T) {
a := newArena(constants.MaxUint32OrInt)

// Allocating under the limit throws no error.
offset, _, err := a.alloc(math.MaxUint16, 1, 0)
offset, err := a.alloc(math.MaxUint16, 1, 0)
require.Nil(t, err)
require.Equal(t, uint32(1), offset)
require.Equal(t, uint32(math.MaxUint16)+1, a.Size())

// Allocating over the limit could cause an accounting
// overflow if 32-bit arithmetic was used. It shouldn't.
_, _, err = a.alloc(math.MaxUint32, 1, 0)
_, err = a.alloc(math.MaxUint32, 1, 0)
require.Equal(t, ErrArenaFull, err)
require.Equal(t, uint32(constants.MaxUint32OrInt), a.Size())

// Continuing to allocate continues to throw an error.
_, _, err = a.alloc(math.MaxUint16, 1, 0)
_, err = a.alloc(math.MaxUint16, 1, 0)
require.Equal(t, ErrArenaFull, err)
require.Equal(t, uint32(constants.MaxUint32OrInt), a.Size())
}
10 changes: 7 additions & 3 deletions internal/arenaskl/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,12 @@ type node struct {
keySize uint32
keyTrailer base.InternalKeyTrailer
valueSize uint32
allocSize uint32

// Padding to align tower on an 8-byte boundary, so that 32-bit and 64-bit
// architectures use the same memory layout for node. Needed for tests which
// expect a certain struct size. The padding can be removed if we add or
// remove a field from the node.
_ [4]byte

// Most nodes do not need to use the full height of the tower, since the
// probability of each successive level decreases exponentially. Because
Expand Down Expand Up @@ -95,7 +100,7 @@ func newRawNode(arena *Arena, height uint32, keySize, valueSize uint32) (nd *nod
unusedSize := uint32((maxHeight - int(height)) * linksSize)
nodeSize := uint32(maxNodeSize) - unusedSize

nodeOffset, allocSize, err := arena.alloc(nodeSize+keySize+valueSize, nodeAlignment, unusedSize)
nodeOffset, err := arena.alloc(nodeSize+keySize+valueSize, nodeAlignment, unusedSize)
if err != nil {
return
}
Expand All @@ -104,7 +109,6 @@ func newRawNode(arena *Arena, height uint32, keySize, valueSize uint32) (nd *nod
nd.keyOffset = nodeOffset + nodeSize
nd.keySize = keySize
nd.valueSize = valueSize
nd.allocSize = allocSize
return
}

Expand Down

0 comments on commit ab9741a

Please sign in to comment.