• Joe Tsai's avatar
    encoding/json: use sync.Map for field cache · f0756ca2
    Joe Tsai authored
    The previous type cache is quadratic in time in the situation where
    new types are continually encountered. Now that it is possible to dynamically
    create new types with the reflect package, this can cause json to
    perform very poorly.
    
    Switch to sync.Map which does well when the cache has hit steady state,
    but also handles occasional updates in better than quadratic time.
    
    benchmark                                     old ns/op      new ns/op     delta
    BenchmarkTypeFieldsCache/MissTypes1-8         14817          16202         +9.35%
    BenchmarkTypeFieldsCache/MissTypes10-8        70926          69144         -2.51%
    BenchmarkTypeFieldsCache/MissTypes100-8       976467         208973        -78.60%
    BenchmarkTypeFieldsCache/MissTypes1000-8      79520162       1750371       -97.80%
    BenchmarkTypeFieldsCache/MissTypes10000-8     6873625837     16847806      -99.75%
    BenchmarkTypeFieldsCache/HitTypes1000-8       7.51           8.80          +17.18%
    BenchmarkTypeFieldsCache/HitTypes10000-8      7.58           8.68          +14.51%
    
    The old implementation takes 12 minutes just to build a cache of size 1e5
    due to the quadratic behavior. I did not bother benchmark sizes above that.
    
    Change-Id: I5e6facc1eb8e1b80e5ca285e4dd2cc8815618dad
    Reviewed-on: https://go-review.googlesource.com/76850
    Run-TryBot: Joe Tsai <thebrokentoaster@gmail.com>
    Reviewed-by: 's avatarBryan Mills <bcmills@google.com>
    TryBot-Result: Gobot Gobot <gobot@golang.org>
    f0756ca2
bench_test.go 7.08 KB