Skip to content

POC: encoder.Encode() using IndentWriter #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open

POC: encoder.Encode() using IndentWriter #2

wants to merge 4 commits into from

Conversation

flimzy
Copy link
Owner

@flimzy flimzy commented Aug 24, 2019

This PR is a POC for using the IndentWriter implementation in #1 to indent when using the json.Encoder.SetIndent, in addition to the new json.Encoder.SetDirectWrite, along with benchmarks to compare the before and after.

Before:

BenchmarkMarshalAndIndentStream_Int-4            3000000               578 ns/op             176 B/op          3 allocs/op
BenchmarkMarshalAndIndentStream_Optionals-4       500000              3265 ns/op             344 B/op          6 allocs/op
BenchmarkMarshalAndIndentStream_StringTag-4       500000              2660 ns/op             336 B/op          6 allocs/op

After:

BenchmarkMarshalAndIndentStream_Int-4            5000000               307 ns/op             224 B/op          3 allocs/op
BenchmarkMarshalAndIndentStream_Optionals-4      2000000               798 ns/op             224 B/op          3 allocs/op
BenchmarkMarshalAndIndentStream_StringTag-4      2000000               739 ns/op             232 B/op          4 allocs/op

So no improvement (and a slight memory hit) for really small values (a single int), but a clear improvement, both in terms of execution time, and memory usage/allocations, when using encoding and indenting structs.

flimzy added 4 commits August 19, 2019 18:47
BEFORE:
BenchmarkIndentExample1-2       20000000               116 ns/op           8.56 MB/s          64 B/op          1 allocs/op
BenchmarkIndentExample2-2       10000000               194 ns/op          10.30 MB/s          72 B/op          2 allocs/op
BenchmarkIndentExample3-2       10000000               180 ns/op          11.10 MB/s          72 B/op          2 allocs/op
BenchmarkIndentExample4-2        5000000               323 ns/op          18.56 MB/s          72 B/op          2 allocs/op
BenchmarkIndentExample5-2       10000000               261 ns/op          11.49 MB/s          72 B/op          2 allocs/op
BenchmarkIndentExample6-2        5000000               365 ns/op          19.13 MB/s          72 B/op          2 allocs/op
BenchmarkIndentExample7-2        5000000               321 ns/op          21.76 MB/s          72 B/op          2 allocs/op
BenchmarkIndentExample7b-2       1000000              1004 ns/op         696.72 MB/s         880 B/op          8 allocs/op

AFTER:
BenchmarkIndentExample1-2               10000000               139 ns/op           7.19 MB/s          64 B/op          1 allocs/op
BenchmarkIndentExample2-2               10000000               189 ns/op          10.58 MB/s          72 B/op          2 allocs/op
BenchmarkIndentExample3-2               10000000               204 ns/op           9.79 MB/s          72 B/op          2 allocs/op
BenchmarkIndentExample4-2                5000000               330 ns/op          18.18 MB/s          72 B/op          2 allocs/op
BenchmarkIndentExample5-2                5000000               276 ns/op          10.84 MB/s          72 B/op          2 allocs/op
BenchmarkIndentExample6-2                3000000               409 ns/op          17.09 MB/s          72 B/op          2 allocs/op
BenchmarkIndentExample7-2                5000000               342 ns/op          20.42 MB/s          72 B/op          2 allocs/op
BenchmarkIndentExample7b-2               1000000              1165 ns/op         600.74 MB/s         880 B/op          8 allocs/op
BenchmarkIndentStreamExample1-2          5000000               323 ns/op           3.09 MB/s         152 B/op          3 allocs/op
BenchmarkIndentStreamExample2-2          5000000               367 ns/op           5.44 MB/s         160 B/op          4 allocs/op
BenchmarkIndentStreamExample3-2          5000000               363 ns/op           5.50 MB/s         160 B/op          4 allocs/op
BenchmarkIndentStreamExample4-2          3000000               496 ns/op          12.08 MB/s         160 B/op          4 allocs/op
BenchmarkIndentStreamExample5-2          3000000               455 ns/op           6.58 MB/s         160 B/op          4 allocs/op
BenchmarkIndentStreamExample6-2          3000000               640 ns/op          10.94 MB/s         160 B/op          4 allocs/op
BenchmarkIndentStreamExample7-2          3000000               520 ns/op          13.45 MB/s         160 B/op          4 allocs/op
BenchmarkIndentStreamExample7b-2         1000000              1293 ns/op         541.27 MB/s         960 B/op          9 allocs/op
Results:
BenchmarkMarshalAndIndentStream_Int-4            3000000               578 ns/op             176 B/op          3 allocs/op
BenchmarkMarshalAndIndentStream_Optionals-4       500000              3265 ns/op             344 B/op          6 allocs/op
BenchmarkMarshalAndIndentStream_StringTag-4       500000              2660 ns/op             336 B/op          6 allocs/op
BenchmarkMarshalAndIndentStream_Int-4            5000000               307 ns/op             224 B/op          3 allocs/op
BenchmarkMarshalAndIndentStream_Optionals-4      2000000               798 ns/op             224 B/op          3 allocs/op
BenchmarkMarshalAndIndentStream_StringTag-4      2000000               739 ns/op             232 B/op          4 allocs/op
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant