bytedance / sonic

A blazingly fast JSON serializing & deserializing library
Apache License 2.0
6.71k stars 333 forks source link

should encoder and decoder object be shared or created on demand? #534

Open yangcheng opened 11 months ago

yangcheng commented 11 months ago

It's unclear from documentation what's the best practice to use encoder and decoder.

Should I keep a global encoder ? is the object thread-safe?

Thanks

liuq19 commented 11 months ago

Encoder or decoder is mostly used for streaming or some specific configurations, such as decoder with UseNumber, or encoder with SortKeys. And there are no benefits to reusing an encoder or decoder. So recommended not to use a shared encoder or decoder.

yangcheng commented 11 months ago

good to know that, I was about to use encoder for streaming io as mentioned in https://github.com/bytedance/sonic#streaming-io , no specific configuration, just http write response. is it recommended to simplely use Marshal ?

The reason I am asking is I noticed very similar performance os sonic encoder vs json encoder in my test and I am curious if I did something wrong

liuq19 commented 11 months ago

good to know that, I was about to use encoder for streaming io as mentioned in https://github.com/bytedance/sonic#streaming-io , no specific configuration, just http write response. is it recommended to simplely use Marshal ?

yes, if not in streaming scenes or specific configuration, just use Marshal