Skip to content

code_review/performance: gjson.ParseBytes call twice in rsp json parsing cause extra performance cost should use json.RawMessage #39

@pymongo

Description

@pymongo

current json rpc response code performance is poor

https://github.com/block-vision/sui-go-sdk/blob/c2c872d55fef3a9a42dec800f089373f29781302/sui/read_transaction_api.go#L55-L61

bad performance points:

  • gjson.ParseBytes(respBytes) call twice (go compiler may not optimize to call once)
  • gjson.ParseBytes would parse whole json to map[string]interface{} recursive

I provide a better_performance/much_faster code demo, benifit:

  • json.RawMessage just a string(lazy parsing), if response json has error would not parsing whole json
  • if no error second marshel Result field string to resp json instead of gjson.ParseBytes whole json
	type rpcRsp struct {
		Error  string          `json:"error,omitempty"`
		Result json.RawMessage `json:"result,omitempty"`
	}
	var r rpcRsp
	err = json.Unmarshal(respBytes, &r)
	if err != nil {
		return rsp, nil
	}
	if r.Error != "" {
		return rsp, errors.New(r.Error)
	}
	err = json.Unmarshal(r.Result, &rsp)
	if err != nil {
		return rsp, nil
	}

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions