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

added Sizer type assert at cachedsize method. The sizecache is not ca… #412

Merged
merged 10 commits into from
Jun 17, 2018
5 changes: 5 additions & 0 deletions proto/pointer_reflect_gogo.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,8 @@ func (p pointer) getSlice(typ reflect.Type) reflect.Value {
slice = slice.Elem()
return slice
}

// Interface returns the struct pointer as an interface value.
func structPointer_Interface(ptr pointer, _ reflect.Type) interface{} {
return ptr.v.Interface()
}
6 changes: 6 additions & 0 deletions proto/pointer_unsafe_gogo.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,9 @@ func (p pointer) getSlice(typ reflect.Type) reflect.Value {
slice = slice.Elem()
return slice
}

// Interface returns the struct pointer, assumed to have element type t,
// as an interface value.
func structPointer_Interface(ptr pointer, t reflect.Type) interface{} {
return reflect.NewAt(t, unsafe.Pointer(ptr.p)).Interface()
}
6 changes: 5 additions & 1 deletion proto/table_marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,9 @@ func (u *marshalInfo) size(ptr pointer) int {
// cachedsize gets the size from cache. If there is no cache (i.e. message is not generated),
// fall back to compute the size.
func (u *marshalInfo) cachedsize(ptr pointer) int {
if s, ok := structPointer_Interface(ptr, u.typ).(Sizer); ok {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really clean solution :)

return s.Size()
}
if u.sizecache.IsValid() {
return int(atomic.LoadInt32(ptr.offset(u.sizecache).toInt32()))
}
Expand Down Expand Up @@ -2743,7 +2746,8 @@ func (p *Buffer) Marshal(pb Message) error {
if m, ok := pb.(Marshaler); ok {
// If the message can marshal itself, let it do it, for compatibility.
// NOTE: This is not efficient.
b, err := m.Marshal()
var b []byte
b, err = m.Marshal()
p.buf = append(p.buf, b...)
return err
}
Expand Down