image-20220512210618415

缘起

来自于一段prometheus代码

type stripeLock struct { 
	sync.RWMutex
	// Padding to avoid multiple locks being on the same cache line.
	_ [40]byte
}

简单地讲就是因为CPU读取数据的缓存机制问题,可能导致性能上的不同差异。参考资料见后文。

常见类型的内存占用大小(Go101):

Kinds of TypesValue SizeRequired by Go Specification
bool1 bytenot specified
int8, uint8 (byte)1 byte1 byte
int16, uint162 bytes2 bytes
int32 (rune), uint32, float324 bytes4 bytes
int64, uint64, float64, complex648 bytes8 bytes
complex12816 bytes16 bytes
int, uint1 wordarchitecture dependent, 4 bytes on 32-bit architectures and 8 bytes on 64-bit architectures
uintptr1 wordlarge enough to store the uninterpreted bits of a pointer value
string2 wordsnot specified
pointer (safe or unsafe)1 wordnot specified
slice3 wordsnot specified
map1 wordnot specified
channel1 wordnot specified
function1 wordnot specified
interface2 wordsnot specified
struct(the sum of sizes of all fields) + (the number of padding bytes)the size of a struct type is zero if it contains no fields that have a size greater than zero
array(element value size) * (array length)the size of an array type is zero if its element type has zero size

参考