insanely_sin
insanely_sin

Reputation: 1026

What is the space taken by bool in Google Protocol Buffers?

message Person{
  optional bool foo = 1;
  optional bool bar = 2;
}

In the serialized form what is the space taken by a bool type in google protobuf?

Upvotes: 5

Views: 3278

Answers (1)

Marc Gravell
Marc Gravell

Reputation: 1063358

A bool is encoded as a varint with value 0 or 1, so the payload will take 1 byte. The field header size is dependent on the field number; for fields 1 and 2, this will be 1 byte. So overall: 2 bytes. If you are storing lots of books, consider packing them bitwise into a single integer field - perhaps using fixed width (fixed32 etc) if the high bits are likely (large magnitude numbers are relatively expensive to encode as varint)

Upvotes: 7

Related Questions