diff --git a/go.mod b/go.mod index c96dfbe..ff5883a 100644 --- a/go.mod +++ b/go.mod @@ -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 @@ -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 diff --git a/go.sum b/go.sum index 5eb7471..7964b92 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/internal/serializers/copi/copi.go b/internal/serializers/copi/copi.go new file mode 100644 index 0000000..de8417c --- /dev/null +++ b/internal/serializers/copi/copi.go @@ -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{} +} diff --git a/serialization_benchmarks_test.go b/serialization_benchmarks_test.go index f32a709..a022b92 100644 --- a/serialization_benchmarks_test.go +++ b/serialization_benchmarks_test.go @@ -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" @@ -231,6 +232,10 @@ var benchmarkCases = []BenchmarkCase{ Name: "baseline", URL: "", New: baseline.NewBaselineSerializer, + }, { + Name: "copi", + URL: "github.com/nazarifard/copi", + New: copi.NewCopiSerializer, }, }