Skip to content

Commit

Permalink
proto: reduce map alloc
Browse files Browse the repository at this point in the history
  • Loading branch information
wdvxdr1123 committed Mar 18, 2022
1 parent 643565f commit d8a9959
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 25 deletions.
3 changes: 3 additions & 0 deletions proto/slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
39 changes: 19 additions & 20 deletions proto/struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
}
*/
}
}

Expand All @@ -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 {
Expand All @@ -237,3 +235,4 @@ func splitNameValue(s string) (name, value string) {
return strings.TrimSpace(s[:i]), strings.TrimSpace(s[i+1:])
}
}
*/
3 changes: 2 additions & 1 deletion proto/struct_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package proto

import (
"reflect"
"testing"
"unsafe"
)
Expand All @@ -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
Expand Down Expand Up @@ -88,3 +88,4 @@ func TestParseStructTag(t *testing.T) {
})
}
}
*/
9 changes: 5 additions & 4 deletions proto/walker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit d8a9959

Please sign in to comment.