What is the best way to write MarshalBinary if the data is a sequence of ints

323 views
Skip to first unread message

Travis Keep

unread,
Oct 22, 2022, 12:12:38 PM10/22/22
to golang-nuts
The type I am writing can be easily encoded as a sequence of ints. When I wrote MarshalBinary for my type, I decided to GobEncode this sequence of ints to a byte buffer and return the bytes.  I learned that GobEncoding an empty slice of int takes 18 bytes.  So even for small instances of my type, I am paying for 18 extra bytes of overhead to encode.  I chose Gob because it uses variable length encoding for ints, so smaller ints require fewer bytes to encode.

Since I know that I will always be encoding and decoding an []int, is there a better way to implement MarshalBinary without reimplementing the variable length encoding of ints from scratch?

Thank you in advance.


Martin Schnabel

unread,
Oct 23, 2022, 3:00:36 AM10/23/22
to golan...@googlegroups.com
hi travis,

if you can use any encoding you like, you can write your own custom
encoding that uses a varint encoding. the binary encoding interface
implies that the framing or length segmenting of the raw bytes happens
outside that function, so you can just read and write all your ints
using the varint functions from package encoding/binary.

https://pkg.go.dev/encoding/binary?utm_source=godoc#Varint

hope that helps
> --
> You received this message because you are subscribed to the Google
> Groups "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to golang-nuts...@googlegroups.com
> <mailto:golang-nuts...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/golang-nuts/983c6c04-2473-42f1-be29-371e85d66480n%40googlegroups.com <https://groups.google.com/d/msgid/golang-nuts/983c6c04-2473-42f1-be29-371e85d66480n%40googlegroups.com?utm_medium=email&utm_source=footer>.

Amnon

unread,
Oct 24, 2022, 12:59:06 AM10/24/22
to golang-nuts
What is best depends on what you want to achieve.
Do you want the most compact encoding? Or the fastest encoding? 
Or the encoding which is easiest to debug? Or the simplest encoding?
Or an encoding which allows you to gracefully add fields to the type as your program evolves without invalidating persisted data?

You state that your type is easily represented as a sequence of ints. But I would ask whether converting to ints is an unnecessary 
step which will lead to less clear code?

Hope this helps....
Reply all
Reply to author
Forward
0 new messages