Skip to content

Commit

Permalink
results: Faster JSON encoding and decoding (#506)
Browse files Browse the repository at this point in the history
* Add .idea to .gitignore

* results: Faster JSON encoding and decoding

This commit improves the performance of JSON encoding and decoding of
`Results`. Additionally, it fixes a regression introduced in #474 that
didn't produce valid JSON for HTTP headers. A test is added that
validates the JSON encoding matches the one produced by the
`encoding/json`.

```
name                            old time/op    new time/op    delta
ResultEncodings/json-encode-16     690ns ±12%     297ns ± 0%   -56.94%
(p=0.000 n=9+9)
ResultEncodings/json-decode-16    1.05µs ± 1%    0.03µs ± 1%   -97.47%
(p=0.000 n=10+8)

name                            old alloc/op   new alloc/op   delta
ResultEncodings/json-encode-16      804B ±87%        0B       -100.00%
(p=0.000 n=9+10)
ResultEncodings/json-decode-16      310B ± 0%        0B       -100.00%
(p=0.000 n=10+10)

name                            old allocs/op  new allocs/op  delta
ResultEncodings/json-encode-16      4.00 ± 0%      0.00       -100.00%
(p=0.000 n=10+10)
ResultEncodings/json-decode-16      4.00 ± 0%      0.00       -100.00%
(p=0.000 n=10+10)
```
  • Loading branch information
tsenart committed Mar 24, 2020
1 parent 19b7458 commit 3629602
Show file tree
Hide file tree
Showing 7 changed files with 297 additions and 242 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,4 @@ vendor
*.lz

.DS_Store
.idea
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@ require (
github.com/miekg/dns v1.1.17
github.com/streadway/quantile v0.0.0-20150917103942-b0c588724d25
github.com/tsenart/go-tsz v0.0.0-20180814232043-cdeb9e1e981e
github.com/valyala/fastjson v1.5.0
golang.org/x/net v0.0.0-20190827160401-ba9fcec4b297
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ github.com/streadway/quantile v0.0.0-20150917103942-b0c588724d25 h1:7z3LSn867ex6
github.com/streadway/quantile v0.0.0-20150917103942-b0c588724d25/go.mod h1:lbP8tGiBjZ5YWIc2fzuRpTaz0b/53vT6PEs3QuAWzuU=
github.com/tsenart/go-tsz v0.0.0-20180814232043-cdeb9e1e981e h1:bB5SXzQmSUsJCmjPDN9fKYx3SSDER5diSjlN6TefTCc=
github.com/tsenart/go-tsz v0.0.0-20180814232043-cdeb9e1e981e/go.mod h1:SWZznP1z5Ki7hDT2ioqiFKEse8K9tU2OUvaRI0NeGQo=
github.com/valyala/fastjson v1.5.0 h1:DGrb4wEYso2HdGLyLmNoyNCQnCWfjd8yhghPv5/5YQg=
github.com/valyala/fastjson v1.5.0/go.mod h1:CLCAqky6SMuOcxStkYQvblddUtoRxhYMGLrsQns1aXY=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190829043050-9756ffdc2472 h1:Gv7RPwsi3eZ2Fgewe3CBsuOebPwO27PoXzRpJPsvSSM=
golang.org/x/crypto v0.0.0-20190829043050-9756ffdc2472/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
Expand Down
34 changes: 0 additions & 34 deletions lib/results.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ import (
"strconv"
"strings"
"time"

"github.com/mailru/easyjson/jlexer"
jwriter "github.com/mailru/easyjson/jwriter"
)

func init() {
Expand Down Expand Up @@ -267,34 +264,3 @@ func NewCSVDecoder(r io.Reader) Decoder {
}
}

//go:generate easyjson -no_std_marshalers -output_filename results_easyjson.go results.go
//easyjson:json
type jsonResult Result

// NewJSONEncoder returns an Encoder that dumps the given *Results as a JSON
// object.
func NewJSONEncoder(w io.Writer) Encoder {
var jw jwriter.Writer
return func(r *Result) error {
(*jsonResult)(r).MarshalEasyJSON(&jw)
if jw.Error != nil {
return jw.Error
}
jw.RawByte('\n')
_, err := jw.DumpTo(w)
return err
}
}

// NewJSONDecoder returns a Decoder that decodes JSON encoded Results.
func NewJSONDecoder(r io.Reader) Decoder {
rd := bufio.NewReader(r)
return func(r *Result) (err error) {
var jl jlexer.Lexer
if jl.Data, err = rd.ReadBytes('\n'); err != nil {
return err
}
(*jsonResult)(r).UnmarshalEasyJSON(&jl)
return jl.Error()
}
}
205 changes: 0 additions & 205 deletions lib/results_easyjson.go

This file was deleted.

0 comments on commit 3629602

Please sign in to comment.