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

On 10/22/22 18:12, Travis Keep wrote:
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.


--
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+unsubscr...@googlegroups.com <mailto:golang-nuts+unsubscr...@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>.

--
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/8cca63fe-27e6-4346-fa6b-71ce8e93ef90%40mb0.org.

Reply via email to