1
0
mirror of https://github.com/Mrs4s/MiraiGo.git synced 2025-05-05 03:23:50 +08:00

speed up jce

pkg: github.com/Mrs4s/MiraiGo/binary/jce
BenchmarkJceWriter_WriteJceStructRaw
BenchmarkJceWriter_WriteJceStructRaw-8    	  200443	      5966 ns/op
BenchmarkJceWriter_WriteJceStructRaw2
BenchmarkJceWriter_WriteJceStructRaw2-8   	  481285	      2659 ns/op
This commit is contained in:
wdvxdr 2021-01-30 18:26:48 +08:00
parent e9537a09a2
commit 59e689bd46

View File

@ -3,8 +3,10 @@ package jce
import ( import (
"bytes" "bytes"
goBinary "encoding/binary" goBinary "encoding/binary"
"github.com/modern-go/reflect2"
"reflect" "reflect"
"strconv" "strconv"
"sync"
) )
type JceWriter struct { type JceWriter struct {
@ -200,24 +202,44 @@ func (w *JceWriter) WriteObject(i interface{}, tag int) {
} }
} }
type decoder []struct {
fieldID int
id int
}
var decoderCache = sync.Map{}
// WriteJceStructRaw 写入 Jce 结构体
func (w *JceWriter) WriteJceStructRaw(s IJceStruct) { func (w *JceWriter) WriteJceStructRaw(s IJceStruct) {
var ( var (
t = reflect.TypeOf(s).Elem() v = reflect.ValueOf(s).Elem()
v = reflect.ValueOf(s).Elem() ty2 = reflect2.TypeOf(s)
jceDec decoder
) )
for i := 0; i < t.NumField(); i++ { dec, ok := decoderCache.Load(ty2)
strId := t.Field(i).Tag.Get("jceId") if ok { // 从缓存中加载
if strId == "" { jceDec = dec.(decoder)
continue } else { // 初次反射
} jceDec = decoder{}
id, err := strconv.Atoi(strId) t := reflect.TypeOf(s).Elem()
if err != nil { for i := 0; i < t.NumField(); i++ {
continue strId := t.Field(i).Tag.Get("jceId")
} if strId == "" {
obj := v.Field(i).Interface() continue
if obj != nil { }
w.WriteObject(v.Field(i).Interface(), id) id, err := strconv.Atoi(strId)
if err != nil {
continue
}
jceDec = append(jceDec, struct {
fieldID int
id int
}{fieldID: i, id: id})
} }
decoderCache.Store(ty2, jceDec) // 存入缓存
}
for _, dec := range jceDec {
w.WriteObject(v.Field(dec.fieldID).Interface(), dec.id)
} }
} }