coocood/freecache

Segment alignment maybe not effect

SsrCoder opened this issue · 3 comments

I think the segment alignment maybe not effect.

When I deleted the padding code _ uint32, I got the smaller obj then before.

here is my test code and the result on play.golang.org website:

package main

import (
	"fmt"
	"unsafe"
	"runtime"
)

// Ring buffer has a fixed size, when data exceeds the
// size, old data will be overwritten by new data.
// It only contains the data in the stream from begin to end
type RingBuf struct {
	begin int64 // beginning offset of the data stream.
	end   int64 // ending offset of the data stream.
	data  []byte
	index int //range from '0' to 'len(rb.data)-1'
}

// entry pointer struct points to an entry in ring buffer
type entryPtr struct {
	offset   int64  // entry offset in ring buffer
	hash16   uint16 // entries are ordered by hash16 in a slot.
	keyLen   uint16 // used to compare a key
	reserved uint32
}

// a segment contains 256 slots, a slot is an array of entry pointers ordered by hash16 value
// the entry can be looked up by hash value of the key.
type segment struct {
	rb            RingBuf // ring buffer that stores data
	segId         int
	_             uint32
	missCount     int64
	hitCount      int64
	entryCount    int64
	totalCount    int64      // number of entries in ring buffer, including deleted entries.
	totalTime     int64      // used to calculate least recent used entry.
	totalEvacuate int64      // used for debug
	totalExpired  int64      // used for debug
	overwrites    int64      // used for debug
	vacuumLen     int64      // up to vacuumLen, new data can be written without overwriting old data.
	slotLens      [256]int32 // The actual length for every slot.
	slotCap       int32      // max number of entry pointers a slot can hold.
	slotsData     []entryPtr // shared by all 256 slots
}

type segmentWithoutAlignment struct {
	rb            RingBuf // ring buffer that stores data
	segId         int
	missCount     int64
	hitCount      int64
	entryCount    int64
	totalCount    int64      // number of entries in ring buffer, including deleted entries.
	totalTime     int64      // used to calculate least recent used entry.
	totalEvacuate int64      // used for debug
	totalExpired  int64      // used for debug
	overwrites    int64      // used for debug
	vacuumLen     int64      // up to vacuumLen, new data can be written without overwriting old data.
	slotLens      [256]int32 // The actual length for every slot.
	slotCap       int32      // max number of entry pointers a slot can hold.
	slotsData     []entryPtr // shared by all 256 slots
}

func main() {
	fmt.Println(runtime.GOOS, runtime.GOARCH)
	fmt.Println("sizeof segment: ", unsafe.Sizeof(segment{}))
	fmt.Println("sizeof segmentWithoutAlignment: ", unsafe.Sizeof(segmentWithoutAlignment{}))
}

result:

linux amd64
sizeof segment:  1192
sizeof segmentWithoutAlignment:  1184

The alignment is for 32bit systems where int is 32bit

The alignment is for 32bit systems where int is 32bit

I just running on centos 32bit. Here is the result

linux 386
sizeof segment:  1152
sizeof segmentWithoutAlignment:  1148

Have you checked out the PR that introduce this alignment?
#38