计算映射的内存占用量(或字节长度) [英] Computing the memory footprint (or byte length) of a map

查看:103
本文介绍了计算映射的内存占用量(或字节长度)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将地图限制为最多X个字节.似乎没有直接的方法可以计算映射的字节长度.

I want to limit a map to be maximum X bytes. It seems there is no straightforward way of computing the byte length of a map though.

"encoding/binary"软件包具有不错的 Size函数,但它仅适用于切片或固定值",不适用于地图.

"encoding/binary" package has a nice Size function, but it only works for slices or "fixed values", not for maps.

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

I could try to get all key/value pairs from the map, infer their type (if it's a map[string]interface{}) and compute the length - but that would be both cumbersome and probably incorrect (because that would exclude the "internal" Go cost of the map itself - managing pointers to elements etc).

有人建议这样做吗?最好是一个代码示例.

Any suggested way of doing this? Preferably a code example.

推荐答案

这是定义以获得地图标题:

// 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).

Calculating its size is pretty straightforward (unsafe.Sizeof).

这是地图指向的每个单独存储桶的定义:

This is the definition for each individual bucket the map points to:

// 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 is a constant defined as:

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))

其中theMap是您的地图值,x是地图的键类型的值,而y是地图的值类型的值.

Where theMap is your map value, x is a value of the map's key type and y a value of the map's value type.

您必须通过汇编与软件包共享hmap结构,类似于 thunk.s 在运行时.

You'll have to share the hmap structure with your package via assembly, analogously to thunk.s in the runtime.

这篇关于计算映射的内存占用量(或字节长度)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆