Gamedev Hub

Data Serialization (High-Speed Game State)

In professional game development, saving or syncing state needs to be nearly instantaneous. Standard JSON or XML is too slow and bulky for hot-path data. Professional engines use Binary Serialization and Memory Mapping to handle massive amounts of data with zero overhead.

1. Text vs. Binary

2. Bit-Packing (The Minimalist Approach)

In networking or high-density save files, we don’t waste 32 bits on a value that only ranges from 0 to 10.

3. Memory-Mapped Files (Zero-Copy)

The fastest way to load data is to not “load” it at all.

4. Zero-Parsing Formats (FlatBuffers)

Standard binary formats still require a “deserialization” step. FlatBuffers (developed by Google for games) change this.

5. Implementation (C# Bit-Packing)

public struct PackedState {
    public uint rawData;

    // Use bits 0-6 for Health (0-127)
    public int Health {
        get => (int)(rawData & 0x7F);
        set => rawData = (rawData & ~0x7Fu) | ((uint)value & 0x7F);
    }

    // Use bit 7 for IsPoisoned
    public bool IsPoisoned {
        get => (rawData & 0x80) != 0;
        set => rawData = value ? (rawData | 0x80) : (rawData & ~0x80u);
    }
}

6. Summary