ugorji / go

idiomatic codec and rpc lib for msgpack, cbor, json, etc. msgpack.org[Go]
MIT License
1.86k stars 295 forks source link

Marshal/Unmarshal directly from/to byte array for message pack #177

Closed thienohs closed 8 years ago

thienohs commented 8 years ago

Hi ugorji,

Currently, I couldn't find a way to Marshal/Unmarshal directly from/to byte array using MsgPack. Is there a way to achieve this without specify a Reader/Writer/Stream beforehand in constructor ?

I can use the ResetBytes function to achieve this but if I have an Encoder/Decoder that share among multiple goroutines, this could be a problem because the underlying []byte is not thread-safe.

I have tried to run a test with allocation a new Encoder/Decoder everytime I have a new []byte array to work with, but that allocation process resulted in doubling the serialisation time.

ugorji commented 8 years ago

You should allocate a new one, or reuse one but ensure that only one person is using it at a time (using an exclusive mutex or a sync.Pool where you grab one, use it and return it to the pool).