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

Update MUS to work like Bebop #169

Closed
wants to merge 1 commit into from
Closed
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
45 changes: 23 additions & 22 deletions internal/serializers/mus/mus.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,43 +5,45 @@ import (

"github.com/alecthomas/go_serialization_benchmarks/goserbench"
"github.com/mus-format/mus-go/ord"
"github.com/mus-format/mus-go/raw"
"github.com/mus-format/mus-go/unsafe"
"github.com/mus-format/mus-go/varint"
)

type MUSSerializer struct{}

func (s MUSSerializer) Marshal(o interface{}) ([]byte, error) {
v := o.(*goserbench.SmallStruct)
var (
v = o.(*goserbench.SmallStruct)
nano = v.BirthDay.UnixNano()
)
n := ord.SizeString(v.Name)
n += raw.SizeInt64(v.BirthDay.UnixNano())
n += unsafe.SizeInt64(nano)
n += ord.SizeString(v.Phone)
n += varint.SizeInt32(int32(v.Siblings))
n += ord.SizeBool(v.Spouse)
n += raw.SizeFloat64(v.Money)
n += unsafe.SizeInt32(int32(v.Siblings))
n += unsafe.SizeBool(v.Spouse)
n += unsafe.SizeFloat64(v.Money)
buf := make([]byte, n)
n = ord.MarshalString(v.Name, buf)
n += raw.MarshalInt64(v.BirthDay.UnixNano(), buf[n:])
n += unsafe.MarshalInt64(nano, buf[n:])
n += ord.MarshalString(v.Phone, buf[n:])
n += varint.MarshalInt32(int32(v.Siblings), buf[n:])
n += ord.MarshalBool(v.Spouse, buf[n:])
raw.MarshalFloat64(v.Money, buf[n:])
n += unsafe.MarshalInt32(int32(v.Siblings), buf[n:])
n += unsafe.MarshalBool(v.Spouse, buf[n:])
unsafe.MarshalFloat64(v.Money, buf[n:])
return buf, nil
}

func (s MUSSerializer) Unmarshal(bs []byte, o interface{}) (err error) {
v := o.(*goserbench.SmallStruct)
var n int
v.Name, n, err = ord.UnmarshalString(bs)
if err != nil {
return
}
var (
v = o.(*goserbench.SmallStruct)
n int
n1 int
birthDay64 int64
siblings32 int32
)
birthDay64, n1, err = raw.UnmarshalInt64(bs[n:])
v.Name, n, err = ord.UnmarshalString(bs)
if err != nil {
return
}
birthDay64, n1, err = unsafe.UnmarshalInt64(bs[n:])
n += n1
if err != nil {
return
Expand All @@ -52,19 +54,18 @@ func (s MUSSerializer) Unmarshal(bs []byte, o interface{}) (err error) {
if err != nil {
return
}
var siblings32 int32
siblings32, n1, err = varint.UnmarshalInt32(bs[n:])
siblings32, n1, err = unsafe.UnmarshalInt32(bs[n:])
n += n1
if err != nil {
return
}
v.Siblings = int(siblings32)
v.Spouse, n1, err = ord.UnmarshalBool(bs[n:])
v.Spouse, n1, err = unsafe.UnmarshalBool(bs[n:])
n += n1
if err != nil {
return
}
v.Money, n1, err = raw.UnmarshalFloat64(bs[n:])
v.Money, n1, err = unsafe.UnmarshalFloat64(bs[n:])
n += n1
return
}
Expand Down
Loading