From d8a99598f896a917d057d98be0c437f14c3145fc Mon Sep 17 00:00:00 2001 From: wdvxdr Date: Fri, 18 Mar 2022 19:32:38 +0800 Subject: [PATCH] proto: reduce map alloc --- proto/slice.go | 3 +++ proto/struct.go | 39 +++++++++++++++++++-------------------- proto/struct_test.go | 3 ++- proto/walker.go | 9 +++++---- 4 files changed, 29 insertions(+), 25 deletions(-) diff --git a/proto/slice.go b/proto/slice.go index e1f2eba..a3bc710 100644 --- a/proto/slice.go +++ b/proto/slice.go @@ -14,6 +14,9 @@ func sliceCodecOf(t reflect.Type, c *codec, w *walker) *codec { if loaded, ok := sliceMap.Load(c); ok { return loaded.(*codec) } + if w.codecs[t] != nil { + return w.codecs[t] + } s := new(codec) w.codecs[t] = s diff --git a/proto/struct.go b/proto/struct.go index 666d296..9b9ce5b 100644 --- a/proto/struct.go +++ b/proto/struct.go @@ -149,21 +149,16 @@ func (info *structInfo) decode(b []byte, p unsafe.Pointer) (int, error) { } type structTag struct { - name string - enum string - json string - version int + // version int wireType wireType fieldNumber fieldNumber - extensions map[string]string repeated bool zigzag bool } func parseStructTag(tag string) (structTag, error) { t := structTag{ - version: 2, - extensions: make(map[string]string), + // version: 2, } for i, f := range splitFields(tag) { @@ -206,19 +201,21 @@ func parseStructTag(tag string) (structTag, error) { } default: - name, value := splitNameValue(f) - switch name { - case "name": - t.name = value - case "enum": - t.enum = value - case "json": - t.json = value - case "proto3": - t.version = 3 - default: - t.extensions[name] = value - } + /* + name, value := splitNameValue(f) + switch name { + case "name": + t.name = value + case "enum": + t.enum = value + case "json": + t.json = value + case "proto3": + t.version = 3 + default: + t.extensions[name] = value + } + */ } } @@ -229,6 +226,7 @@ func splitFields(s string) []string { return strings.Split(s, ",") } +/* func splitNameValue(s string) (name, value string) { i := strings.IndexByte(s, '=') if i < 0 { @@ -237,3 +235,4 @@ func splitNameValue(s string) (name, value string) { return strings.TrimSpace(s[:i]), strings.TrimSpace(s[i+1:]) } } +*/ diff --git a/proto/struct_test.go b/proto/struct_test.go index d431a61..d28418a 100644 --- a/proto/struct_test.go +++ b/proto/struct_test.go @@ -1,7 +1,6 @@ package proto import ( - "reflect" "testing" "unsafe" ) @@ -10,6 +9,7 @@ func TestStructFieldSize(t *testing.T) { t.Log("sizeof(structField) =", unsafe.Sizeof(structField{})) } +/* func TestParseStructTag(t *testing.T) { tests := []struct { str string @@ -88,3 +88,4 @@ func TestParseStructTag(t *testing.T) { }) } } +*/ diff --git a/proto/walker.go b/proto/walker.go index 0d383df..1ffb6bc 100644 --- a/proto/walker.go +++ b/proto/walker.go @@ -67,7 +67,9 @@ func (w *walker) structCodec(t reflect.Type) *codec { if c, ok := codecCache.Load(pointer(t)); ok { return c.(*codec) } - + if c, ok := w.codecs[t]; ok { + return c + } c := new(codec) w.codecs[t] = c elem := t.Elem() @@ -194,9 +196,8 @@ func (w *walker) structInfo(t reflect.Type) *structInfo { case reflect.Slice: elem := f.Type.Elem() - if elem.Kind() == reflect.Uint8 { // []byte - field.codec = w.codec(f.Type, conf) + field.codec = &bytesCodec } else { conf.required = true field.codec = w.codec(elem, conf) @@ -237,7 +238,7 @@ func (w *walker) structInfo(t reflect.Type) *structInfo { copy(fields2, fields) info.fields = fields2 - info.fieldIndex = make(map[fieldNumber]*structField) + info.fieldIndex = make(map[fieldNumber]*structField, len(info.fields)) for _, f := range info.fields { info.fieldIndex[f.fieldNumber()] = f }