Skip to content
This repository has been archived by the owner on Jan 21, 2023. It is now read-only.

motecshine/kdb

Repository files navigation

KDB

SkipList Arena 内存对齐问题

场景

假设我们有一结构体长下面这样:

const (
	maxHeight      = 20
	heightIncrease = math.MaxUint32 / 3
)

const (
	offsetSize = int(unsafe.Sizeof(uint32(0)))
	nodeAlign = int(unsafe.Sizeof(uint64(0))) - 1
	MaxNodeSize = int(unsafe.Sizeof(node{}))
)

type node struct {
	
	value uint64

	keyOffset uint32 // Immutable. No need to lock to access key.
	keySize   uint16 // Immutable. No need to lock to access key.

	height uint16

	tower [maxHeight]uint32
}

func putNode(height int) uint32 {
	// Compute the amount of the tower that will never be used, since the height
	// is less than maxHeight.
	// 假设新node 的高度10
	// unusedSize = (20 - 10) * 4 = 40
	// 假设新node 的高度3
	// unusedSize = (20 - 3) * 4 = 68
	// 76
	unusedSize := (maxHeight - height) * offsetSize

	// Pad the allocation with enough bytes to ensure pointer alignment.
	// 	假设新node 的高度10
	//   96 - 40 + 7  = 63
	// 	 假设新node 的高度3
	//   96 - 68 + 7  = 35
	// MaxNodeSize - unusedSize  是tower[height] 要使用的 size 。
	l := uint32(MaxNodeSize - unusedSize + nodeAlign)
	n := s.allocate(l)

	// Return the aligned offset.

	m := (n + uint32(nodeAlign)) & ^uint32(nodeAlign)
	return m
}

Special thanks

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages