]> Cypherpunks repositories - gostls13.git/commit
encoding/json: simplify the structEncoder type
authorDaniel Martí <mvdan@mvdan.cc>
Sun, 22 Jul 2018 11:26:38 +0000 (12:26 +0100)
committerDaniel Martí <mvdan@mvdan.cc>
Wed, 22 Aug 2018 15:08:21 +0000 (15:08 +0000)
commit8148726676d63c2aebc561717a949135389868b8
treea75e922e6a15c82c0bd8964f46310f6d10476e06
parentf4e4ec2cd09c2f9d821f3cb6f47edd7c41a90b25
encoding/json: simplify the structEncoder type

structEncoder had two slices - the list of fields, and a list containing
the encoder for each field. structEncoder.encode then looped over the
fields, and indexed into the second slice to grab the field encoder.

However, this makes it very hard for the compiler to be able to prove
that the two slices always have the same length, and that the index
expression doesn't need a bounds check.

Merge the two slices into one to completely remove the need for bounds
checks in the hot loop.

While at it, don't copy the field elements when ranging, which greatly
speeds up the hot loop in structEncoder.

name           old time/op    new time/op    delta
CodeEncoder-4    6.18ms ± 0%    5.56ms ± 0%  -10.08%  (p=0.002 n=6+6)

name           old speed      new speed      delta
CodeEncoder-4   314MB/s ± 0%   349MB/s ± 0%  +11.21%  (p=0.002 n=6+6)

name           old alloc/op   new alloc/op   delta
CodeEncoder-4    93.2kB ± 0%    62.1kB ± 0%  -33.33%  (p=0.002 n=6+6)

Updates #5683.

Change-Id: I0dd47783530f439b125e084aede09dda172eb1e8
Reviewed-on: https://go-review.googlesource.com/125416
Run-TryBot: Daniel Martí <mvdan@mvdan.cc>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
src/encoding/json/encode.go