diff --git a/conv/j2t/conv_test.go b/conv/j2t/conv_test.go index c09677be..898219ff 100644 --- a/conv/j2t/conv_test.go +++ b/conv/j2t/conv_test.go @@ -166,6 +166,25 @@ func TestConvJSON2Thrift(t *testing.T) { require.Equal(t, exp, act) } +func TestConvJSON2Thrift_ForceHashMapAsFieldNameMap(t *testing.T) { + desc := getExampleDescByName("ExampleMethod", true, thrift.Options{ + ForceHashMapAsFieldNameMap: true, + }) + data := getExampleData() + cv := NewBinaryConv(conv.Options{}) + out, err := cv.Do(context.Background(), desc, data) + require.NoError(t, err) + + exp := example3.NewExampleReq() + err = json.Unmarshal(data, exp) + require.NoError(t, err) + + act := example3.NewExampleReq() + _, err = act.FastRead(out) + require.NoError(t, err) + require.Equal(t, exp, act) +} + func TestConvHTTP2Thrift(t *testing.T) { desc := getExampleDesc() data := getExampleData() diff --git a/internal/util/fieldmap.go b/internal/util/fieldmap.go index 00ae202a..0ac5c8fb 100644 --- a/internal/util/fieldmap.go +++ b/internal/util/fieldmap.go @@ -35,12 +35,12 @@ type FieldNameMap struct { maxKeyLength int all []caching.Pair trie *caching.TrieTree - hash map[string]unsafe.Pointer + hash *caching.HashMap } func NewFieldNameMap() *FieldNameMap { return &FieldNameMap{ - hash: make(map[string]unsafe.Pointer, defaultMapSize), + hash: caching.NewHashMap(defaultMapSize, defaultHashMapLoadFactor), } } @@ -68,7 +68,7 @@ func (ft FieldNameMap) Get(k string) unsafe.Pointer { if ft.trie != nil { return (unsafe.Pointer)(ft.trie.Get(k)) } else if ft.hash != nil { - return (unsafe.Pointer)(ft.hash[k]) + return (unsafe.Pointer)(ft.hash.Get(k)) } return nil } @@ -81,7 +81,7 @@ func (ft FieldNameMap) All() []caching.Pair { // Size returns the size of the map func (ft FieldNameMap) Size() int { if ft.hash != nil { - return len(ft.hash) + return ft.hash.Size() } else if ft.trie != nil { return ft.trie.Size() } @@ -162,18 +162,18 @@ func (ft *FieldNameMap) Build(noTrieTree bool) { // no ideal position or force use hash map ft.trie = nil - ft.hash = make(map[string]unsafe.Pointer, len(ft.all)) + ft.hash = caching.NewHashMap(len(ft.all), defaultHashMapLoadFactor) // set all key-values to the trie tree for _, v := range ft.all { // caching.HashMap does not support duplicate key, so must check if the key exists before set // WARN: if the key exists, the value WON'T be replaced - o := ft.hash[v.Key] + o := ft.hash.Get(v.Key) if o == nil { - ft.hash[v.Key] = v.Val + ft.hash.Set(v.Key, v.Val) } } if empty != nil { - ft.hash[""] = empty + ft.hash.Set("", empty) } }