Commit d771e9e34b0850450200486428e26441d253dc3d
1 parent
b499527e
fix: 优化FieldByName, 使用Field(i int) 更快。
用空间换时间
Showing
4 changed files
with
39 additions
and
46 deletions
Show diff stats
common/utils.go
| ... | ... | @@ -3,13 +3,12 @@ package common |
| 3 | 3 | import ( |
| 4 | 4 | "crypto/md5" |
| 5 | 5 | "encoding/hex" |
| 6 | - "google.golang.org/protobuf/reflect/protoreflect" | |
| 7 | 6 | "math/rand" |
| 8 | 7 | "strings" |
| 9 | 8 | "time" |
| 10 | 9 | ) |
| 11 | 10 | |
| 12 | -func Md5V(str string) string { | |
| 11 | +func Md5V(str string) string { | |
| 13 | 12 | h := md5.New() |
| 14 | 13 | h.Write([]byte(str)) |
| 15 | 14 | return hex.EncodeToString(h.Sum(nil)) |
| ... | ... | @@ -20,6 +19,7 @@ func Timex() int64 { |
| 20 | 19 | } |
| 21 | 20 | |
| 22 | 21 | var defaultLetters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789") |
| 22 | + | |
| 23 | 23 | func RandomString(n int, allowedChars ...[]rune) string { |
| 24 | 24 | var letters []rune |
| 25 | 25 | |
| ... | ... | @@ -37,7 +37,6 @@ func RandomString(n int, allowedChars ...[]rune) string { |
| 37 | 37 | return string(b) |
| 38 | 38 | } |
| 39 | 39 | |
| 40 | - | |
| 41 | 40 | func RandomName(name [][]string) string { |
| 42 | 41 | idx1 := rand.Intn(len(name[0])) |
| 43 | 42 | idx2 := rand.Intn(len(name[1])) |
| ... | ... | @@ -56,19 +55,3 @@ func FirstCharToUpper(key string) string { |
| 56 | 55 | str.WriteString(key[1:]) |
| 57 | 56 | return str.String() |
| 58 | 57 | } |
| 59 | - | |
| 60 | - | |
| 61 | -func ParseFields(message protoreflect.Message ,properties map[string]interface{}) []int32 { | |
| 62 | - ids := make([]int32,0) | |
| 63 | - for k, v := range properties { | |
| 64 | - name := protoreflect.Name(strings.ToLower(k)) | |
| 65 | - field := message.Descriptor().Fields().ByName(name) | |
| 66 | - if field == nil { | |
| 67 | - continue | |
| 68 | - } | |
| 69 | - | |
| 70 | - ids = append(ids, int32(field.Index())) | |
| 71 | - message.Set(field, protoreflect.ValueOf(v)) | |
| 72 | - } | |
| 73 | - return ids | |
| 74 | -} | |
| 75 | 58 | \ No newline at end of file | ... | ... |
models/role.go
| ... | ... | @@ -115,7 +115,7 @@ func (m *RoleModel) LoadHero() { |
| 115 | 115 | |
| 116 | 116 | func (m *RoleModel) LoadTeams() { |
| 117 | 117 | teams := make([]*pb.Team, 4) |
| 118 | - err := mongoproxy.FindMany("hero", "role_id", m.Role.Id, &teams) | |
| 118 | + err := mongoproxy.FindMany("team", "role_id", m.Role.Id, &teams) | |
| 119 | 119 | if err != nil { |
| 120 | 120 | logger.Error(err) |
| 121 | 121 | return |
| ... | ... | @@ -131,7 +131,7 @@ func (m *RoleModel) LoadAll() { |
| 131 | 131 | } |
| 132 | 132 | |
| 133 | 133 | func (m *RoleModel) UpdateProperty(conn components.IConnection, key string, val interface{}, notify bool) { |
| 134 | - m.UpdateProperties(conn, map[string]interface{}{key:val}, notify) | |
| 134 | + m.UpdateProperties(conn, map[string]interface{}{key: val}, notify) | |
| 135 | 135 | } |
| 136 | 136 | |
| 137 | 137 | func (m *RoleModel) UpdateProperties(conn components.IConnection, property map[string]interface{}, notify bool) { |
| ... | ... | @@ -139,7 +139,7 @@ func (m *RoleModel) UpdateProperties(conn components.IConnection, property map[s |
| 139 | 139 | return |
| 140 | 140 | } |
| 141 | 141 | |
| 142 | - role := &pb.Role{} | |
| 142 | + role := &pb.Role{} | |
| 143 | 143 | ids := m.ParseFields(role.ProtoReflect(), property) |
| 144 | 144 | if len(ids) == 0 { |
| 145 | 145 | logger.Error("ParseFields err, len is 0") |
| ... | ... | @@ -147,14 +147,16 @@ func (m *RoleModel) UpdateProperties(conn components.IConnection, property map[s |
| 147 | 147 | } |
| 148 | 148 | |
| 149 | 149 | update := &pb.UpdateRolePropertyRsp{ |
| 150 | - Id: ids, | |
| 150 | + Id: ids, | |
| 151 | 151 | Role: role, |
| 152 | 152 | } |
| 153 | 153 | if rsp, err := proto.Marshal(update); err != nil { |
| 154 | 154 | logger.Error("id %s, err:", m.Role.Id, err) |
| 155 | 155 | return |
| 156 | - }else { | |
| 157 | - conn.Send(0, uint32(pb.ProtoCode_UpdateRolePropertyRsp), rsp) | |
| 156 | + } else { | |
| 157 | + if conn != nil { | |
| 158 | + conn.Send(0, uint32(pb.ProtoCode_UpdateRolePropertyRsp), rsp) | |
| 159 | + } | |
| 158 | 160 | } |
| 159 | 161 | } |
| 160 | 162 | ... | ... |
models/role_test.go
| ... | ... | @@ -31,7 +31,7 @@ func TestNewRole(t *testing.T) { |
| 31 | 31 | //}) |
| 32 | 32 | role.SetProperty("Device", "1111") |
| 33 | 33 | //role.Save() |
| 34 | - }else { | |
| 34 | + } else { | |
| 35 | 35 | //uid不存在,创建角色 |
| 36 | 36 | role = NewRole("1") |
| 37 | 37 | role.Role.Uid = uid |
| ... | ... | @@ -45,7 +45,7 @@ func TestNewRole(t *testing.T) { |
| 45 | 45 | |
| 46 | 46 | func TestRoleIndex(t *testing.T) { |
| 47 | 47 | coll, keys := mongoproxy.FindIndex(pb.Role{}) |
| 48 | - for _, index := range keys { | |
| 48 | + for _, index := range keys { | |
| 49 | 49 | logger.Debug("coll: %s, key: %s", coll, index) |
| 50 | 50 | } |
| 51 | 51 | } |
| ... | ... | @@ -74,16 +74,11 @@ func TestRoleModel_ProtoReflect(t *testing.T) { |
| 74 | 74 | return |
| 75 | 75 | } |
| 76 | 76 | |
| 77 | - role := &pb.Role{} | |
| 78 | - sch := NewSchema("", role) | |
| 79 | - mp := map[string]interface{}{ | |
| 80 | - "Id": "1", | |
| 81 | - "Device": "12312312312", | |
| 82 | - } | |
| 83 | - ids := sch.ParseFields(role.ProtoReflect(), mp) | |
| 84 | - if len(ids) == 0 { | |
| 85 | - return | |
| 86 | - } | |
| 87 | - | |
| 88 | - fmt.Println(role) | |
| 89 | -} | |
| 90 | 77 | \ No newline at end of file |
| 78 | + sch := NewRole("") | |
| 79 | + //mp := map[string]interface{}{ | |
| 80 | + // "Id": "1", | |
| 81 | + // "Device": "12312312312", | |
| 82 | + //} | |
| 83 | + sch.UpdateProperty(nil, "Device", "123123123", false) | |
| 84 | + fmt.Println(sch.Role) | |
| 85 | +} | ... | ... |
models/schema.go
| ... | ... | @@ -2,7 +2,6 @@ package models |
| 2 | 2 | |
| 3 | 3 | import ( |
| 4 | 4 | "google.golang.org/protobuf/reflect/protoreflect" |
| 5 | - "pro2d/common" | |
| 6 | 5 | "pro2d/common/components" |
| 7 | 6 | "pro2d/common/db/mongoproxy" |
| 8 | 7 | "pro2d/common/logger" |
| ... | ... | @@ -23,6 +22,7 @@ type SchemaMap map[string]components.ISchema |
| 23 | 22 | type Schema struct { |
| 24 | 23 | db components.IDB |
| 25 | 24 | reflectValue *reflect.Value |
| 25 | + reflectIndex map[string]int | |
| 26 | 26 | |
| 27 | 27 | cacheFields map[string]interface{} |
| 28 | 28 | |
| ... | ... | @@ -39,6 +39,11 @@ func NewSchema(key string, schema interface{}) *Schema { |
| 39 | 39 | reflectValue: &s, |
| 40 | 40 | cacheFields: make(map[string]interface{}), |
| 41 | 41 | schema: schema, |
| 42 | + reflectIndex: make(map[string]int), | |
| 43 | + } | |
| 44 | + | |
| 45 | + for i := 0; i < s.Type().NumField(); i++ { | |
| 46 | + sch.reflectIndex[strings.ToLower(s.Type().Field(i).Name)] = i | |
| 42 | 47 | } |
| 43 | 48 | |
| 44 | 49 | sch.db = mongoproxy.NewMongoColl(sch.GetSchemaName(), sch) |
| ... | ... | @@ -111,22 +116,30 @@ func (s *Schema) Update() { |
| 111 | 116 | } |
| 112 | 117 | |
| 113 | 118 | func (s *Schema) SetProperty(key string, val interface{}) { |
| 114 | - s.reflectValue.FieldByName(common.FirstCharToUpper(key)).Set(reflect.ValueOf(val)) | |
| 119 | + idx, ok := s.reflectIndex[strings.ToLower(key)] | |
| 120 | + if !ok { | |
| 121 | + return | |
| 122 | + } | |
| 123 | + s.reflectValue.Field(idx).Set(reflect.ValueOf(val)) | |
| 115 | 124 | s.cacheFields[strings.ToLower(key)] = val |
| 116 | 125 | } |
| 117 | 126 | |
| 118 | 127 | func (s *Schema) SetProperties(properties map[string]interface{}) { |
| 119 | 128 | for key, val := range properties { |
| 120 | - s.reflectValue.FieldByName(common.FirstCharToUpper(key)).Set(reflect.ValueOf(val)) | |
| 129 | + idx, ok := s.reflectIndex[strings.ToLower(key)] | |
| 130 | + if !ok { | |
| 131 | + continue | |
| 132 | + } | |
| 133 | + | |
| 134 | + s.reflectValue.Field(idx).Set(reflect.ValueOf(val)) | |
| 121 | 135 | s.cacheFields[strings.ToLower(key)] = val |
| 122 | 136 | } |
| 123 | 137 | } |
| 124 | 138 | |
| 125 | -func (s *Schema) ParseFields(message protoreflect.Message ,properties map[string]interface{}) []int32 { | |
| 139 | +func (s *Schema) ParseFields(message protoreflect.Message, properties map[string]interface{}) []int32 { | |
| 126 | 140 | ids := make([]int32, 0, len(properties)) |
| 127 | 141 | for k, v := range properties { |
| 128 | - name := protoreflect.Name(strings.ToLower(k)) | |
| 129 | - field := message.Descriptor().Fields().ByName(name) | |
| 142 | + field := message.Descriptor().Fields().ByName(protoreflect.Name(strings.ToLower(k))) | |
| 130 | 143 | if field == nil { |
| 131 | 144 | continue |
| 132 | 145 | } | ... | ... |