Golang:计算地图的内存占用(或字节长度)

orc*_*man 5 dictionary go

我想将地图限制为最大X字节.似乎没有直接计算地图字节长度的方法.

"encoding/binary"package有一个很好的Size功能,但它只适用于切片或"固定值",而不适用于map.

我可以尝试从地图中获取所有键/值对,推断它们的类型(如果它是a map[string]interface{})并计算长度 - 但这既麻烦又可能不正确(因为这将排除地图的"内部"golang成本本身 - 管理元素指针等.

这样做的任何建议方式?最好是代码示例.

thw*_*hwd 9

这是地图标题的定义:

// 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)
}
Run Code Online (Sandbox Code Playgroud)

计算它的大小非常简单(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.
}
Run Code Online (Sandbox Code Playgroud)

bucketCnt 是一个常量定义为:

bucketCnt     = 1 << bucketCntBits // equals decimal 8
bucketCntBits = 3
Run Code Online (Sandbox Code Playgroud)

最终的计算是:

unsafe.Sizeof(hmap) + (len(theMap) * 8) + (len(theMap) * 8 * unsafe.Sizeof(x)) + (len(theMap) * 8 * unsafe.Sizeof(y))
Run Code Online (Sandbox Code Playgroud)

theMap地图值在哪里,x是地图的键类型y值和地图值类型的值.

您必须hmap通过程序集与程序包共享结构,类似于thunk.s运行时.

  • JFYI`thunk.s`已替换为[`// go:linkname`编译器指令](https://golang.org/cmd/compile/#hdr-Compiler_Directives). (2认同)
  • 你能更新这个答案吗?显然,这些都不起作用了。hmap结构完全不同,您的答案不考虑容量,也无法通过汇编共享结构(也不能通过go:linkname共享) (2认同)