https://developers.google.com/protocol-buffers/docs/overview
Overview | Protocol Buffers | Google Developers
Protocol buffers provide a language-neutral, platform-neutral, extensible mechanism for serializing structured data in a forward-compatible and backward-compatible way. It’s like JSON, except it's smaller and faster, and it generates native language bind
developers.google.com
serialize 플랫폼으로 Google 의 Protocol Buffers 라는 솔루션이 있다. C++, C#, Dart, Go, Java, Kotlin, Python 등 다양한 언어를 지원하고 protocol 간의 하위 호환성이 좋아서 네트워크 통신이나 db 에 blob 형태로 데이터 저장할 때 유용하다.
https://developers.google.com/protocol-buffers/docs/encoding#structure
Encoding | Protocol Buffers | Google Developers
Encoding Stay organized with collections Save and categorize content based on your preferences. This document describes the protocol buffer wire format, which defines the details of how your message is sent on the wire and how much space it consumes on dis
developers.google.com
scalar 값을 저장할 때 int32, int64 등은 VARINT 방식으로 저장된다.
Encoding | Protocol Buffers | Google Developers
Encoding Stay organized with collections Save and categorize content based on your preferences. This document describes the protocol buffer wire format, which defines the details of how your message is sent on the wire and how much space it consumes on dis
developers.google.com
Base 128 Varints 라고 명명하고 있는데 8 bit 중 msb 에 다음 데이터가 있는지 적고 나머지 7 bit 에 데이터를 싣는 방식이다.
150 같은 경우 아래와 같이 표기된다.
10010110 00000001
^ msb ^ msb
10010110 00000001 // Original inputs.
0010110 0000001 // Drop continuation bits.
0000001 0010110 // Put into little-endian order.
10010110 // Concatenate.
128 + 16 + 4 + 2 = 150 // Interpret as integer.
작은 숫자의 경우 작은 저장 공간을 사용할 수 있다. int32 같은 경우 int64 와 type 호환도 가능하다.
Encoding | Protocol Buffers | Google Developers
Encoding Stay organized with collections Save and categorize content based on your preferences. This document describes the protocol buffer wire format, which defines the details of how your message is sent on the wire and how much space it consumes on dis
developers.google.com
음수의 경우 two's complement 방식이라면 항상 최대 저장공간을 사용하게 된다. sint32, sint64 type 을 사용하면 ZigZag 인코딩을 사용해서 저장 공간을 줄일 수 있다. 음수 값이 자주 사용되는 상황이면 sintN 형식을 사용을 고려해볼만 하다.
Signed Original | Encoded As |
0 | 0 |
-1 | 1 |
1 | 2 |
-2 | 3 |
... | ... |
0x7fffffff | 0xfffffffe |
-0x80000000 | 0xffffffff |