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

add low level IDR encoding demo #156

Merged
merged 3 commits into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
10 changes: 10 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,15 @@ var benchmarkCases = []BenchmarkCase{
UnsafeStringUnmarshal: true,
TimeSupport: TSNoSupport,
APIKind: AKManual,
}, {
Name: "idr",
URL: "github.com/chmike/ditp",
New: idr.NewIDRSerializer,

BufferReuseMarshal: true,
chmike marked this conversation as resolved.
Show resolved Hide resolved
TimeSupport: TSFullTzOffset,
APIKind: AKManual,
Notes: []string{"low level IDR encoding demo with same nsec time encoding as benc"},
chmike marked this conversation as resolved.
Show resolved Hide resolved
}, {
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
42 changes: 42 additions & 0 deletions internal/serializers/idr/idr.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package idr

import (
"time"

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

type IDRSerializer struct {
e low.Encoder
}

func (s IDRSerializer) Marshal(o interface{}) ([]byte, error) {
v := o.(*goserbench.SmallStruct)
e := low.Reset(s.e)
e = low.PutInt64(e, v.BirthDay.UnixNano())
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[21]
d, v0 := low.Int64(d)
v.BirthDay = time.Unix(0, v0)
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{e: make([]byte, 256)} // set initial buffer to 256 bytes
}