| 1 | // Copyright 2022 The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | package jsonscip-go gomod k8s.io/kube-openapi . `k8s.io/kube-openapi/pkg/internal/third_party/go-json-experiment/json`/ |
| 6 | |
| 7 | import ( |
| 8 | "bytesscip-go gomod github.com/golang/go/src go1.21 bytes/" |
| 9 | "fmtscip-go gomod github.com/golang/go/src go1.21 fmt/" |
| 10 | "ioscip-go gomod github.com/golang/go/src go1.21 io/" |
| 11 | "testingscip-go gomod github.com/golang/go/src go1.21 testing/" |
| 12 | ) |
| 13 | |
| 14 | func TestInternscip-go gomod k8s.io/kube-openapi . `k8s.io/kube-openapi/pkg/internal/third_party/go-json-experiment/json`/TestIntern().(t *testingscip-go gomod github.com/golang/go/src go1.21 testing/.Tscip-go gomod github.com/golang/go/src go1.21 testing/T#) { |
| 15 | var sc stringCachescip-go gomod k8s.io/kube-openapi . `k8s.io/kube-openapi/pkg/internal/third_party/go-json-experiment/json`/stringCache# |
| 16 | const alphabet = "abcdefghijklmnopqrstuvwxyz" |
| 17 | for i := 0; i <= len(alphabet); i++ { |
| 18 | want := alphabet[i:] |
| 19 | if got := sc.makescip-go gomod k8s.io/kube-openapi . `k8s.io/kube-openapi/pkg/internal/third_party/go-json-experiment/json`/stringCache#make().([]byte(want)); got != want { |
| 20 | t.Fatalfscip-go gomod github.com/golang/go/src go1.21 testing/common#Fatalf().("make = %v, want %v", got, want) |
| 21 | } |
| 22 | } |
| 23 | for i := 0; i < 1000; i++ { |
| 24 | want := fmtscip-go gomod github.com/golang/go/src go1.21 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.21 fmt/Sprintf().("test%b", i) |
| 25 | if got := sc.makescip-go gomod k8s.io/kube-openapi . `k8s.io/kube-openapi/pkg/internal/third_party/go-json-experiment/json`/stringCache#make().([]byte(want)); got != want { |
| 26 | t.Fatalfscip-go gomod github.com/golang/go/src go1.21 testing/common#Fatalf().("make = %v, want %v", got, want) |
| 27 | } |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | var sinkscip-go gomod k8s.io/kube-openapi . `k8s.io/kube-openapi/pkg/internal/third_party/go-json-experiment/json`/sink. string |
| 32 | |
| 33 | func BenchmarkInternscip-go gomod k8s.io/kube-openapi . `k8s.io/kube-openapi/pkg/internal/third_party/go-json-experiment/json`/BenchmarkIntern().(b *testingscip-go gomod github.com/golang/go/src go1.21 testing/.Bscip-go gomod github.com/golang/go/src go1.21 testing/B#) { |
| 34 | datasetStrings := func(name string) (out [][]byte) { |
| 35 | var data []byte |
| 36 | for _, ts := range jsonTestdatascip-go gomod k8s.io/kube-openapi . `k8s.io/kube-openapi/pkg/internal/third_party/go-json-experiment/json`/jsonTestdata().() { |
| 37 | if ts.namescip-go gomod k8s.io/kube-openapi . `k8s.io/kube-openapi/pkg/internal/third_party/go-json-experiment/json`/jsonTestdataEntry#name. == name { |
| 38 | data = ts.datascip-go gomod k8s.io/kube-openapi . `k8s.io/kube-openapi/pkg/internal/third_party/go-json-experiment/json`/jsonTestdataEntry#data. |
| 39 | } |
| 40 | } |
| 41 | dec := NewDecoderscip-go gomod k8s.io/kube-openapi . `k8s.io/kube-openapi/pkg/internal/third_party/go-json-experiment/json`/NewDecoder().(bytesscip-go gomod github.com/golang/go/src go1.21 bytes/.NewReaderscip-go gomod github.com/golang/go/src go1.21 bytes/NewReader().(data)) |
| 42 | for { |
| 43 | k, n := dec.StackIndexscip-go gomod k8s.io/kube-openapi . `k8s.io/kube-openapi/pkg/internal/third_party/go-json-experiment/json`/Decoder#StackIndex().(dec.StackDepthscip-go gomod k8s.io/kube-openapi . `k8s.io/kube-openapi/pkg/internal/third_party/go-json-experiment/json`/Decoder#StackDepth().()) |
| 44 | isObjectName := k == '{' && n%2 == 0 |
| 45 | tok, err := dec.ReadTokenscip-go gomod k8s.io/kube-openapi . `k8s.io/kube-openapi/pkg/internal/third_party/go-json-experiment/json`/Decoder#ReadToken().() |
| 46 | if err != nil { |
| 47 | if err == ioscip-go gomod github.com/golang/go/src go1.21 io/.EOFscip-go gomod github.com/golang/go/src go1.21 io/EOF. { |
| 48 | break |
| 49 | } |
| 50 | b.Fatalfscip-go gomod github.com/golang/go/src go1.21 testing/common#Fatalf().("ReadToken error: %v", err) |
| 51 | } |
| 52 | if tok.Kindscip-go gomod k8s.io/kube-openapi . `k8s.io/kube-openapi/pkg/internal/third_party/go-json-experiment/json`/Token#Kind().() == '"' && !isObjectName { |
| 53 | out = append(out, []byte(tok.Stringscip-go gomod k8s.io/kube-openapi . `k8s.io/kube-openapi/pkg/internal/third_party/go-json-experiment/json`/Token#String().())) |
| 54 | } |
| 55 | } |
| 56 | return out |
| 57 | } |
| 58 | |
| 59 | tests := []struct { |
| 60 | label string |
| 61 | data [][]byte |
| 62 | }{ |
| 63 | // Best is the best case scenario where every string is the same. |
| 64 | {"Best", func() (out [][]byte) { |
| 65 | for i := 0; i < 1000; i++ { |
| 66 | out = append(out, []byte("hello, world!")) |
| 67 | } |
| 68 | return out |
| 69 | }()}, |
| 70 | |
| 71 | // Repeat is a sequence of the same set of names repeated. |
| 72 | // This commonly occurs when unmarshaling a JSON array of JSON objects, |
| 73 | // where the set of all names is usually small. |
| 74 | {"Repeat", func() (out [][]byte) { |
| 75 | for i := 0; i < 100; i++ { |
| 76 | for _, s := range []string{"first_name", "last_name", "age", "address", "street_address", "city", "state", "postal_code", "phone_numbers", "gender"} { |
| 77 | out = append(out, []byte(s)) |
| 78 | } |
| 79 | } |
| 80 | return out |
| 81 | }()}, |
| 82 | |
| 83 | // Synthea is all string values encountered in the Synthea FHIR dataset. |
| 84 | {"Synthea", datasetStrings("SyntheaFhir")}, |
| 85 | |
| 86 | // Twitter is all string values encountered in the Twitter dataset. |
| 87 | {"Twitter", datasetStrings("TwitterStatus")}, |
| 88 | |
| 89 | // Worst is the worst case scenario where every string is different |
| 90 | // resulting in wasted time looking up a string that will never match. |
| 91 | {"Worst", func() (out [][]byte) { |
| 92 | for i := 0; i < 1000; i++ { |
| 93 | out = append(out, []byte(fmtscip-go gomod github.com/golang/go/src go1.21 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.21 fmt/Sprintf().("%016x", i))) |
| 94 | } |
| 95 | return out |
| 96 | }()}, |
| 97 | } |
| 98 | |
| 99 | for _, tt := range tests { |
| 100 | b.Runscip-go gomod github.com/golang/go/src go1.21 testing/B#Run().(tt.label, func(b *testingscip-go gomod github.com/golang/go/src go1.21 testing/.Bscip-go gomod github.com/golang/go/src go1.21 testing/B#) { |
| 101 | // Alloc simply heap allocates each string. |
| 102 | // This provides an upper bound on the number of allocations. |
| 103 | b.Runscip-go gomod github.com/golang/go/src go1.21 testing/B#Run().("Alloc", func(b *testingscip-go gomod github.com/golang/go/src go1.21 testing/.Bscip-go gomod github.com/golang/go/src go1.21 testing/B#) { |
| 104 | b.ReportAllocsscip-go gomod github.com/golang/go/src go1.21 testing/B#ReportAllocs().() |
| 105 | for i := 0; i < b.Nscip-go gomod github.com/golang/go/src go1.21 testing/B#N.; i++ { |
| 106 | for _, b := range tt.data { |
| 107 | sinkscip-go gomod k8s.io/kube-openapi . `k8s.io/kube-openapi/pkg/internal/third_party/go-json-experiment/json`/sink. = string(b) |
| 108 | } |
| 109 | } |
| 110 | }) |
| 111 | // Cache interns strings using stringCache. |
| 112 | // We want to optimize for having a faster runtime than Alloc, |
| 113 | // and also keeping the number of allocations closer to GoMap. |
| 114 | b.Runscip-go gomod github.com/golang/go/src go1.21 testing/B#Run().("Cache", func(b *testingscip-go gomod github.com/golang/go/src go1.21 testing/.Bscip-go gomod github.com/golang/go/src go1.21 testing/B#) { |
| 115 | b.ReportAllocsscip-go gomod github.com/golang/go/src go1.21 testing/B#ReportAllocs().() |
| 116 | for i := 0; i < b.Nscip-go gomod github.com/golang/go/src go1.21 testing/B#N.; i++ { |
| 117 | var sc stringCachescip-go gomod k8s.io/kube-openapi . `k8s.io/kube-openapi/pkg/internal/third_party/go-json-experiment/json`/stringCache# |
| 118 | for _, b := range tt.data { |
| 119 | sinkscip-go gomod k8s.io/kube-openapi . `k8s.io/kube-openapi/pkg/internal/third_party/go-json-experiment/json`/sink. = sc.makescip-go gomod k8s.io/kube-openapi . `k8s.io/kube-openapi/pkg/internal/third_party/go-json-experiment/json`/stringCache#make().(b) |
| 120 | } |
| 121 | } |
| 122 | }) |
| 123 | // GoMap interns all strings in a simple Go map. |
| 124 | // This provides a lower bound on the number of allocations. |
| 125 | b.Runscip-go gomod github.com/golang/go/src go1.21 testing/B#Run().("GoMap", func(b *testingscip-go gomod github.com/golang/go/src go1.21 testing/.Bscip-go gomod github.com/golang/go/src go1.21 testing/B#) { |
| 126 | b.ReportAllocsscip-go gomod github.com/golang/go/src go1.21 testing/B#ReportAllocs().() |
| 127 | for i := 0; i < b.Nscip-go gomod github.com/golang/go/src go1.21 testing/B#N.; i++ { |
| 128 | m := make(map[string]string) |
| 129 | for _, b := range tt.data { |
| 130 | s, ok := m[string(b)] |
| 131 | if !ok { |
| 132 | s = string(b) |
| 133 | m[s] = s |
| 134 | } |
| 135 | sinkscip-go gomod k8s.io/kube-openapi . `k8s.io/kube-openapi/pkg/internal/third_party/go-json-experiment/json`/sink. = s |
| 136 | } |
| 137 | } |
| 138 | }) |
| 139 | }) |
| 140 | } |
| 141 | } |