Skip to content

Commit

Permalink
优化mapper组件,1万条复杂对象记录降低100ms。
Browse files Browse the repository at this point in the history
  • Loading branch information
steden committed Aug 20, 2024
1 parent 5d679e0 commit 0195bf8
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 21 deletions.
7 changes: 6 additions & 1 deletion fastReflect/typeMeta.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ type TypeMeta struct {
//Name string // 字段名称
ReflectType reflect.Type // 字段类型
ReflectTypeString string // 类型
ReflectTypeBytes []byte // 类型
TypeIdentity string // 类型标识
Type FieldType // 集合类型
IsAddr bool // 原类型是否带指针
StructField []reflect.StructField // 结构体的字段
Expand All @@ -31,9 +33,9 @@ type TypeMeta struct {
IsDateTime bool // 是否dateTime.DateTime
IsSliceOrArray bool // 是否切片或数组类型
IsStruct bool // 是否结构体
IsMap bool // 是否字典
HashCode uint32 // 每个类型的HashCode都是唯一的
Size uintptr // 内存占用大小
TypeIdentity string // 类型标识
}

func typeOf(reflectType reflect.Type, inf *EmptyInterface) *TypeMeta {
Expand Down Expand Up @@ -86,6 +88,7 @@ func (receiver *TypeMeta) parseType() {

//receiver.Name = receiver.ReflectType.Name()
receiver.ReflectTypeString = receiver.ReflectType.String()
receiver.ReflectTypeBytes = []byte(receiver.ReflectTypeString)

switch receiver.Kind {
case reflect.Slice:
Expand All @@ -99,6 +102,7 @@ func (receiver *TypeMeta) parseType() {
receiver.Type = Array
receiver.setItemHashCode(receiver.ReflectType.Elem())
case reflect.Map:
receiver.IsMap = true
receiver.Type = Map
receiver.MapType = receiver.ReflectType
// key type
Expand Down Expand Up @@ -143,6 +147,7 @@ func (receiver *TypeMeta) parseType() {

// Dictionary类型
if isTrue := types.IsDictionaryByType(receiver.ReflectType); isTrue {
receiver.IsMap = true
receiver.Type = Dic
// 得到底层的map类型
receiver.MapType = types.GetDictionaryMapType(receiver.ReflectType)
Expand Down
9 changes: 3 additions & 6 deletions fastReflect/valueMeta.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,13 @@ var anyValueMap []any
var anyNil = TypeMeta{
ReflectType: reflect.TypeOf(anyValueMap).Elem(),
ReflectTypeString: "interface {}",
ReflectTypeBytes: []byte("interface {}"),
Type: Interface,
Kind: reflect.Interface,
HashCode: 252279353,
Size: 16,
}

var cacheTyp sync.Map

func init() {
Expand All @@ -55,16 +57,11 @@ func init() {
func PointerOfValue(val reflect.Value) PointerMeta {
inf := (*EmptyInterface)(unsafe.Pointer(&val))
valueMeta := PointerMeta{PointerValue: inf.Value, HashCode: inf.Typ.hash}
//if inf.Typ != nil {
// valueMeta.HashCode = inf.Typ.hash
//} else {
// valueMeta.HashCode = 252279353
//}

if typeMeta, exists := cacheTyp.Load(valueMeta.HashCode); exists {
valueMeta.TypeMeta = typeMeta.(*TypeMeta)
return valueMeta
}

valueMeta.TypeMeta = typeOf(val.Type(), inf)
cacheTyp.Store(valueMeta.HashCode, valueMeta.TypeMeta)
return valueMeta
Expand Down
2 changes: 1 addition & 1 deletion types/isType.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,6 @@ func IsNil(val reflect.Value) bool {
case reflect.Chan, reflect.Func, reflect.Interface, reflect.Map, reflect.Pointer, reflect.Slice:
return val.IsNil()
default:
return false //return !val.IsValid()
return false
}
}
28 changes: 15 additions & 13 deletions types/listType.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,21 +87,13 @@ func GetListItemType(lstType reflect.Type) reflect.Type {

// GetListToArray 在集合中获取数据
func GetListToArray(lstValue reflect.Value) []any {
key := lstValue.String() + ".ToArray"

key := lstValue.String() + ".ToArrayAny"
if _, isExists := getCache(key); !isExists {
method, _ := lstValue.Type().MethodByName("ToArray")
method, _ := lstValue.Type().MethodByName("ToArrayAny")
setCache(key, []int{method.Index})
}

arrValue := lstValue.Method(getCacheVal(key)[0]).Call(nil)[0]

var items []any
for i := 0; i < arrValue.Len(); i++ {
item := arrValue.Index(i).Interface()
items = append(items, item)
}
return items
return arrValue.Interface().([]any)
}

// GetListToArrayValue 在集合中获取数据
Expand All @@ -112,8 +104,18 @@ func GetListToArrayValue(lstValue reflect.Value) reflect.Value {
setCache(key, []int{method.Index})
}

arrValue := lstValue.Method(getCacheVal(key)[0]).Call(nil)[0]
return arrValue
return lstValue.Method(getCacheVal(key)[0]).Call(nil)[0]
}

// ExecuteMapperInit 在集合中获取数据
func ExecuteMapperInit(targetVal reflect.Value) {
key := targetVal.String() + ".MapperInit"
if _, isExists := getCache(key); !isExists {
method, _ := targetVal.Type().MethodByName("MapperInit")
setCache(key, []int{method.Index})
}

targetVal.Method(getCacheVal(key)[0]).Call([]reflect.Value{})
}

func setCache(key string, val []int) {
Expand Down

0 comments on commit 0195bf8

Please sign in to comment.