-
Notifications
You must be signed in to change notification settings - Fork 17.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
proposal: encoding/json: more performant implementation with JIT and SIMD #53178
Comments
There is currently work going on creating a successor to encoding/json by some members of the Go team at https://github.com/go-json-experiment/json. There is a section in the Readme file comparing performance of selected json packages including sonic. |
Hi @AsterDY, thanks for your proposal. The approaches that
These are significant disadvantages that I believe make a JIT unsuitable as the default approach. JITs make sense if performance is the greatest (and almost singular) priority. However, most people use Go because they want the type and memory safety that the language provides. JSON performance is important, but it is catastrophic to many users if the JSON implementation was a means through which attackers could exploit remote code execution by sending the server a maliciously crafted JSON input that triggers memory corruption in the code produced by the JIT. As much as possible, we want to lean on code that avoids the use of That said, use of assembly (and SIMD instructions) can be appropriate if it 1) provides significant performance, and 2) is relatively concise and well tested. For this reason, packages under Based on my work on JSON, I would say that there are two notable areas where performance optimizations (done in assembly or otherwise) will help
Most of these areas of optimization are actually not in the |
Thanks for the reply. As you mentioned, sonic has its own problems regarding compatibility, such as our JIT system can work on assumptions about how the Go runtime operates, or |
Merging a JIT into the Go toolchain itself will help alleviate the 4th disadvantage, but it doesn't directly address the 1st, 2nd, or 3rd disadvantages. The 1st disadvantage is the most significant and is alleviated if there is a well-staffed team to maintain the JIT and battle test it for correctness. This is way beyond my ability to adequately comment on, but I suspect any attempt to staff a team to manage a JIT will take away from efforts spent on the Go compiler itself. Personally, I would rather see the Go compiler get better. |
I think it's worth looking at https://go.dev/blog/survey2021-results#prioritization for context. It looks like many Go developers care more about maintainability (diagnosing bugs), reliability, and security than they do about CPU usage. There are also binary size and build speed further down the list; it seems to me like using a JIT in encoding/json would be a very hard tradeoff to sell. We'd make the library faster at the cost of all the other factors I mentioned, which goes against what the survey tells us. There's also https://go.dev/doc/faq#x_in_std; nothing prevents a JIT-based JSON encoder and decoder from living outside of the standard library. If people want to squeeze every bit of performance at the cost of some other factor (user experience, safety, maintainability, binary size, etc), typically the answer is outside of the standard library. Take a look at https://github.com/valyala/fasthttp for example, which is up to ten times faster than net/http, but likely does not make sense as part of the standard library. |
Since JSON protocol is such popular today,
encoding/json
is being heavily used in all kinds of Go-based applications. However, we found it consumed a lot of computation resources in practice, due to its poor performance and less-efficient APIs. This is the main reason why we developed sonic, a high-performance and dedicated-API JSON library:encoding/json
(default behaviors may be a little different given performance, but all can be adjusted through options)Get()/Set()/Unset()/Add()
), which is a very common need in practice.encoding/json
regarding standard APIs (referring benchmarks), not to speak of dynamic APIs that are more efficient.One topic I want to discuss here is if it can be used as the underlying implementation of the standard library? If not, at least some of our optimizations,
JIT
/SIMD
/lazy-load
, can be absorbed into the standard library?The text was updated successfully, but these errors were encountered: