From 47549aa3d10808c063d45dcaa598887dadde59c5 Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Tue, 7 Feb 2023 12:28:35 -0700 Subject: compat: Enable the cache benchmark This should have been done back in 8aa12d3 (struct.go: Cache structIndexes, 2023-01-26) --- compat/json/borrowed_bench_test.go | 14 ++++++++------ compat/json/compat_test.go | 6 +++++- internal/jsonstruct/struct.go | 4 ++++ 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/compat/json/borrowed_bench_test.go b/compat/json/borrowed_bench_test.go index e2d74ab..443a13d 100644 --- a/compat/json/borrowed_bench_test.go +++ b/compat/json/borrowed_bench_test.go @@ -15,9 +15,13 @@ package json import ( "bytes" "compress/gzip" + "fmt" "io" "os" + "reflect" + "runtime" "strings" + "sync" "testing" ) @@ -325,11 +329,10 @@ func BenchmarkUnmapped(b *testing.B) { }) } -/* // MODIFIED: we don't have a cache func BenchmarkTypeFieldsCache(b *testing.B) { b.ReportAllocs() var maxTypes int = 1e6 - if testenv.Builder() != "" { + if false { // testenv.Builder() != "" { // MODIFIED: disabled maxTypes = 1e3 // restrict cache sizes on builders } @@ -345,9 +348,9 @@ func BenchmarkTypeFieldsCache(b *testing.B) { } // clearClear clears the cache. Other JSON operations, must not be running. - clearCache := func() { - fieldCache = sync.Map{} - } + // clearCache := func() { // MODIFIED: use function from compat_test.go + // fieldCache = sync.Map{} // MODIFIED: use function from compat_test.go + // } // MODIFIED: use function from compat_test.go // MissTypes tests the performance of repeated cache misses. // This measures the time to rebuild a cache of size nt. @@ -389,7 +392,6 @@ func BenchmarkTypeFieldsCache(b *testing.B) { }) } } -*/ // MODIFIED: we don't have a cache func BenchmarkEncodeMarshaler(b *testing.B) { b.ReportAllocs() diff --git a/compat/json/compat_test.go b/compat/json/compat_test.go index 2969107..203594f 100644 --- a/compat/json/compat_test.go +++ b/compat/json/compat_test.go @@ -14,7 +14,11 @@ import ( "git.lukeshu.com/go/lowmemjson/internal/jsontest" ) -var parseTag = jsonstruct.ParseTag +var ( + parseTag = jsonstruct.ParseTag + clearCache = jsonstruct.ClearCache + cachedTypeFields = jsonstruct.IndexStruct +) type scanner = lowmemjson.ReEncoderConfig diff --git a/internal/jsonstruct/struct.go b/internal/jsonstruct/struct.go index 830dc80..16c45de 100644 --- a/internal/jsonstruct/struct.go +++ b/internal/jsonstruct/struct.go @@ -37,6 +37,10 @@ func IndexStruct(typ reflect.Type) StructIndex { return ret } +func ClearCache() { + structIndexCache = typedsync.CacheMap[reflect.Type, StructIndex]{} +} + // indexStructReal is like indexStruct, but is the real indexer, // bypassing the cache. func indexStructReal(typ reflect.Type) StructIndex { -- cgit v1.1-4-g5e80