Make writing a habit together! This is the third day of my participation in the “Gold Digging Day New Plan · April More text Challenge”. Click here for more details.

The official Go library always gives us the impression that it is powerful and easy to use. However, no one is perfect, and in some performance demanding scenarios such as JSON parsing, some official libraries are not as good as they should be.

This article introduces several open source libraries that are benchmarked against official libraries, and their performance far exceeds the former. When readers encounter performance issues with these official libraries, they can try alternative optimizations.

net/http -> fasthttp

Address: github.com/valyala/fas…

Fasthttp claims to be 10 times faster than NET/HTTP, and the core idea behind its optimization is simple: resource reuse.

  • Reuse Goroutine to reduce runtime scheduling pressure;

  • Object reuse, heavy use of sync.pool to reduce GC pressure.

In addition to reuse, there are other optimizations, such as minimizing the conversion overhead between string and []byte.

These optimization tips and best practices are provided on Github’s homepage: _github.com/valyala/fas…

Because the implementation of FASthTTP is quite different from the standard library, it is different from the API interface of NET/HTTP, which leads to some learning cost in refactoring from NET/HTTP to FasthTTP.

Notable projects using FasTHTTP: Fiber, Gearbox, Atreugo, etc.

encoding/json -> jsoniter

Address: github.com/json-iterat…

Jsoniter (JSON-Iterator) is a fast and flexible JSON parser available in both Java and Go versions. Golang is officially stated to be up to 6 times faster than the standard library (Encoding/JSON).

Most importantly, it works with the standard libraryencoding/jsonFully compatible.

  • Marshal()
# encoding/json 
import "encoding/json"json.Marshal(&data)

# jsoniter
import jsoniter "github.com/json-iterator/go"
var json = jsoniter.ConfigCompatibleWithStandardLibrary
json.Marshal(&data)
Copy the code
  • Unmarshal()
# encoding/json
import "encoding/json"
json.Unmarshal(input, &data)

# jsoniter
import jsoniter "github.com/json-iterator/go"
var json = jsoniter.ConfigCompatibleWithStandardLibrary
json.Unmarshal(input, &data)
Copy the code

Readers interested in how it works can see it here: jsoniter.com/benchmark.h…

golang/protobuf -> gogo/protobuf

Address: github.com/gogo/protob…

ProtoBuf, Protocol Buffers, is a Protocol format similar to XML and JSON developed and defined by Google. It is used to efficiently store and read structured data. It’s binary based, so with ProtoBuf you can compress the data even smaller.

Gogo/Protobuf is an enhanced implementation based on the official golang/ Protobuf library:

  • Faster serialization and deserialization than Golang/Protobuf;

  • More standard Go structure;

  • Compatible golang/protobuf;

  • Optionally generate additional help code to reduce code input;

  • Can generate test code and benchmark code;

  • Other serialization formats;

There are many well-known projects using this library, such as ETCD, K8S, Docker SwarmKit, TiDB, Nakama, etc.

html/template -> valyala/quicktemplate

Address: github.com/valyala/qui…

Quicktemplate, inspired by Python’s Mako project, is a fast, powerful, and easy to use Go template rendering engine with the following key features:

  • Quicktemplate converts the written template code to Go language code before compiling and rendering. Therefore, it is more than 20 times faster than the standard library HTML/Template.

  • Quicktemplate’s syntax is very similar to the Go syntax, with almost no learning cost.

  • Almost all bugs can be caught at template compilation time, so there are very few template-related bugs in a real project.

  • Any Go code can be embedded in the template.

Although the primary purpose of QuickTemplate is to generate HTML, it can also be used to generate other data.

For example, JSON and XML serialization can be easily implemented using QuickTemplate, and serialization through QuickTemplate is usually faster than encoding/ JSON and Encoding/XML through the standard libraries.

conclusion

The list of libraries in this article is not intended to immediately replace the official library. The NET/HTTP package, for example, is already adequate for most usage scenarios.

When we have a problem using the official library, it is easy to find a solution through a search engine, or to issue an issue directly to the Go official. When switching to open source libraries, problems may not always be addressed in a timely manner.

The official library API is almost guaranteed to be compatible with iterations of the Go version, while the third-party library may have potential version compatibility issues that need to be considered when switching.

Several open source libraries listed in this article focus on optimizing the performance of their official counterparts. There are plenty of useful Go code optimization tips to learn from these open source libraries.

Of course, if your project is experiencing performance issues due to these official libraries, try it.