当前位置:网站首页>Using easyjson to improve the efficiency of serialization transmission

Using easyjson to improve the efficiency of serialization transmission

2022-06-24 17:22:00 Johns

Introduce

easyjson It is used to do sth quickly json Serialization and deserialization toolkit , By giving us the struct Generate methods to implement... Without reflection json serialize , Than golang The original json tool kit , Performance can be improved 2~3 times .

go The reflection of language api The design is not like java You can also get the field value of the object directly , Instead, use it every time reflect.ValueOf(v) To create a new field object and then get the field value , This will add an extra GC The burden of , At the same time, the efficiency is also low . By traversing fields to assemble field contents, unnecessary object creation can be avoided , And it will be more efficient .

Use

  • install
go get -u github.com/mailru/easyjson/
go install  github.com/mailru/easyjson/easyjsonor
go build -o easyjson github.com/mailru/easyjson/easyjson

Check if the installation is successful

 ~bash>easyjson
Usage of easyjson:
  -all
        generate marshaler/unmarshalers for all structs in a file
  -build_tags string
        build tags to add to generated file
  -byte
        use simple bytes instead of Base64Bytes for slice of bytes
  -disable_members_unescape
.....
  • Entity class service.go
//easyjson:json
type MultiplyRequest struct {
	A int `json:"a"`
	B int `json:"b"`
}

//easyjson:json
type MultiplyResponse struct {
	Res int `json:"res"`
}
  • Go to the command line , Switch to current go Enter the directory where the file is located :easyjson -all service.go Will generate service_easyjson.go, This file provides methods for serialization and deserialization .
func easyjson8d893851DecodeGoKitMicroservicePb3(in *jlexer.Lexer, out *MultiplyRequest) {
	isTopLevel := in.IsStart()
	if in.IsNull() {
		if isTopLevel {
			in.Consumed()
		}
		in.Skip()
		return
	}
	in.Delim('{')
	for !in.IsDelim('}') {
		key := in.UnsafeFieldName(false)
		in.WantColon()
		if in.IsNull() {
			in.Skip()
			in.WantComma()
			continue
		}
		switch key {
		case "a":
			out.A = int64(in.Int64())
		case "b":
			out.B = int64(in.Int64())
		default:
			in.SkipRecursive()
		}
		in.WantComma()
	}
	in.Delim('}')
	if isTopLevel {
		in.Consumed()
	}
}

We can see very clearly that Marshal The specific process of .

  • Formal code replacement transport.go
func decodeMultiplyRequest(ctx context.Context, r *http.Request) (interface{}, error) {

	bs, err := ioutil.ReadAll(r.Body)
	if err != nil {
		return nil, err
	}

	fmt.Printf(string(bs))

	req := &MultiplyRequest{}
	err = req.UnmarshalJSON(bs)
	if err != nil {
		return nil, err
	}

	return nil, nil
}

func decodeMultiplyRequest(ctx context.Context, r *http.Request) (interface{}, error) {

	bs, err := ioutil.ReadAll(r.Body)
	if err != nil {
		return nil, err
	}

	fmt.Printf(string(bs))

	req := &MultiplyRequest{}
	err = req.UnmarshalJSON(bs)
	if err != nil {
		return nil, err
	}

	return nil, nil
}
// decode response
func encodeMultiplyResponse(ctx context.Context, w http.ResponseWriter, response interface{}) error {

	if f, ok := response.(endpoint.Failer); ok && f.Failed() != nil {
		errorEncoder(ctx, f.Failed(), w)
		return nil
	}

	resp := response.(*pb.MultiplyResponse)
	w.Header().Set("Content-Type", "application/json; charset=utf-8")
	bs, err := resp.MarshalJSON()
	if err != nil {
		return err
	}
	w.Write(bs)
	return nil
}
原网站

版权声明
本文为[Johns]所创,转载请带上原文链接,感谢
https://yzsam.com/2021/03/20210322183714189h.html

随机推荐