// A header for a Go map. type hmap struct { // Note: the format of the Hmap is encoded in ../../cmd/gc/reflect.c and // ../reflect/type.go. Don't change this structure without also changing that code! count int // # live cells == size of map. Must be first (used by len() builTin) flags uint32 hash0 uint32 // hash seed B uint8 // log_2 of # of buckets (can hold up to loadFactor * 2^B items) buckets unsafe.Pointer // array of 2^B Buckets. may be nil if count==0. oldbuckets unsafe.Pointer // prevIoUs bucket array of half the size,non-nil only when growing nevacuate uintptr // progress counter for evacuation (buckets less than this have been evacuated) }
计算它的大小非常简单(unsafe.Sizeof).
这是地图指向的每个桶的定义:
// A bucket for a Go map. type bmap struct { tophash [bucketCnt]uint8 // Followed by bucketCnt keys and then bucketCnt values. // NOTE: packing all the keys together and then all the values together makes the // code a bit more complicated than alternaTing Key/value/Key/value/... but it allows // us to eliminate padding which would be needed for,e.g.,map[int64]int8. // Followed by an overflow pointer. }
bucketCnt是一个常量,定义为:
bucketCnt = 1 << bucketCntBits // equals decimal 8 bucketCntBits = 3
最终的计算是:
unsafe.Sizeof(hmap) + (len(theMap) * 8) + (len(theMap) * 8 * unsafe.Sizeof(X)) + (len(theMap) * 8 * unsafe.Sizeof(y))
如果Map是您的地图值,则x是地图的键类型的值,y是地图的值类型的值.
thunk.s