Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Protocol Buffers int32 vs int64

If I store an integer field in int32...will this use more space than int64? From what I understand the varint will adjust its size with the size of the number being stored.

like image 321
DD. Avatar asked Feb 05 '12 11:02

DD.


People also ask

What is the Protobuf wire format?

Protocol Buffers is a high-performance, compact binary wire format invented by Google who use it internally so they can communicate with their internal network services at very high speed.

What is Google Protobuf value?

Value represents a dynamically typed value which can be either null, a number, a string, a boolean, a recursive struct value, or a list of values.

What are Google Protocol Buffers used for?

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 bindings.

Should I use Proto2 and Proto3?

Proto2: supports optional natively, the wrapper types were recommended but should be avoided in new applications. Proto3: originally did not support presence tracking for primitive fields. As of 2020, proto3 supports both optional fields which have has_foo() methods and "singular" fields, which do not.


1 Answers

No, this only impacts the generated code. Any combination of [s|u]int{32|64} uses "varint" encoding, so the size is generally related to the magnitude, at least after noting the difference in negative numbers. In particular, a negative number that doesn't use sint* will be disproportionately large (10 bytes, IIRC), regardless of whether it is 32 or 64.

like image 75
Marc Gravell Avatar answered Sep 30 '22 19:09

Marc Gravell