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,13 +3,12 @@ package common | ||
3 | import ( | 3 | import ( |
4 | "crypto/md5" | 4 | "crypto/md5" |
5 | "encoding/hex" | 5 | "encoding/hex" |
6 | - "google.golang.org/protobuf/reflect/protoreflect" | ||
7 | "math/rand" | 6 | "math/rand" |
8 | "strings" | 7 | "strings" |
9 | "time" | 8 | "time" |
10 | ) | 9 | ) |
11 | 10 | ||
12 | -func Md5V(str string) string { | 11 | +func Md5V(str string) string { |
13 | h := md5.New() | 12 | h := md5.New() |
14 | h.Write([]byte(str)) | 13 | h.Write([]byte(str)) |
15 | return hex.EncodeToString(h.Sum(nil)) | 14 | return hex.EncodeToString(h.Sum(nil)) |
@@ -20,6 +19,7 @@ func Timex() int64 { | @@ -20,6 +19,7 @@ func Timex() int64 { | ||
20 | } | 19 | } |
21 | 20 | ||
22 | var defaultLetters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789") | 21 | var defaultLetters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789") |
22 | + | ||
23 | func RandomString(n int, allowedChars ...[]rune) string { | 23 | func RandomString(n int, allowedChars ...[]rune) string { |
24 | var letters []rune | 24 | var letters []rune |
25 | 25 | ||
@@ -37,7 +37,6 @@ func RandomString(n int, allowedChars ...[]rune) string { | @@ -37,7 +37,6 @@ func RandomString(n int, allowedChars ...[]rune) string { | ||
37 | return string(b) | 37 | return string(b) |
38 | } | 38 | } |
39 | 39 | ||
40 | - | ||
41 | func RandomName(name [][]string) string { | 40 | func RandomName(name [][]string) string { |
42 | idx1 := rand.Intn(len(name[0])) | 41 | idx1 := rand.Intn(len(name[0])) |
43 | idx2 := rand.Intn(len(name[1])) | 42 | idx2 := rand.Intn(len(name[1])) |
@@ -56,19 +55,3 @@ func FirstCharToUpper(key string) string { | @@ -56,19 +55,3 @@ func FirstCharToUpper(key string) string { | ||
56 | str.WriteString(key[1:]) | 55 | str.WriteString(key[1:]) |
57 | return str.String() | 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 | \ No newline at end of file | 58 | \ No newline at end of file |
models/role.go
@@ -115,7 +115,7 @@ func (m *RoleModel) LoadHero() { | @@ -115,7 +115,7 @@ func (m *RoleModel) LoadHero() { | ||
115 | 115 | ||
116 | func (m *RoleModel) LoadTeams() { | 116 | func (m *RoleModel) LoadTeams() { |
117 | teams := make([]*pb.Team, 4) | 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 | if err != nil { | 119 | if err != nil { |
120 | logger.Error(err) | 120 | logger.Error(err) |
121 | return | 121 | return |
@@ -131,7 +131,7 @@ func (m *RoleModel) LoadAll() { | @@ -131,7 +131,7 @@ func (m *RoleModel) LoadAll() { | ||
131 | } | 131 | } |
132 | 132 | ||
133 | func (m *RoleModel) UpdateProperty(conn components.IConnection, key string, val interface{}, notify bool) { | 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 | func (m *RoleModel) UpdateProperties(conn components.IConnection, property map[string]interface{}, notify bool) { | 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,7 +139,7 @@ func (m *RoleModel) UpdateProperties(conn components.IConnection, property map[s | ||
139 | return | 139 | return |
140 | } | 140 | } |
141 | 141 | ||
142 | - role := &pb.Role{} | 142 | + role := &pb.Role{} |
143 | ids := m.ParseFields(role.ProtoReflect(), property) | 143 | ids := m.ParseFields(role.ProtoReflect(), property) |
144 | if len(ids) == 0 { | 144 | if len(ids) == 0 { |
145 | logger.Error("ParseFields err, len is 0") | 145 | logger.Error("ParseFields err, len is 0") |
@@ -147,14 +147,16 @@ func (m *RoleModel) UpdateProperties(conn components.IConnection, property map[s | @@ -147,14 +147,16 @@ func (m *RoleModel) UpdateProperties(conn components.IConnection, property map[s | ||
147 | } | 147 | } |
148 | 148 | ||
149 | update := &pb.UpdateRolePropertyRsp{ | 149 | update := &pb.UpdateRolePropertyRsp{ |
150 | - Id: ids, | 150 | + Id: ids, |
151 | Role: role, | 151 | Role: role, |
152 | } | 152 | } |
153 | if rsp, err := proto.Marshal(update); err != nil { | 153 | if rsp, err := proto.Marshal(update); err != nil { |
154 | logger.Error("id %s, err:", m.Role.Id, err) | 154 | logger.Error("id %s, err:", m.Role.Id, err) |
155 | return | 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,7 +31,7 @@ func TestNewRole(t *testing.T) { | ||
31 | //}) | 31 | //}) |
32 | role.SetProperty("Device", "1111") | 32 | role.SetProperty("Device", "1111") |
33 | //role.Save() | 33 | //role.Save() |
34 | - }else { | 34 | + } else { |
35 | //uid不存在,创建角色 | 35 | //uid不存在,创建角色 |
36 | role = NewRole("1") | 36 | role = NewRole("1") |
37 | role.Role.Uid = uid | 37 | role.Role.Uid = uid |
@@ -45,7 +45,7 @@ func TestNewRole(t *testing.T) { | @@ -45,7 +45,7 @@ func TestNewRole(t *testing.T) { | ||
45 | 45 | ||
46 | func TestRoleIndex(t *testing.T) { | 46 | func TestRoleIndex(t *testing.T) { |
47 | coll, keys := mongoproxy.FindIndex(pb.Role{}) | 47 | coll, keys := mongoproxy.FindIndex(pb.Role{}) |
48 | - for _, index := range keys { | 48 | + for _, index := range keys { |
49 | logger.Debug("coll: %s, key: %s", coll, index) | 49 | logger.Debug("coll: %s, key: %s", coll, index) |
50 | } | 50 | } |
51 | } | 51 | } |
@@ -74,16 +74,11 @@ func TestRoleModel_ProtoReflect(t *testing.T) { | @@ -74,16 +74,11 @@ func TestRoleModel_ProtoReflect(t *testing.T) { | ||
74 | return | 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 | \ No newline at end of file | 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,7 +2,6 @@ package models | ||
2 | 2 | ||
3 | import ( | 3 | import ( |
4 | "google.golang.org/protobuf/reflect/protoreflect" | 4 | "google.golang.org/protobuf/reflect/protoreflect" |
5 | - "pro2d/common" | ||
6 | "pro2d/common/components" | 5 | "pro2d/common/components" |
7 | "pro2d/common/db/mongoproxy" | 6 | "pro2d/common/db/mongoproxy" |
8 | "pro2d/common/logger" | 7 | "pro2d/common/logger" |
@@ -23,6 +22,7 @@ type SchemaMap map[string]components.ISchema | @@ -23,6 +22,7 @@ type SchemaMap map[string]components.ISchema | ||
23 | type Schema struct { | 22 | type Schema struct { |
24 | db components.IDB | 23 | db components.IDB |
25 | reflectValue *reflect.Value | 24 | reflectValue *reflect.Value |
25 | + reflectIndex map[string]int | ||
26 | 26 | ||
27 | cacheFields map[string]interface{} | 27 | cacheFields map[string]interface{} |
28 | 28 | ||
@@ -39,6 +39,11 @@ func NewSchema(key string, schema interface{}) *Schema { | @@ -39,6 +39,11 @@ func NewSchema(key string, schema interface{}) *Schema { | ||
39 | reflectValue: &s, | 39 | reflectValue: &s, |
40 | cacheFields: make(map[string]interface{}), | 40 | cacheFields: make(map[string]interface{}), |
41 | schema: schema, | 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 | sch.db = mongoproxy.NewMongoColl(sch.GetSchemaName(), sch) | 49 | sch.db = mongoproxy.NewMongoColl(sch.GetSchemaName(), sch) |
@@ -111,22 +116,30 @@ func (s *Schema) Update() { | @@ -111,22 +116,30 @@ func (s *Schema) Update() { | ||
111 | } | 116 | } |
112 | 117 | ||
113 | func (s *Schema) SetProperty(key string, val interface{}) { | 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 | s.cacheFields[strings.ToLower(key)] = val | 124 | s.cacheFields[strings.ToLower(key)] = val |
116 | } | 125 | } |
117 | 126 | ||
118 | func (s *Schema) SetProperties(properties map[string]interface{}) { | 127 | func (s *Schema) SetProperties(properties map[string]interface{}) { |
119 | for key, val := range properties { | 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 | s.cacheFields[strings.ToLower(key)] = val | 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 | ids := make([]int32, 0, len(properties)) | 140 | ids := make([]int32, 0, len(properties)) |
127 | for k, v := range properties { | 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 | if field == nil { | 143 | if field == nil { |
131 | continue | 144 | continue |
132 | } | 145 | } |