Skip to content

Commit

Permalink
feat: performance optimization - user
Browse files Browse the repository at this point in the history
  • Loading branch information
wubenqi committed Sep 11, 2022
1 parent 852059d commit 391eb4c
Show file tree
Hide file tree
Showing 6 changed files with 469 additions and 336 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ func (c *ContactsCore) ContactsGetContacts(in *mtproto.TLContactsGetContacts) (*

users, _ := c.svcCtx.UserClient.UserGetMutableUsers(c.ctx, &userpb.TLUserGetMutableUsers{
Id: append([]int64{c.MD.UserId}, idList...),
To: []int64{c.MD.UserId},
})
contacts = mtproto.MakeTLContactsContacts(&mtproto.Contacts_Contacts{
Contacts: contactList.ToContacts(),
Expand Down
1 change: 1 addition & 0 deletions app/service/biz/user/client/user_client_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ func (m *UserClientHelper) GetUserById(ctx context.Context, selfId, id int64) (*
func (m *UserClientHelper) GetUserListByIdList(ctx context.Context, selfId int64, id ...int64) []*mtproto.User {
users, err := m.cli.UserGetMutableUsers(ctx, &user.TLUserGetMutableUsers{
Id: append(id, selfId),
To: []int64{selfId},
})
if err != nil {
return []*mtproto.User{}
Expand Down
32 changes: 24 additions & 8 deletions app/service/biz/user/internal/core/user.getMutableUsers_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
package core

import (
"github.com/teamgram/marmota/pkg/container2"
"github.com/zeromicro/go-zero/core/mr"

"github.com/teamgram/teamgram-server/app/service/biz/user/user"
Expand All @@ -18,32 +19,47 @@ import (
// UserGetMutableUsers
// user.getMutableUsers id:Vector<int> = Vector<ImmutableUser>;
func (c *UserCore) UserGetMutableUsers(in *user.TLUserGetMutableUsers) (*user.Vector_ImmutableUser, error) {
id := make([]int64, 0, len(in.Id)+len(in.To))
for _, v := range in.Id {
if ok, _ := container2.Contains(v, id); !ok {
id = append(id, v)
}
}
for _, v := range in.To {
if ok, _ := container2.Contains(v, id); !ok {
id = append(id, v)
}
}

vUser := &user.Vector_ImmutableUser{
Datas: make([]*user.ImmutableUser, 0, len(in.Id)),
Datas: make([]*user.ImmutableUser, 0, len(id)),
}

if len(in.Id) == 0 {
if len(id) == 0 {
return vUser, nil
} else if len(in.Id) == 1 {
immutableUser, _ := c.svcCtx.Dao.GetImmutableUser(c.ctx, in.Id[0], false)
} else if len(id) == 1 {
immutableUser, _ := c.svcCtx.Dao.GetImmutableUser(c.ctx, id[0], false)
if immutableUser != nil {
vUser.Datas = append(vUser.Datas, immutableUser)
}

return vUser, nil
}

mutableUsers := make([]*user.ImmutableUser, len(in.Id))
mutableUsers := make([]*user.ImmutableUser, len(id))
mr.ForEach(
func(source chan<- interface{}) {
for idx := 0; idx < len(in.Id); idx++ {
for idx := 0; idx < len(id); idx++ {
source <- idx
}
},
func(item interface{}) {
idx := item.(int)
immutableUser, _ := c.svcCtx.Dao.GetImmutableUser(c.ctx, in.Id[idx], true, in.Id...)
mutableUsers[idx] = immutableUser
if ok, _ := container2.Contains(id[idx], in.To); ok {
mutableUsers[idx], _ = c.svcCtx.Dao.GetImmutableUser(c.ctx, id[idx], true, in.Id...)
} else {
mutableUsers[idx], _ = c.svcCtx.Dao.GetImmutableUser(c.ctx, id[idx], true, in.Id...)
}
})

for _, v := range mutableUsers {
Expand Down
4 changes: 2 additions & 2 deletions app/service/biz/user/user/class_name_registers.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ var clazzNameRegisters2 = map[string]map[int]int32{

},
Predicate_user_getMutableUsers: {
0: 187684863, // 0xb2fd7ff
0: -1657068585, // 0x9d3b23d7

},
Predicate_user_getImmutableUserByPhone: {
Expand Down Expand Up @@ -374,7 +374,7 @@ var clazzIdNameRegisters2 = map[int32]string{
-46114259: Predicate_user_updateLastSeen, // 0xfd405a2d
-1860581154: Predicate_user_getLastSeen, // 0x9119c8de
-47047585: Predicate_user_getImmutableUser, // 0xfd321c5f
187684863: Predicate_user_getMutableUsers, // 0xb2fd7ff
-1657068585: Predicate_user_getMutableUsers, // 0x9d3b23d7
-373067804: Predicate_user_getImmutableUserByPhone, // 0xe9c36fe4
-12709005: Predicate_user_getImmutableUserByToken, // 0xff3e1373
-766178484: Predicate_user_setAccountDaysTTL, // 0xd2550b4c
Expand Down
14 changes: 9 additions & 5 deletions app/service/biz/user/user/codec_schema.tl.pb.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,9 @@ var clazzIdRegisters2 = map[int32]func() mtproto.TLObject{
Constructor: -47047585,
}
},
187684863: func() mtproto.TLObject { // 0xb2fd7ff
-1657068585: func() mtproto.TLObject { // 0x9d3b23d7
return &TLUserGetMutableUsers{
Constructor: 187684863,
Constructor: -1657068585,
}
},
-373067804: func() mtproto.TLObject { // 0xe9c36fe4
Expand Down Expand Up @@ -2239,13 +2239,15 @@ func (m *TLUserGetMutableUsers) Encode(layer int32) []byte {
// x.Int(int32(CRC32_user_getMutableUsers))

switch uint32(m.Constructor) {
case 0xb2fd7ff:
x.UInt(0xb2fd7ff)
case 0x9d3b23d7:
x.UInt(0x9d3b23d7)

// no flags

x.VectorLong(m.GetId())

x.VectorLong(m.GetTo())

default:
// log.Errorf("")
}
Expand All @@ -2259,12 +2261,14 @@ func (m *TLUserGetMutableUsers) CalcByteSize(layer int32) int {

func (m *TLUserGetMutableUsers) Decode(dBuf *mtproto.DecodeBuf) error {
switch uint32(m.Constructor) {
case 0xb2fd7ff:
case 0x9d3b23d7:

// not has flags

m.Id = dBuf.VectorLong()

m.To = dBuf.VectorLong()

return dBuf.GetError()

default:
Expand Down
Loading

0 comments on commit 391eb4c

Please sign in to comment.