Skip to content

Conversation

goccy
Copy link
Owner

@goccy goccy commented Jun 6, 2021

Summary

In the default behavior, go-json, like encoding/json, will reflect the result of the last evaluation when a field with the same name exists. I've added new options to allow you to change this behavior. json.DecodeFieldPriorityFirstWin option reflects the result of the first evaluation if a field with the same name exists. This behavior has a performance advantage as it allows the subsequent strings to be skipped if all fields have been evaluated.

Benchmark

Results

goos: darwin
goarch: amd64
pkg: benchmark
Benchmark_Decode_SmallStruct_Unmarshal_EncodingJson-16                            418848              2395 ns/op             400 B/op         10 allocs/op
Benchmark_Decode_SmallStruct_Unmarshal_FastJson-16                                720466              1624 ns/op            3376 B/op         11 allocs/op
Benchmark_Decode_SmallStruct_Unmarshal_JsonIter-16                               1602945               745 ns/op             208 B/op         13 allocs/op
Benchmark_Decode_SmallStruct_Unmarshal_GoJay-16                                  2604565               455 ns/op             256 B/op          2 allocs/op
Benchmark_Decode_SmallStruct_Unmarshal_GoJayUnsafe-16                            3026695               397 ns/op             112 B/op          1 allocs/op
Benchmark_Decode_SmallStruct_Unmarshal_SegmentioJson-16                          1260757               959 ns/op             192 B/op          5 allocs/op
Benchmark_Decode_SmallStruct_Unmarshal_GoJson-16                                 2852440               417 ns/op             256 B/op          2 allocs/op
Benchmark_Decode_SmallStruct_Unmarshal_GoJsonNoEscape-16                         3440616               348 ns/op             144 B/op          1 allocs/op
Benchmark_Decode_SmallStruct_Stream_EncodingJson-16                               429216              2710 ns/op            1104 B/op         12 allocs/op
Benchmark_Decode_SmallStruct_Stream_JsonIter-16                                  1236762               969 ns/op             872 B/op         16 allocs/op
Benchmark_Decode_SmallStruct_Stream_GoJay-16                                     2147300               562 ns/op             720 B/op          3 allocs/op
Benchmark_Decode_SmallStruct_Stream_SegmentioJson-16                              208017              5250 ns/op           32960 B/op          6 allocs/op
Benchmark_Decode_SmallStruct_Stream_GoJson-16                                    1919298               613 ns/op             728 B/op          4 allocs/op
Benchmark_Decode_MediumStruct_Unmarshal_EncodingJson-16                            79514             14821 ns/op             696 B/op         19 allocs/op
Benchmark_Decode_MediumStruct_Unmarshal_FastJson-16                               115572              9728 ns/op           17304 B/op         54 allocs/op
Benchmark_Decode_MediumStruct_Unmarshal_JsonIter-16                               214368              5383 ns/op             792 B/op         82 allocs/op
Benchmark_Decode_MediumStruct_Unmarshal_GoJay-16                                  361904              3341 ns/op            2449 B/op          8 allocs/op
Benchmark_Decode_MediumStruct_Unmarshal_GoJayUnsafe-16                            391798              2793 ns/op             144 B/op          7 allocs/op
Benchmark_Decode_MediumStruct_Unmarshal_SegmentioJson-16                          220917              5706 ns/op             296 B/op          9 allocs/op
Benchmark_Decode_MediumStruct_Unmarshal_GoJson-16                                 297412              3448 ns/op            2451 B/op          8 allocs/op
Benchmark_Decode_MediumStruct_Unmarshal_GoJsonNoEscape-16                         381306              3220 ns/op            2419 B/op          7 allocs/op
Benchmark_Decode_MediumStruct_Stream_EncodingJson-16                               65142             18784 ns/op            7136 B/op         26 allocs/op
Benchmark_Decode_MediumStruct_Stream_JsonIter-16                                  182212              6607 ns/op            1792 B/op         91 allocs/op
Benchmark_Decode_MediumStruct_Stream_GoJay-16                                     270672              4545 ns/op            7920 B/op         12 allocs/op
Benchmark_Decode_MediumStruct_Stream_SegmentioJson-16                              78078             14739 ns/op           33064 B/op         10 allocs/op
Benchmark_Decode_MediumStruct_Stream_GoJson-16                                    277111              4223 ns/op            3834 B/op         12 allocs/op
Benchmark_Decode_LargeStruct_Unmarshal_EncodingJson-16                              5744            204362 ns/op            6240 B/op        191 allocs/op
Benchmark_Decode_LargeStruct_Unmarshal_FastJson-16                                  7574            134493 ns/op          283204 B/op        540 allocs/op
Benchmark_Decode_LargeStruct_Unmarshal_JsonIter-16                                 10000            102447 ns/op           17179 B/op       1271 allocs/op
Benchmark_Decode_LargeStruct_Unmarshal_GoJay-16                                    29066             39675 ns/op           31252 B/op         77 allocs/op
Benchmark_Decode_LargeStruct_Unmarshal_GoJayUnsafe-16                              36532             32485 ns/op            2561 B/op         76 allocs/op
Benchmark_Decode_LargeStruct_Unmarshal_SegmentioJson-16                            13063             90192 ns/op            4400 B/op        132 allocs/op
Benchmark_Decode_LargeStruct_Unmarshal_GoJson-16                                   23830             44974 ns/op           30792 B/op         67 allocs/op
Benchmark_Decode_LargeStruct_Unmarshal_GoJsonNoEscape-16                           26092             45308 ns/op           30759 B/op         66 allocs/op
Benchmark_Decode_LargeStruct_Unmarshal_GoJsonFirstWinMode-16                       27903             38975 ns/op           30792 B/op         67 allocs/op
Benchmark_Decode_LargeStruct_Unmarshal_GoJsonNoEscapeFirstWinMode-16               30661             39387 ns/op           30760 B/op         66 allocs/op
Benchmark_Decode_LargeStruct_Stream_EncodingJson-16                                 4974            229413 ns/op           70016 B/op        201 allocs/op
Benchmark_Decode_LargeStruct_Stream_JsonIter-16                                     9038            110930 ns/op           21320 B/op       1398 allocs/op
Benchmark_Decode_LargeStruct_Stream_GoJay-16                                       25328             47193 ns/op           67680 B/op         84 allocs/op
Benchmark_Decode_LargeStruct_Stream_SegmentioJson-16                                7567            152554 ns/op           37168 B/op        133 allocs/op
Benchmark_Decode_LargeStruct_Stream_GoJson-16                                      21698             54857 ns/op           34464 B/op         74 allocs/op
Benchmark_Decode_LargeStruct_Stream_GoJsonFirstWinMode-16                          24721             48504 ns/op           34465 B/op         74 allocs/op
Benchmark_Decode_SlowReader_EncodingJson/chunksize_16384-16                         4630            259097 ns/op          114516 B/op        403 allocs/op
Benchmark_Decode_SlowReader_EncodingJson/chunksize_4096-16                          4366            255513 ns/op          110429 B/op        406 allocs/op
Benchmark_Decode_SlowReader_EncodingJson/chunksize_1024-16                          4538            261276 ns/op          110418 B/op        425 allocs/op
Benchmark_Decode_SlowReader_EncodingJson/chunksize_256-16                           4420            268734 ns/op          110428 B/op        507 allocs/op
Benchmark_Decode_SlowReader_EncodingJson/chunksize_64-16                            3378            306652 ns/op          110403 B/op        836 allocs/op
Benchmark_Decode_SlowReader_GoJson/chunksize_16384-16                              13065             93375 ns/op           79034 B/op        276 allocs/op
Benchmark_Decode_SlowReader_GoJson/chunksize_4096-16                               13022             91710 ns/op           79034 B/op        280 allocs/op
Benchmark_Decode_SlowReader_GoJson/chunksize_1024-16                               12795             92556 ns/op           75957 B/op        299 allocs/op
Benchmark_Decode_SlowReader_GoJson/chunksize_256-16                                12289             97962 ns/op           75011 B/op        381 allocs/op
Benchmark_Decode_SlowReader_GoJson/chunksize_64-16                                 10000            117662 ns/op           74938 B/op        711 allocs/op

@codecov-commenter
Copy link

Codecov Report

Merging #242 (5c39787) into master (7d7af9f) will decrease coverage by 0.10%.
The diff coverage is 65.06%.

@@            Coverage Diff             @@
##           master     #242      +/-   ##
==========================================
- Coverage   81.61%   81.51%   -0.11%     
==========================================
  Files          46       46              
  Lines       14449    14537      +88     
==========================================
+ Hits        11793    11850      +57     
- Misses       2113     2141      +28     
- Partials      543      546       +3     

@goccy goccy merged commit b05a0c3 into master Jun 6, 2021
@goccy goccy deleted the feature/improve-decoder-performance branch June 6, 2021 09:38
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.

2 participants