如何最大限度地减少结构类型的内存使用情况?限度、内存、类型、结构

2023-09-11 23:35:19 作者:可爱到炸

对于连接四个的置换表(通常是一个哈希表) 比赛中,我想使用的内存效率(存储最有可能 元素的数量)。一个表元素来存储以下内容:

for the transposition table (generally a hash table) of a Connect Four game, I would like to use the memory efficiently (to store the most possible number of elements). One table element has to store following information:

锁:无符号64位 将:[0..6] - >无符号的3位 比分:[-2000..2000] - >签署12位 标记:有效,UBOUND,LBOUND: - >无符号的2位 高度:[-1..42]: - >签署7位

首先,我试过下面的数据结构,这需要24个字节:

First I tried following data structure, which needs 24 Bytes:

struct TableEntry1
{
    unsigned __int64 lock;
    unsigned char move;
    short score;
    enum { VALID, UBOUND, LBOUND } flag;
    char height;
};

重新安排它需要16个字节的元素后,(我发现answer为了这 行为):

struct TableEntry2
{
    unsigned __int64 lock;
    enum { VALID, UBOUND, LBOUND } flag;
    short score;
    char height;
    unsigned char move;
};

我最后的尝试是:

My last try was:

struct TableEntry3
{
    unsigned __int64 lock;
    unsigned int move:3;
    int score:12;
    enum { VALID, UBOUND, LBOUND } flag:2;
    int height:7;
};

这也需要16个字节。是否有可能改变,使得它的结构 只用了12个(在32位架构)?为什么编译器不会让我的最后一次尝试12个字节长?

Which also needs 16 Bytes. Is it possible to change the structure so that it only uses 12 Bytes (on a 32 bit-architecture)? Why the compiler doesn't make my last try 12 bytes long?

谢谢!

修改属性锁定是一个独特的元素ID检测散列冲突​​。

Edit The property lock is a unique element id to detect hash collisions.

推荐答案

根据你如何实现锁,它可能会减少你的锁字段的大小(我假设这是一个典型的SMP锁SMP机)

Depending on how you implement locks, it may be possible to reduce the size of your lock field (I'm assuming this is a typical SMP lock SMP machine)

一种选择是降低锁的数量。也就是说,有一个的单独的的较小的阵列的锁,并锁定在这里来自您的数组元素的元素。也就是说,如果你在访问置换表元素 T ,使用锁 T%locktablesize 。锁表没有是几乎一样大换位表。

One option would be to reduce the number of locks. That is, have a seperate, smaller array of locks, and lock an element derived from your array element here. That is, if you're accessing transposition table element t, use lock t % locktablesize. The lock table doesn't have to be nearly as big as the transposition table.

使用这种方法,你的 TableEntry2 字段顺序,并假设你的锁表的置换表的一半大小(这可能不是必要的大),你趴下12个字节的而不丧失性能由于bitshift操作的 - 这种性能损失可能是相当显著,所以它总是有帮助,以便能够避免它

Using this approach, and your TableEntry2 field order, and assuming your lock table is half the size of the transposition table (which is probably bigger than necessary) you get down to 12 bytes without losing performance due to bitshift operations - this performance loss can be quite significant, so it's always helpful to be able to avoid it.