Skip to content

Commit

Permalink
Merge pull request #156 from chmike/master
Browse files Browse the repository at this point in the history
add low level IDR encoding demo
  • Loading branch information
deneonet committed Jun 18, 2024
2 parents 2f4d936 + 205f42f commit 0d9a3f2
Show file tree
Hide file tree
Showing 5 changed files with 447 additions and 311 deletions.
18 changes: 18 additions & 0 deletions benchmarks.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/alecthomas/go_serialization_benchmarks/internal/serializers/gotiny"
"github.com/alecthomas/go_serialization_benchmarks/internal/serializers/hprose"
"github.com/alecthomas/go_serialization_benchmarks/internal/serializers/hprose2"
"github.com/alecthomas/go_serialization_benchmarks/internal/serializers/idr"
"github.com/alecthomas/go_serialization_benchmarks/internal/serializers/ikea"
"github.com/alecthomas/go_serialization_benchmarks/internal/serializers/jsoniter"
"github.com/alecthomas/go_serialization_benchmarks/internal/serializers/mongobson"
Expand Down Expand Up @@ -454,6 +455,23 @@ var benchmarkCases = []BenchmarkCase{
UnsafeStringUnmarshal: true,
TimeSupport: TSNoSupport,
APIKind: AKManual,
}, {
Name: "idr",
URL: "github.com/chmike/ditp",
New: idr.NewIDRSerializer,

TimeSupport: TSFullTzOffset,
APIKind: AKManual,
Notes: []string{"Low level IDR encoding demo"},
}, {
Name: "idr/reuse",
URL: "github.com/chmike/ditp",
New: idr.NewIDRSerializerReuse,

BufferReuseMarshal: true,
TimeSupport: TSFullTzOffset,
APIKind: AKManual,
Notes: []string{"Low level IDR encoding demo"},
}, {
Name: "baseline",
URL: "",
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ require (
)

require (
github.com/chmike/ditp v0.0.0-20240618130435-627cf7ce9ad6 // indirect
github.com/nazarifard/copi v0.0.0-20240609072615-763316f77579 // indirect
github.com/nazarifard/fastape v0.0.0-20240611084216-abaecf150e5b // indirect
)
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ github.com/bketelsen/crypt v0.0.3-0.20200106085610-5cbc8cc4026c/go.mod h1:MKsuJm
github.com/calmh/xdr v1.1.0 h1:U/Dd4CXNLoo8EiQ4ulJUXkgO1/EyQLgDKLgpY1SOoJE=
github.com/calmh/xdr v1.1.0/go.mod h1:E8sz2ByAdXC8MbANf1LCRYzedSnnc+/sXXJs/PVqoeg=
github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
github.com/chmike/ditp v0.0.0-20240618130435-627cf7ce9ad6 h1:FIQLUAh5cGcvt6Sm4wD2TUbDDJjCnmpv1A/lnJDBMh0=
github.com/chmike/ditp v0.0.0-20240618130435-627cf7ce9ad6/go.mod h1:Az8w1aHUmNFs7+C7v41Y/cpXIBafvOs+7J7M/KGsM1c=
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
github.com/coreos/bbolt v1.3.2/go.mod h1:iRUV2dpdMOn7Bo10OQBFzIJO9kkE559Wcmn+qkEiiKk=
github.com/coreos/etcd v3.3.13+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE=
Expand Down
71 changes: 71 additions & 0 deletions internal/serializers/idr/idr.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package idr

import (
"github.com/alecthomas/go_serialization_benchmarks/goserbench"
"github.com/chmike/ditp/idr/low"
)

type IDRSerializer struct {
}

func (s IDRSerializer) Marshal(o interface{}) ([]byte, error) {
v := o.(*goserbench.SmallStruct)
e := make([]byte, 0, 64)
e = low.PutTime(e, v.BirthDay)
e = low.PutFloat64(e, v.Money)
e = low.PutInt32(e, int32(v.Siblings))
e = low.PutBool(e, v.Spouse)
e = low.PutString(e, v.Name)
e = low.PutString(e, v.Phone)
return e, nil
}

func (s IDRSerializer) Unmarshal(d []byte, o interface{}) (err error) {
v := o.(*goserbench.SmallStruct)
_ = d[29]
d, v.BirthDay = low.Time(d)
d, v.Money = low.Float64(d)
d, v1 := low.Int32(d)
v.Siblings = int(v1)
d, v.Spouse = low.Bool(d)
d, v.Name = low.String(d, 1<<14)
_, v.Phone = low.String(d, 20)
return nil
}

func NewIDRSerializer() goserbench.Serializer {
return IDRSerializer{} // set initial buffer to 256 bytes
}

type IDRSerializerReuse struct {
e low.Encoder
}

func (s IDRSerializerReuse) Marshal(o interface{}) ([]byte, error) {
v := o.(*goserbench.SmallStruct)
e := low.Reset(s.e)
e = low.PutTime(e, v.BirthDay)
e = low.PutFloat64(e, v.Money)
e = low.PutInt32(e, int32(v.Siblings))
e = low.PutBool(e, v.Spouse)
e = low.PutString(e, v.Name)
e = low.PutString(e, v.Phone)
return e, nil
}

func (s IDRSerializerReuse) Unmarshal(d []byte, o interface{}) (err error) {
v := o.(*goserbench.SmallStruct)
_ = d[29]
d, v.BirthDay = low.Time(d)
d, v.Money = low.Float64(d)
d, v1 := low.Int32(d)
v.Siblings = int(v1)
d, v.Spouse = low.Bool(d)
d, v.Name = low.String(d, 1<<14)
_, v.Phone = low.String(d, 20)
return nil
}

func NewIDRSerializerReuse() goserbench.Serializer {
return IDRSerializerReuse{e: make([]byte, 256)} // set initial buffer to 256 bytes
}
Loading

0 comments on commit 0d9a3f2

Please sign in to comment.