How to minimize struct memory usage?

for a transpose table (usually a hash table) of Connect Four games, I would like to use memory efficiently (to save as many elements as possible). One element of the table should store the following information:

  • lock: unsigned 64 bit
  • move: [0..6] → unsigned 3 bits
  • rating: [-2000..2000] → 12 bit signed
  • flag: VALID, UBOUND, LBOUND: → unsigned 2 bit
  • height: [-1..42]: → signed 7 bits

First, I tried the following data structure, which needs 24 bytes:

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

After rearranging the elements, 16 bytes are required (I found the answer for this behavior):

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

My last attempt:

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 ?

!

lock - -.

+3
3

, , ( , SMP- SMP-)

- . , , . , t, lock t % locktablesize. , .

TableEntry2, (, , , ), 12 - - , .

+2

, 88 , 96 (12 ); , ? , .

gazillions , , , ? ? , 16 , ? , , .

, , , __int64 8- . : 12 , - __int64 8 .

+6

You can reach 12 bytes using non-standard constructs such as Visual Studio #pragmapack :

#pragma pack(1)
struct TableEntry3
{
    unsigned __int64 lock;
    unsigned int move:3;
    int score:12;
    enum { VALID, UBOUND, LBOUND } flag:2;
    int height:7;
};

Here sizeof(TableEntry3)gives 12 for me.

+3
source

Source: https://habr.com/ru/post/1784594/


All Articles