| 1 |
package encoder |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"math/rand" |
| 6 |
"testing" |
| 7 |
|
| 8 |
"github.com/goccy/go-json/internal/runtime" |
| 9 |
) |
| 10 |
|
| 11 |
// The shapes of the table of the recent opcodes of a context, measured against each other: the number of |
| 12 |
// the sets, which the address of a type is hashed to, and the number of the entries of a set, which hold |
| 13 |
// the types of the set encoded last. A lookup which misses goes to the table shared by every goroutine, as |
| 14 |
// CompileToGetCodeSet does, so a miss costs what it costs the encoder. |
| 15 |
// |
| 16 |
// The patterns are the types encoded by turns in a Marshal: the type passed to Marshal and the types held by |
| 17 |
// its values of interface{}. The types of a pattern are in the same set or in different sets, as their |
| 18 |
// addresses in a binary may have them. |
| 19 |
|
| 20 |
type recentVariantEntry struct { |
| 21 |
typeptr uintptr |
| 22 |
codeSet *OpcodeSet |
| 23 |
} |
| 24 |
|
| 25 |
// recentVariantTable is a table of the recent opcodes of a shape. A lookup compares the entries of the set of |
| 26 |
// the type in their order; an insertion puts the type first and drops the last entry of the set. |
| 27 |
type recentVariantTable interface { |
| 28 |
lookup(typeptr uintptr) *OpcodeSet |
| 29 |
insert(typeptr uintptr, codeSet *OpcodeSet) |
| 30 |
shape() string |
| 31 |
} |
| 32 |
|
| 33 |
type recentVariant16x1 [16][1]recentVariantEntry |
| 34 |
type recentVariant16x2 [16][2]recentVariantEntry |
| 35 |
type recentVariant16x4 [16][4]recentVariantEntry |
| 36 |
type recentVariant32x2 [32][2]recentVariantEntry |
| 37 |
type recentVariant64x2 [64][2]recentVariantEntry |
| 38 |
type recentVariant32x4 [32][4]recentVariantEntry |
| 39 |
|
| 40 |
func recentVariantIndex(typeptr uintptr, bits uint) uint64 { |
| 41 |
return (uint64(typeptr) * runtime.TypeHashMultiplier) >> (64 - bits) |
| 42 |
} |
| 43 |
|
| 44 |
func (t *recentVariant16x1) shape() string { return "16x1" } |
| 45 |
func (t *recentVariant16x1) lookup(typeptr uintptr) *OpcodeSet { |
| 46 |
set := &t[recentVariantIndex(typeptr, 4)] |
| 47 |
if set[0].typeptr == typeptr { |
| 48 |
return set[0].codeSet |
| 49 |
} |
| 50 |
return nil |
| 51 |
} |
| 52 |
func (t *recentVariant16x1) insert(typeptr uintptr, codeSet *OpcodeSet) { |
| 53 |
set := &t[recentVariantIndex(typeptr, 4)] |
| 54 |
set[0] = recentVariantEntry{typeptr, codeSet} |
| 55 |
} |
| 56 |
|
| 57 |
func (t *recentVariant16x2) shape() string { return "16x2" } |
| 58 |
func (t *recentVariant16x2) lookup(typeptr uintptr) *OpcodeSet { |
| 59 |
set := &t[recentVariantIndex(typeptr, 4)] |
| 60 |
if set[0].typeptr == typeptr { |
| 61 |
return set[0].codeSet |
| 62 |
} |
| 63 |
if set[1].typeptr == typeptr { |
| 64 |
return set[1].codeSet |
| 65 |
} |
| 66 |
return nil |
| 67 |
} |
| 68 |
func (t *recentVariant16x2) insert(typeptr uintptr, codeSet *OpcodeSet) { |
| 69 |
set := &t[recentVariantIndex(typeptr, 4)] |
| 70 |
set[1] = set[0] |
| 71 |
set[0] = recentVariantEntry{typeptr, codeSet} |
| 72 |
} |
| 73 |
|
| 74 |
func (t *recentVariant32x2) shape() string { return "32x2" } |
| 75 |
func (t *recentVariant32x2) lookup(typeptr uintptr) *OpcodeSet { |
| 76 |
set := &t[recentVariantIndex(typeptr, 5)] |
| 77 |
if set[0].typeptr == typeptr { |
| 78 |
return set[0].codeSet |
| 79 |
} |
| 80 |
if set[1].typeptr == typeptr { |
| 81 |
return set[1].codeSet |
| 82 |
} |
| 83 |
return nil |
| 84 |
} |
| 85 |
func (t *recentVariant32x2) insert(typeptr uintptr, codeSet *OpcodeSet) { |
| 86 |
set := &t[recentVariantIndex(typeptr, 5)] |
| 87 |
set[1] = set[0] |
| 88 |
set[0] = recentVariantEntry{typeptr, codeSet} |
| 89 |
} |
| 90 |
|
| 91 |
func (t *recentVariant64x2) shape() string { return "64x2" } |
| 92 |
func (t *recentVariant64x2) lookup(typeptr uintptr) *OpcodeSet { |
| 93 |
set := &t[recentVariantIndex(typeptr, 6)] |
| 94 |
if set[0].typeptr == typeptr { |
| 95 |
return set[0].codeSet |
| 96 |
} |
| 97 |
if set[1].typeptr == typeptr { |
| 98 |
return set[1].codeSet |
| 99 |
} |
| 100 |
return nil |
| 101 |
} |
| 102 |
func (t *recentVariant64x2) insert(typeptr uintptr, codeSet *OpcodeSet) { |
| 103 |
set := &t[recentVariantIndex(typeptr, 6)] |
| 104 |
set[1] = set[0] |
| 105 |
set[0] = recentVariantEntry{typeptr, codeSet} |
| 106 |
} |
| 107 |
|
| 108 |
func (t *recentVariant16x4) shape() string { return "16x4" } |
| 109 |
func (t *recentVariant16x4) lookup(typeptr uintptr) *OpcodeSet { |
| 110 |
set := &t[recentVariantIndex(typeptr, 4)] |
| 111 |
if set[0].typeptr == typeptr { |
| 112 |
return set[0].codeSet |
| 113 |
} |
| 114 |
if set[1].typeptr == typeptr { |
| 115 |
return set[1].codeSet |
| 116 |
} |
| 117 |
if set[2].typeptr == typeptr { |
| 118 |
return set[2].codeSet |
| 119 |
} |
| 120 |
if set[3].typeptr == typeptr { |
| 121 |
return set[3].codeSet |
| 122 |
} |
| 123 |
return nil |
| 124 |
} |
| 125 |
func (t *recentVariant16x4) insert(typeptr uintptr, codeSet *OpcodeSet) { |
| 126 |
set := &t[recentVariantIndex(typeptr, 4)] |
| 127 |
set[3], set[2], set[1] = set[2], set[1], set[0] |
| 128 |
set[0] = recentVariantEntry{typeptr, codeSet} |
| 129 |
} |
| 130 |
|
| 131 |
func (t *recentVariant32x4) shape() string { return "32x4" } |
| 132 |
func (t *recentVariant32x4) lookup(typeptr uintptr) *OpcodeSet { |
| 133 |
set := &t[recentVariantIndex(typeptr, 5)] |
| 134 |
if set[0].typeptr == typeptr { |
| 135 |
return set[0].codeSet |
| 136 |
} |
| 137 |
if set[1].typeptr == typeptr { |
| 138 |
return set[1].codeSet |
| 139 |
} |
| 140 |
if set[2].typeptr == typeptr { |
| 141 |
return set[2].codeSet |
| 142 |
} |
| 143 |
if set[3].typeptr == typeptr { |
| 144 |
return set[3].codeSet |
| 145 |
} |
| 146 |
return nil |
| 147 |
} |
| 148 |
func (t *recentVariant32x4) insert(typeptr uintptr, codeSet *OpcodeSet) { |
| 149 |
set := &t[recentVariantIndex(typeptr, 5)] |
| 150 |
set[3], set[2], set[1] = set[2], set[1], set[0] |
| 151 |
set[0] = recentVariantEntry{typeptr, codeSet} |
| 152 |
} |
| 153 |
|
| 154 |
// recentVariantTypes returns n addresses of types which are in the set of the first one in a table of the |
| 155 |
// bits ( sameSet ), or in sets of their own ( !sameSet ): the smaller the table, the more likely the same set. |
| 156 |
// They are hashed to the same set of every table with fewer bits too, as the set is the top bits of the hash. |
| 157 |
func recentVariantTypes(n int, bits uint, sameSet bool) []uintptr { |
| 158 |
r := rand.New(rand.NewSource(1)) |
| 159 |
// what the addresses of the types of a binary look like: 8 byte aligned, in a range of a few MB. |
| 160 |
random := func() uintptr { return 0x1000000 + uintptr(r.Intn(1<<20))*8 } |
| 161 |
typeptrs := []uintptr{random()} |
| 162 |
seen := map[uint64]bool{recentVariantIndex(typeptrs[0], bits): true} |
| 163 |
for len(typeptrs) < n { |
| 164 |
typeptr := random() |
| 165 |
set := recentVariantIndex(typeptr, bits) |
| 166 |
if sameSet != (set == recentVariantIndex(typeptrs[0], bits)) || (!sameSet && seen[set]) { |
| 167 |
continue |
| 168 |
} |
| 169 |
seen[set] = true |
| 170 |
typeptrs = append(typeptrs, typeptr) |
| 171 |
} |
| 172 |
return typeptrs |
| 173 |
} |
| 174 |
|
| 175 |
// the patterns of the types encoded by turns: their number, and whether they are in the same set of the |
| 176 |
// table of the most sets measured ( then they are in the same set of every table ). |
| 177 |
var recentVariantPatterns = []struct { |
| 178 |
types int |
| 179 |
sameSet bool |
| 180 |
}{ |
| 181 |
{1, false}, |
| 182 |
{2, false}, |
| 183 |
{2, true}, |
| 184 |
{3, false}, |
| 185 |
{3, true}, |
| 186 |
{4, true}, |
| 187 |
{8, false}, |
| 188 |
} |
| 189 |
|
| 190 |
func BenchmarkVariant_RecentCodeSets(b *testing.B) { |
| 191 |
tables := []recentVariantTable{ |
| 192 |
&recentVariant16x1{}, |
| 193 |
&recentVariant16x2{}, &recentVariant32x2{}, &recentVariant64x2{}, |
| 194 |
&recentVariant16x4{}, &recentVariant32x4{}, |
| 195 |
} |
| 196 |
for _, pattern := range recentVariantPatterns { |
| 197 |
typeptrs := recentVariantTypes(pattern.types, 6, pattern.sameSet) |
| 198 |
// the table shared by every goroutine, which a miss goes to: it has the types, compiled. |
| 199 |
var shared runtime.TypeCache[OpcodeSet] |
| 200 |
for _, typeptr := range typeptrs { |
| 201 |
shared.Store(typeptr, &OpcodeSet{}) |
| 202 |
} |
| 203 |
// the sequence of the lookups of a Marshal: the type passed to it, then each of the others, and the |
| 204 |
// first one again for the next Marshal. |
| 205 |
sequence := make([]uintptr, 0, 64) |
| 206 |
for len(sequence) < 64 { |
| 207 |
sequence = append(sequence, typeptrs...) |
| 208 |
} |
| 209 |
set := "separate sets" |
| 210 |
if pattern.sameSet { |
| 211 |
set = "same set" |
| 212 |
} |
| 213 |
for _, table := range tables { |
| 214 |
b.Run(fmt.Sprintf("%s/%d types %s", table.shape(), pattern.types, set), func(b *testing.B) { |
| 215 |
var hits, misses int |
| 216 |
for i := 0; i < b.N; i++ { |
| 217 |
for _, typeptr := range sequence { |
| 218 |
codeSet := table.lookup(typeptr) |
| 219 |
if codeSet == nil { |
| 220 |
codeSet = shared.Load(typeptr) |
| 221 |
table.insert(typeptr, codeSet) |
| 222 |
misses++ |
| 223 |
} else { |
| 224 |
hits++ |
| 225 |
} |
| 226 |
} |
| 227 |
} |
| 228 |
b.ReportMetric(float64(b.Elapsed().Nanoseconds())/float64(b.N*len(sequence)), "ns/lookup") |
| 229 |
b.ReportMetric(float64(misses)/float64(hits+misses)*100, "miss%") |
| 230 |
}) |
| 231 |
} |
| 232 |
} |
| 233 |
} |