Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Dictionary 计算映射的内存占用(或字节长度)_Dictionary_Go - Fatal编程技术网

Dictionary 计算映射的内存占用(或字节长度)

Dictionary 计算映射的内存占用(或字节长度),dictionary,go,Dictionary,Go,我想将映射限制为最大X字节。但是,似乎没有直接的方法来计算映射的字节长度 “encoding/binary”软件包有一个不错的功能,但它只适用于切片或“固定值”,不适用于贴图 我可以尝试从映射中获取所有键/值对,推断它们的类型(如果它是map[string]接口{}),并计算长度-但这既麻烦又可能不正确(因为这将排除映射本身的“内部”Go成本-管理元素指针等) 有什么建议吗?最好是一个代码示例 这适用于地图标题: // A header for a Go map. type hmap struc

我想将映射限制为最大X字节。但是,似乎没有直接的方法来计算映射的字节长度

“encoding/binary”
软件包有一个不错的功能,但它只适用于切片或“固定值”,不适用于贴图

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

有什么建议吗?最好是一个代码示例

这适用于地图标题:

// 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
其中
theMap
是地图值,
x
是地图键类型的值,
y
是地图值类型的值


您必须通过汇编与您的包共享
hmap
结构,类似于在运行时。

您不能以可靠的方式做您想做的事情:例如,“内部golang成本”取决于您机器的字数大小。虽然我认为“地图使用X字节”的概念是有缺陷的;考虑一个包含一个切片的地图:你把后备数组的字节数映射到地图吗?我建议搜索不同的解决方案,尤其是
interface{}
的使用有一股难闻的味道。谢谢你的评论。这个用例实际上相当简单:我想对缓存施加一个大小限制(而不是时间限制)。如果我想使用映射(可以说是Go中用作缓存对象的最合适的原语)(让我们假设它不是字符串/接口的映射)来实现这一点,我在语言中的选项是什么(使用外部工具似乎有些过分)?限制缓存中的数据量是一个有效的用例,但这只有在缓存中存储大量可变大小的数据时才是明智的,这意味着映射本身的开销可以忽略不计。如果您有固定大小的数据,您可以通过地图的长度进行限制。JFYI
thunk.s
已被替换为。您可以更新此答案吗?显然,这一切都不管用了。hmap结构是完全不同的,您的答案不考虑容量,您不能通过assembly(也不能通过go:linkname)共享结构Smaug:Hi,事实是map的实现在未来会不断变化。因此,我把答案留作指南,而不是解决方案。您仍然可以使用程序集共享名称。@完整的代码片段比部分响应更有用,然后我们必须在其他地方查看如何使用程序集共享名称,为什么不放置一个自包含的响应呢?
unsafe.Sizeof(hmap) + (len(theMap) * 8) + (len(theMap) * 8 * unsafe.Sizeof(x)) + (len(theMap) * 8 * unsafe.Sizeof(y))