Live data from Hacker News

Every Byte Matters

fzakaria.com

1–3 of 3 posts

Re: Every Byte Matters

#2
This can be optimized further even without struct of arrays:

  struct Monster {
      uint32_t id;          // 4 bytes
      float    x, y, z;     // 12 bytes
      float    vx, vy, vz;  // 12 bytes
      int32_t  hp;          // 4 bytes
      int32_t  attack;      // 4 bytes
      int32_t  defense;     // 4 bytes
      uint8_t  is_alive;    // 1 byte
      uint8_t  team;        // 1 byte
      char     name[22];    // 22 bytes
  };
In many cases 16-bit integers are enough for storing positions and velocities (with carefully tuned range). Other stats can use 16-bit or even 8-bit integers, bit-fields can be used for boolean flags and other small integer values (like team). Storing per-moster name may be not necessary unless they are not monsters but unique NPCs.

The same optimization may also reduce network bandwidth for multiplayer games and save sizes for singleplayer games.

Re: Every Byte Matters

#3

This can be optimized further even without struct of arrays: struct Monster { uint32_t id; // 4 bytes float x, y, z; // 12 bytes float vx, vy, vz; // 12 bytes int32_t hp; // 4 bytes int32_t attack; // 4 bytes int32_t defense; // 4 bytes uint8_t is_alive; // 1 byte uint8_t team; // 1 byte char name[22]; // 22 bytes }; In many cases 16-bit integers are enough for storing positions and velocities (with carefully tuned r…

The struct can be optimized but if we keep growing it with our features we hit the same problem for the cache line sequential access.

Smaller structs help with keeping working set down though.