2
* Copyright 2021 ByteDance Inc.
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
8
* http://www.apache.org/licenses/LICENSE-2.0
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
23
`github.com/bytedance/sonic/internal/rt`
32
type FieldEntry struct {
39
FieldMap_N = int64(unsafe.Offsetof(FieldMap{}.N))
40
FieldMap_b = int64(unsafe.Offsetof(FieldMap{}.b))
41
FieldEntrySize = int64(unsafe.Sizeof(FieldEntry{}))
44
func newBucket(n int) unsafe.Pointer {
45
v := make([]FieldEntry, n)
46
return (*rt.GoSlice)(unsafe.Pointer(&v)).Ptr
49
func CreateFieldMap(n int) *FieldMap {
52
b: newBucket(n * 2), // LoadFactor = 0.5
53
m: make(map[string]int, n * 2),
57
func (self *FieldMap) At(p uint64) *FieldEntry {
58
off := uintptr(p) * uintptr(FieldEntrySize)
59
return (*FieldEntry)(unsafe.Pointer(uintptr(self.b) + off))
62
// Get searches FieldMap by name. JIT generated assembly does NOT call this
63
// function, rather it implements its own version directly in assembly. So
64
// we must ensure this function stays in sync with the JIT generated one.
65
func (self *FieldMap) Get(name string) int {
71
* the hash map is never full, so the loop will always terminate */
73
if s.Hash == h && s.Name == name {
85
func (self *FieldMap) Set(name string, i int) {
90
/* searching for an empty slot;
91
* the hash map is never full, so the loop will always terminate */
102
/* add the case-insensitive version, prefer the one with smaller field ID */
103
key := strings.ToLower(name)
104
if v, ok := self.m[key]; !ok || i < v {
109
func (self *FieldMap) GetCaseInsensitive(name string) int {
110
if i, ok := self.m[strings.ToLower(name)]; ok {