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 with last fastest release v0.2 #157

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
25 changes: 17 additions & 8 deletions benchmarks.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,23 @@ type BenchmarkCase struct {
}

var benchmarkCases = []BenchmarkCase{
{
Name: "fastape",
URL: "github.com/nazarifard/fastape",
New: fastape.NewTape,

UnsafeStringUnmarshal: true,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If fastape has both safe and unsafe unmarshalling versions, you should have the fastape_reuse use the unsafe version, and this one use the safe string decoding version.

TimeSupport: TSUnixNs,
APIKind: AKManual,
}, {
Name: "fastape_reuse",
URL: "github.com/nazarifard/fastape",
New: fastape.NewTape_Reuse,

UnsafeStringUnmarshal: true,
TimeSupport: TSUnixNs,
APIKind: AKManual,
},
{
Name: "gotiny",
URL: "github.com/niubaoshu/gotiny",
Expand Down Expand Up @@ -476,13 +493,5 @@ var benchmarkCases = []BenchmarkCase{
Notes: []string{
"This is a manually written encoding, designed to be the fastest possible for this benchmark.",
},
}, {
Name: "fastape",
URL: "github.com/nazarifard/fastape",
New: fastape.NewTape,

UnsafeStringUnmarshal: true,
TimeSupport: TSUnixNs,
APIKind: AKManual,
},
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ require (

require (
github.com/nazarifard/copi v0.0.0-20240609072615-763316f77579 // indirect
github.com/nazarifard/fastape v0.0.0-20240611084216-abaecf150e5b // indirect
github.com/nazarifard/fastape v0.0.0-20240618210653-607305a3ae51 // indirect
)

require (
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,8 @@ github.com/nazarifard/copi v0.0.0-20240609072615-763316f77579 h1:toqD8/J9DygfET+
github.com/nazarifard/copi v0.0.0-20240609072615-763316f77579/go.mod h1:RZX6PlUi+vF52MjBneuJcoszjuejOPWdMnAKaJOccGc=
github.com/nazarifard/fastape v0.0.0-20240611084216-abaecf150e5b h1:Roh7HBLqAxLpo/qxWztQlzt6QUBGTCX9AXxCaJf+pB8=
github.com/nazarifard/fastape v0.0.0-20240611084216-abaecf150e5b/go.mod h1:F54tpGg4uMgsYNUqD07iNENNGcS2GJobr+td/iZ5Xm4=
github.com/nazarifard/fastape v0.0.0-20240618210653-607305a3ae51 h1:QocQlmZXhuz9I8TOqe+df2kDqq5HWeWWwZnT0uVNUxw=
github.com/nazarifard/fastape v0.0.0-20240618210653-607305a3ae51/go.mod h1:F54tpGg4uMgsYNUqD07iNENNGcS2GJobr+td/iZ5Xm4=
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
77 changes: 51 additions & 26 deletions internal/serializers/fastape/fastape.go
Original file line number Diff line number Diff line change
@@ -1,44 +1,51 @@
package fastape

import (
"time"

"github.com/alecthomas/go_serialization_benchmarks/goserbench"
"github.com/nazarifard/fastape"
)

type smallStructTape struct {
NameTape fastape.StringTape
BirthDayTape fastape.TimeTape
BirthDayTape fastape.UnitTape[int64]
PhoneTape fastape.StringTape
SiblingsTape fastape.UnitTape[int]
SiblingsTape fastape.UnitTape[byte]
SpouseTape fastape.UnitTape[bool]
MoneyTape fastape.UnitTape[float64]
buf []byte
}

func (cp smallStructTape) Sizeof(p goserbench.SmallStruct) int {
return cp.NameTape.Sizeof(p.Name) +
cp.BirthDayTape.Sizeof(p.BirthDay) +
cp.PhoneTape.Sizeof(p.Phone) +
cp.SiblingsTape.Sizeof(p.Siblings) +
cp.SpouseTape.Sizeof(p.Spouse) +
cp.MoneyTape.Sizeof(p.Money)
}

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

birthday := p.BirthDay.UnixNano()
if cp.buf != nil {
buf = cp.buf //bufer reuse
} else {
sizeof := cp.NameTape.Sizeof(p.Name) +
cp.BirthDayTape.Sizeof(birthday) +
cp.PhoneTape.Sizeof(p.Phone) +
cp.SiblingsTape.Sizeof(byte(p.Siblings)) +
cp.SpouseTape.Sizeof(p.Spouse) +
cp.MoneyTape.Sizeof(p.Money)

buf = make([]byte, sizeof)
}

k, n := 0, 0
k, _ = cp.NameTape.Marshal(p.Name, buf[n:])
k, _ = cp.NameTape.Roll(p.Name, buf[n:])
n += k
k, _ = cp.BirthDayTape.Marshal(p.BirthDay, buf[n:])
k, _ = cp.BirthDayTape.Roll(birthday, buf[n:])
n += k
k, _ = cp.PhoneTape.Marshal(p.Phone, buf[n:])
k, _ = cp.PhoneTape.Roll(p.Phone, buf[n:])
n += k
k, _ = cp.SiblingsTape.Marshal(p.Siblings, buf[n:])
k, _ = cp.SiblingsTape.Roll(byte(p.Siblings), buf[n:])
n += k
k, _ = cp.SpouseTape.Marshal(p.Spouse, buf[n:])
k, _ = cp.SpouseTape.Roll(p.Spouse, buf[n:])
n += k
k, _ = cp.MoneyTape.Marshal(p.Money, buf[n:])
k, _ = cp.MoneyTape.Roll(p.Money, buf[n:])
n += k
return
}
Expand All @@ -47,37 +54,41 @@ func (cp smallStructTape) Unmarshal(bs []byte, o interface{}) (err error) {
p := o.(*goserbench.SmallStruct)

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

k, err = cp.BirthDayTape.Unmarshal(bs[n:], &p.BirthDay)
var nano int64
k, err = cp.BirthDayTape.Unroll(bs[n:], &nano)
p.BirthDay = time.Unix(0, nano)
n += k
if err != nil {
return err
}

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

k, err = cp.SiblingsTape.Unmarshal(bs[n:], &p.Siblings)
var sib byte
k, err = cp.SiblingsTape.Unroll(bs[n:], &sib)
p.Siblings = int(sib)
n += k
if err != nil {
return err
}

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

k, err = cp.MoneyTape.Unmarshal(bs[n:], &p.Money)
k, err = cp.MoneyTape.Unroll(bs[n:], &p.Money)
n += k
if err != nil {
return err
Expand All @@ -87,5 +98,19 @@ func (cp smallStructTape) Unmarshal(bs []byte, o interface{}) (err error) {
}

func NewTape() goserbench.Serializer {
return smallStructTape{}
return &smallStructTape{}
}

func NewTape_Reuse() goserbench.Serializer {
const maxSize = 0 +
8 + //date
8 + //money
1 + //sibling
1 + //spouse
1 + goserbench.MaxSmallStructPhoneSize +
1 + goserbench.MaxSmallStructNameSize

return &smallStructTape{
buf: make([]byte, maxSize),
}
}
Loading