data-serialization

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Data Serialization for Games

游戏数据序列化

Implement efficient serialization for low-latency game networking.
为低延迟游戏网络实现高效序列化

Format Comparison

格式对比

FormatSizeSpeedSchemaUse Case
ProtobufSmallFastRequiredMost games
FlatBuffersSmallFastestRequiredReal-time
MsgPackSmallFastOptionalFlexible
JSONLargeSlowNoneDebug
Custom BinarySmallestFastestCustomUltra-low latency
格式大小速度模式适用场景
Protobuf必填大多数游戏
FlatBuffers最快必填实时场景
MsgPack可选灵活场景
JSON调试场景
自定义二进制最小最快自定义超低延迟场景

Protocol Buffers

Protocol Buffers

protobuf
syntax = "proto3";

message PlayerState {
    uint32 player_id = 1;
    float x = 2;
    float y = 3;
    float z = 4;
    uint32 health = 5;
}

message GameUpdate {
    uint64 tick = 1;
    repeated PlayerState players = 2;
}
cpp
GameUpdate update;
update.set_tick(current_tick);
auto* player = update.add_players();
player->set_player_id(1);
player->set_x(pos.x);

std::string serialized;
update.SerializeToString(&serialized);
protobuf
syntax = "proto3";

message PlayerState {
    uint32 player_id = 1;
    float x = 2;
    float y = 3;
    float z = 4;
    uint32 health = 5;
}

message GameUpdate {
    uint64 tick = 1;
    repeated PlayerState players = 2;
}
cpp
GameUpdate update;
update.set_tick(current_tick);
auto* player = update.add_players();
player->set_player_id(1);
player->set_x(pos.x);

std::string serialized;
update.SerializeToString(&serialized);

Custom Binary Format

自定义二进制格式

cpp
struct PacketHeader {
    uint16_t type;
    uint16_t length;
    uint32_t sequence;
};

struct PlayerUpdate {
    uint32_t player_id;
    float position[3];
    uint16_t angle;  // Compressed rotation
    uint8_t flags;
};

void serialize(Buffer& buf, const PlayerUpdate& p) {
    buf.write_u32(htonl(p.player_id));
    for (int i = 0; i < 3; i++)
        buf.write_float(p.position[i]);
    buf.write_u16(htons(p.angle));
    buf.write_u8(p.flags);
}
cpp
struct PacketHeader {
    uint16_t type;
    uint16_t length;
    uint32_t sequence;
};

struct PlayerUpdate {
    uint32_t player_id;
    float position[3];
    uint16_t angle;  // 压缩后的旋转角度
    uint8_t flags;
};

void serialize(Buffer& buf, const PlayerUpdate& p) {
    buf.write_u32(htonl(p.player_id));
    for (int i = 0; i < 3; i++)
        buf.write_float(p.position[i]);
    buf.write_u16(htons(p.angle));
    buf.write_u8(p.flags);
}

Compression Techniques

压缩技术

TechniqueSavingsComplexity
Delta50-80%Medium
Quantization30-50%Low
LZ450-70%Low
压缩技术压缩率实现复杂度
Delta50-80%中等
量化(Quantization)30-50%
LZ450-70%

Troubleshooting

故障排查

Common Failure Modes

常见故障模式

ErrorRoot CauseSolution
Parse errorVersion mismatchSchema versioning
Large packetsNo compressionEnable delta/LZ4
Slow parsingJSON in hot pathUse binary format
CorruptionByte orderUse htonl/ntohl
错误类型根本原因解决方案
解析错误版本不兼容模式版本控制
数据包过大未启用压缩启用Delta/LZ4压缩
解析缓慢热路径使用JSON使用二进制格式
数据损坏字节序问题使用htonl/ntohl转换

Debug Checklist

调试检查清单

cpp
// Check serialized size
std::string data;
message.SerializeToString(&data);
std::cout << "Size: " << data.size() << " bytes\n";

// Validate roundtrip
Message parsed;
parsed.ParseFromString(data);
assert(parsed.id() == message.id());
cpp
// 检查序列化后大小
std::string data;
message.SerializeToString(&data);
std::cout << "Size: " << data.size() << " bytes\n";

// 验证往返序列化
Message parsed;
parsed.ParseFromString(data);
assert(parsed.id() == message.id());

Unit Test Template

单元测试模板

cpp
TEST(Serialization, RoundTrip) {
    PlayerState original{1, 10.0f, 20.0f, 30.0f, 100};

    std::string data;
    original.SerializeToString(&data);

    PlayerState parsed;
    parsed.ParseFromString(data);

    EXPECT_EQ(original.player_id(), parsed.player_id());
    EXPECT_FLOAT_EQ(original.x(), parsed.x());
}
cpp
TEST(Serialization, RoundTrip) {
    PlayerState original{1, 10.0f, 20.0f, 30.0f, 100};

    std::string data;
    original.SerializeToString(&data);

    PlayerState parsed;
    parsed.ParseFromString(data);

    EXPECT_EQ(original.player_id(), parsed.player_id());
    EXPECT_FLOAT_EQ(original.x(), parsed.x());
}

Resources

资源

  • assets/
    - Schema templates
  • references/
    - Format benchmarks
  • assets/
    - 模式模板
  • references/
    - 格式基准测试