Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rft: change value reference to ptr to improve performance #244

Merged
merged 1 commit into from
Nov 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ type Decoder struct {
// record type refs, both list and map need it
// todo: map
typeRefs *TypeRefs
classInfoList []classInfo
classInfoList []*classInfo
isSkip bool
}

Expand Down
2 changes: 1 addition & 1 deletion encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import (

// Encoder struct
type Encoder struct {
classInfoList []classInfo
classInfoList []*classInfo
buffer []byte
refMap map[unsafe.Pointer]_refElem
}
Expand Down
2 changes: 1 addition & 1 deletion java_collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func (JavaCollectionSerializer) EncObject(e *Encoder, vv POJO) error {
return nil
}

func (JavaCollectionSerializer) DecObject(d *Decoder, typ reflect.Type, cls classInfo) (interface{}, error) {
func (JavaCollectionSerializer) DecObject(d *Decoder, typ reflect.Type, cls *classInfo) (interface{}, error) {
//for the java impl of hessian encode collections as list, which will not be decoded as object in go impl, this method should not be called
return nil, perrors.New("unexpected collection decode call")
}
Expand Down
4 changes: 2 additions & 2 deletions java_sql_time.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (JavaSqlTimeSerializer) EncObject(e *Encoder, vv POJO) error {
i int
idx int
err error
clsDef classInfo
clsDef *classInfo
className string
ptrV reflect.Value
)
Expand Down Expand Up @@ -116,7 +116,7 @@ func (JavaSqlTimeSerializer) EncObject(e *Encoder, vv POJO) error {
}

// nolint
func (JavaSqlTimeSerializer) DecObject(d *Decoder, typ reflect.Type, cls classInfo) (interface{}, error) {
func (JavaSqlTimeSerializer) DecObject(d *Decoder, typ reflect.Type, cls *classInfo) (interface{}, error) {

if typ.Kind() != reflect.Struct {
return nil, perrors.Errorf("wrong type expect Struct but get:%s", typ.String())
Expand Down
6 changes: 3 additions & 3 deletions java_unknown_exception.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ import (

var exceptionCheckMutex sync.Mutex

func checkAndGetException(cls classInfo) (structInfo, bool) {
func checkAndGetException(cls *classInfo) (*structInfo, bool) {

if len(cls.fieldNameList) < 4 {
return structInfo{}, false
return nil, false
}
var (
throwable structInfo
throwable *structInfo
ok bool
)
var count = 0
Expand Down
6 changes: 3 additions & 3 deletions java_unknown_exception_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
)

func TestCheckAndGetException(t *testing.T) {
clazzInfo1 := classInfo{
clazzInfo1 := &classInfo{
javaName: "com.test.UserDefinedException",
fieldNameList: []string{"detailMessage", "code", "suppressedExceptions", "stackTrace", "cause"},
}
Expand All @@ -35,11 +35,11 @@ func TestCheckAndGetException(t *testing.T) {
assert.Equal(t, s.javaName, "com.test.UserDefinedException")
assert.Equal(t, s.goName, "hessian.UnknownException")

clazzInfo2 := classInfo{
clazzInfo2 := &classInfo{
javaName: "com.test.UserDefinedException",
fieldNameList: []string{"detailMessage", "code", "suppressedExceptions", "cause"},
}
s, b = checkAndGetException(clazzInfo2)
assert.False(t, b)
assert.Equal(t, s, structInfo{})
assert.Nil(t, s)
}
5 changes: 3 additions & 2 deletions list.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,11 @@ func getListType(javalistname string) reflect.Type {

if sliceTy == nil {
tpStructInfo, _ := getStructInfo(javaname)
tp := tpStructInfo.typ
if tp == nil {
if tpStructInfo == nil || tpStructInfo.typ == nil {
return nil
}

tp := tpStructInfo.typ
if tp.Kind() != reflect.Ptr {
tp = reflect.New(tp).Type()
}
Expand Down
22 changes: 11 additions & 11 deletions object.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func (e *Encoder) encObject(v POJO) error {
idx int
num int
err error
clsDef classInfo
clsDef *classInfo
)

vv := reflect.ValueOf(v)
Expand Down Expand Up @@ -292,7 +292,7 @@ func (d *Decoder) decClassDef() (interface{}, error) {
fieldList[i] = fieldName
}

return classInfo{javaName: clsName, fieldNameList: fieldList}, nil
return &classInfo{javaName: clsName, fieldNameList: fieldList}, nil
}

type fieldInfo struct {
Expand Down Expand Up @@ -362,7 +362,7 @@ func findField(name string, typ reflect.Type) ([]int, *reflect.StructField, erro
return []int{}, nil, perrors.Errorf("failed to find field %s", name)
}

func (d *Decoder) decInstance(typ reflect.Type, cls classInfo) (interface{}, error) {
func (d *Decoder) decInstance(typ reflect.Type, cls *classInfo) (interface{}, error) {
if typ.Kind() != reflect.Struct {
return nil, perrors.Errorf("wrong type expect Struct but get:%s", typ.String())
}
Expand Down Expand Up @@ -537,15 +537,15 @@ func (d *Decoder) decInstance(typ reflect.Type, cls classInfo) (interface{}, err
return vRef.Interface(), nil
}

func (d *Decoder) appendClsDef(cd classInfo) {
func (d *Decoder) appendClsDef(cd *classInfo) {
d.classInfoList = append(d.classInfoList, cd)
}

func (d *Decoder) getStructDefByIndex(idx int) (reflect.Type, classInfo, error) {
func (d *Decoder) getStructDefByIndex(idx int) (reflect.Type, *classInfo, error) {
var (
ok bool
cls classInfo
s structInfo
cls *classInfo
s *structInfo
err error
)

Expand Down Expand Up @@ -573,7 +573,7 @@ func (d *Decoder) decEnum(javaName string, flag int32) (JavaEnum, error) {
err error
enumName string
ok bool
info structInfo
info *structInfo
enumValue JavaEnum
)
enumName, err = d.decString(TAG_READ) // java enum class member is "name"
Expand All @@ -591,7 +591,7 @@ func (d *Decoder) decEnum(javaName string, flag int32) (JavaEnum, error) {
}

// skip this object
func (d *Decoder) skip(cls classInfo) error {
func (d *Decoder) skip(cls *classInfo) error {
len := len(cls.fieldNameList)
if len < 1 {
return nil
Expand All @@ -613,7 +613,7 @@ func (d *Decoder) decObject(flag int32) (interface{}, error) {
idx int32
err error
typ reflect.Type
cls classInfo
cls *classInfo
)

if flag != TAG_READ {
Expand All @@ -632,7 +632,7 @@ func (d *Decoder) decObject(flag int32) (interface{}, error) {
if err != nil {
return nil, perrors.Wrap(err, "decObject->decClassDef byte double")
}
cls, _ = clsDef.(classInfo)
cls, _ = clsDef.(*classInfo)
//add to slice
d.appendClsDef(cls)

Expand Down
4 changes: 2 additions & 2 deletions object_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -564,9 +564,9 @@ func doTestBasePointer(t *testing.T, base *BasePointer, expected *BasePointer) {

func TestSkip(t *testing.T) {
// clear pojo
pojoRegistry = POJORegistry{
pojoRegistry = &POJORegistry{
j2g: make(map[string]string),
registry: make(map[string]structInfo),
registry: make(map[string]*structInfo),
}
testDecodeFrameworkWithSkip(t, "replyObject_0", nil)
testDecodeFrameworkWithSkip(t, "replyObject_1", nil)
Expand Down
43 changes: 26 additions & 17 deletions pojo.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,15 @@ type structInfo struct {
// POJORegistry pojo registry struct
type POJORegistry struct {
sync.RWMutex
classInfoList []classInfo // {class name, field name list...} list
j2g map[string]string // java class name --> go struct name
registry map[string]structInfo // go class name --> go struct info
classInfoList []*classInfo // {class name, field name list...} list
j2g map[string]string // java class name --> go struct name
registry map[string]*structInfo // go class name --> go struct info
}

var (
pojoRegistry = POJORegistry{
pojoRegistry = &POJORegistry{
j2g: make(map[string]string),
registry: make(map[string]structInfo),
registry: make(map[string]*structInfo),
}
pojoType = reflect.TypeOf((*POJO)(nil)).Elem()
javaEnumType = reflect.TypeOf((*POJOEnum)(nil)).Elem()
Expand Down Expand Up @@ -205,8 +205,8 @@ func RegisterPOJOMapping(javaClassName string, o interface{}) int {
clsDef.buffer = append(bHeader, bBody...)

structInfo.index = len(pojoRegistry.classInfoList)
pojoRegistry.classInfoList = append(pojoRegistry.classInfoList, clsDef)
pojoRegistry.registry[structInfo.goName] = structInfo
pojoRegistry.classInfoList = append(pojoRegistry.classInfoList, &clsDef)
pojoRegistry.registry[structInfo.goName] = &structInfo

return structInfo.index
}
Expand Down Expand Up @@ -307,8 +307,8 @@ func RegisterJavaEnum(o POJOEnum) int {
c = classInfo{javaName: t.javaName, fieldNameList: l}
c.buffer = append(c.buffer, b[:]...)
t.index = len(pojoRegistry.classInfoList)
pojoRegistry.classInfoList = append(pojoRegistry.classInfoList, c)
pojoRegistry.registry[t.goName] = t
pojoRegistry.classInfoList = append(pojoRegistry.classInfoList, &c)
pojoRegistry.registry[t.goName] = &t
i = t.index
} else {
i = -1
Expand All @@ -319,23 +319,32 @@ func RegisterJavaEnum(o POJOEnum) int {

// check if go struct name @goName has been registered or not.
func checkPOJORegistry(goName string) (int, bool) {
s, ok := loadPOJORegistry(goName)
if !ok {
return -1, false
}
return s.index, true
}

// load struct info if go struct name @goName has been registered or not.
func loadPOJORegistry(goName string) (*structInfo, bool) {
var (
ok bool
s structInfo
s *structInfo
)
pojoRegistry.RLock()
s, ok = pojoRegistry.registry[goName]
pojoRegistry.RUnlock()

return s.index, ok
return s, ok
}

// @typeName is class's java name
func getStructInfo(javaName string) (structInfo, bool) {
func getStructInfo(javaName string) (*structInfo, bool) {
var (
ok bool
g string
s structInfo
s *structInfo
)

pojoRegistry.RLock()
Expand All @@ -348,12 +357,12 @@ func getStructInfo(javaName string) (structInfo, bool) {
return s, ok
}

func getStructDefByIndex(idx int) (reflect.Type, classInfo, error) {
func getStructDefByIndex(idx int) (reflect.Type, *classInfo, error) {
var (
ok bool
clsName string
cls classInfo
s structInfo
cls *classInfo
s *structInfo
)

pojoRegistry.RLock()
Expand All @@ -380,7 +389,7 @@ func getStructDefByIndex(idx int) (reflect.Type, classInfo, error) {
func createInstance(goName string) interface{} {
var (
ok bool
s structInfo
s *structInfo
)

pojoRegistry.RLock()
Expand Down
6 changes: 3 additions & 3 deletions serialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func init() {

type Serializer interface {
EncObject(*Encoder, POJO) error
DecObject(*Decoder, reflect.Type, classInfo) (interface{}, error)
DecObject(*Decoder, reflect.Type, *classInfo) (interface{}, error)
}

var serializerMap = make(map[string]Serializer, 16)
Expand All @@ -53,7 +53,7 @@ func GetSerializer(javaClassName string) (Serializer, bool) {

type IntegerSerializer struct{}

func (IntegerSerializer) DecObject(d *Decoder, typ reflect.Type, cls classInfo) (interface{}, error) {
func (IntegerSerializer) DecObject(d *Decoder, typ reflect.Type, cls *classInfo) (interface{}, error) {
bigInt, err := d.decInstance(typ, cls)
if err != nil {
return nil, err
Expand Down Expand Up @@ -88,7 +88,7 @@ func (DecimalSerializer) EncObject(e *Encoder, v POJO) error {
return e.encObject(decimal)
}

func (DecimalSerializer) DecObject(d *Decoder, typ reflect.Type, cls classInfo) (interface{}, error) {
func (DecimalSerializer) DecObject(d *Decoder, typ reflect.Type, cls *classInfo) (interface{}, error) {
dec, err := d.decInstance(typ, cls)
if err != nil {
return nil, err
Expand Down