Finding encoded size of var int?

646 views
Skip to first unread message

jonath...@gree.co.jp

unread,
Jan 6, 2014, 2:07:21 PM1/6/14
to prot...@googlegroups.com
Sorry if this has been covered before. I searched but couldn't find a complete answer (or at least what I thought was complete).

When I write a varint to a coded output stream via coded_stream.WriteVarInt32([some value]) is it possible to just do a quick calculation to find the number of bytes that would be written to the stream in that scenario just based on the value of the integer passed in?

Is there any additional overhead to indicate that it is a varint when encoded to the stream or is the varint size just the same calculation as dictated in the language docs (here https://developers.google.com/protocol-buffers/docs/encoding?hl=zh-CN#varints). Obviously one easy way to find the size out be to create a coded output stream, write the varint to it and then find the byte size difference. I'm just wondering if there is a better/faster way than having to construct and delete a coded output stream for a calculation.

Thanks ahead of time!

-Jonathan

Ilia Mirkin

unread,
Jan 6, 2014, 4:17:45 PM1/6/14
to jonath...@gree.co.jp, prot...@googlegroups.com
Find the highest bit set on your integer, divide by 7 and round up
(i.e. + 6 / 7) -- that should be the number of bytes it takes to
encode the varint. On x86, there is a bsr instruction which computes
ilog2 (with gcc you could do e.g. 32 - __builtin_clz(var)).

-ilia

jonath...@gree.co.jp

unread,
Jan 6, 2014, 7:19:12 PM1/6/14
to prot...@googlegroups.com
Cool, thanks for the info. So it seems as if there is no additional information in the encoding to say that it is a varint, just the raw bytes of it. I'll give it a shot.

-Jonathan

jonath...@gree.co.jp

unread,
Jan 9, 2014, 7:30:00 PM1/9/14
to prot...@googlegroups.com
Ha!  Apparently there is a VarintSize32 in CodedOutputStream. I somehow overlooked that in the coded and the docs.

I must have been tired :)

-Jonathan
Reply all
Reply to author
Forward
0 new messages