Skip to content

Commit

Permalink
copi: a new modern high performance go serializer (alecthomas#143)
Browse files Browse the repository at this point in the history
  • Loading branch information
nazarifard authored Jun 9, 2024
1 parent 1b2ac9c commit 6992cb1
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 1 deletion.
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/alecthomas/go_serialization_benchmarks

go 1.21.4
go 1.22.4

require (
github.com/200sc/bebop v0.5.0
Expand Down Expand Up @@ -34,6 +34,8 @@ require (
wellquite.org/bebop v0.0.0-20231109192402-a92af83691ec
)

require github.com/nazarifard/copi v0.0.0-20240609072615-763316f77579 // indirect

require (
github.com/DataDog/zstd v1.5.2 // indirect
github.com/cosmos/cosmos-proto v1.0.0-beta.3
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,8 @@ github.com/mus-format/mus-common-go v0.0.0-20230426111652-d1324c6517b3/go.mod h1
github.com/mus-format/mus-go v0.0.0-20230426134740-6572513fca3d h1:/FqtbOoougvCbsp769ZZxgN3Pj/fepBAr4ETQXM+uwQ=
github.com/mus-format/mus-go v0.0.0-20230426134740-6572513fca3d/go.mod h1:zdDnTFri6XqdQVRTE/HuGnmn+/3c3a0ODNUsw+9SXS8=
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
github.com/nazarifard/copi v0.0.0-20240609072615-763316f77579 h1:toqD8/J9DygfET+HZRsIu4kLUz32E4qcUhR1USRNPrY=
github.com/nazarifard/copi v0.0.0-20240609072615-763316f77579/go.mod h1:RZX6PlUi+vF52MjBneuJcoszjuejOPWdMnAKaJOccGc=
github.com/neelance/astrewrite v0.0.0-20160511093645-99348263ae86/go.mod h1:kHJEU3ofeGjhHklVoIGuVj85JJwZ6kWPaJwCIxgnFmo=
github.com/neelance/sourcemap v0.0.0-20200213170602-2833bce08e4c/go.mod h1:Qr6/a/Q4r9LP1IltGz7tA7iOK1WonHEYhu1HRBA7ZiM=
github.com/niubaoshu/goutils v0.0.0-20180828035119-e8e576f66c2b h1:T7vmCmpGIvqlOOp5SXatALP+HYc/40ZHbxmgy+p+sN0=
Expand Down
91 changes: 91 additions & 0 deletions internal/serializers/copi/copi.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package copi

import (
"github.com/alecthomas/go_serialization_benchmarks/goserbench"
"github.com/nazarifard/copi"
)

type smallStructCp struct {
NameCp copi.StringCp
BirthDayCp copi.TimeCp
PhoneCp copi.StringCp
SiblingsCp copi.UnitCp[int]
SpouseCp copi.UnitCp[bool]
MoneyCp copi.UnitCp[float64]
}

func (cp smallStructCp) SizeOf(p goserbench.SmallStruct) int {
return cp.NameCp.SizeOf(p.Name) +
cp.BirthDayCp.SizeOf(p.BirthDay) +
cp.PhoneCp.SizeOf(p.Phone) +
cp.SiblingsCp.SizeOf(p.Siblings) +
cp.SpouseCp.SizeOf(p.Spouse) +
cp.MoneyCp.SizeOf(p.Money)
}

func (cp smallStructCp) Marshal(o interface{}) (buf []byte, err error) {
p := o.(*goserbench.SmallStruct)
buf = make([]byte, cp.SizeOf(*p))

k, n := 0, 0
k, _ = cp.NameCp.Marshal(p.Name, buf[n:])
n += k
k, _ = cp.BirthDayCp.Marshal(p.BirthDay, buf[n:])
n += k
k, _ = cp.PhoneCp.Marshal(p.Phone, buf[n:])
n += k
k, _ = cp.SiblingsCp.Marshal(p.Siblings, buf[n:])
n += k
k, _ = cp.SpouseCp.Marshal(p.Spouse, buf[n:])
n += k
k, _ = cp.MoneyCp.Marshal(p.Money, buf[n:])
n += k
return
}

func (cp smallStructCp) Unmarshal(bs []byte, o interface{}) (err error) {
p := o.(*goserbench.SmallStruct)

k, n := 0, 0
k, err = cp.NameCp.Unmarshal(bs[n:], &p.Name)
n += k
if err != nil {
return err
}

k, err = cp.BirthDayCp.Unmarshal(bs[n:], &p.BirthDay)
n += k
if err != nil {
return err
}

k, err = cp.PhoneCp.Unmarshal(bs[n:], &p.Phone)
n += k
if err != nil {
return err
}

k, err = cp.SiblingsCp.Unmarshal(bs[n:], &p.Siblings)
n += k
if err != nil {
return err
}

k, err = cp.SpouseCp.Unmarshal(bs[n:], &p.Spouse)
n += k
if err != nil {
return err
}

k, err = cp.MoneyCp.Unmarshal(bs[n:], &p.Money)
n += k
if err != nil {
return err
}

return
}

func NewCopiSerializer() goserbench.Serializer {
return smallStructCp{}
}
5 changes: 5 additions & 0 deletions serialization_benchmarks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/alecthomas/go_serialization_benchmarks/internal/serializers/bson"
"github.com/alecthomas/go_serialization_benchmarks/internal/serializers/capnproto"
"github.com/alecthomas/go_serialization_benchmarks/internal/serializers/colfer"
"github.com/alecthomas/go_serialization_benchmarks/internal/serializers/copi"
"github.com/alecthomas/go_serialization_benchmarks/internal/serializers/easyjson"
"github.com/alecthomas/go_serialization_benchmarks/internal/serializers/fastjson"
"github.com/alecthomas/go_serialization_benchmarks/internal/serializers/flatbuffers"
Expand Down Expand Up @@ -231,6 +232,10 @@ var benchmarkCases = []BenchmarkCase{
Name: "baseline",
URL: "",
New: baseline.NewBaselineSerializer,
}, {
Name: "copi",
URL: "github.com/nazarifard/copi",
New: copi.NewCopiSerializer,
},
}

Expand Down

0 comments on commit 6992cb1

Please sign in to comment.