Skip to content

Commit d30f965

Browse files
Add github.com/calmh/xdr
Does not support time.Time or floats.
1 parent 437b7cf commit d30f965

File tree

4 files changed

+168
-4
lines changed

4 files changed

+168
-4
lines changed

Makefile

+8-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# This is necessary due to the use of two conflicting generator commands for capnproto
22
.NOTPARALLEL:
33

4-
all: Colfer.go FlatBufferA.go msgp_gen.go structdef-gogo.pb.go structdef.pb.go structdef.capnp.go structdef.capnp2.go gencode.schema.gen.go gencode-unsafe.schema.gen.go
4+
all: Colfer.go FlatBufferA.go msgp_gen.go structdef-gogo.pb.go structdef.pb.go structdef.capnp.go structdef.capnp2.go gencode.schema.gen.go gencode-unsafe.schema.gen.go structdefxdr_generated.go
55

66
Colfer.go:
77
colf go
@@ -33,16 +33,19 @@ structdef.capnp2.go: structdef.capnp2
3333
structdef.capnp.go: structdef.capnp
3434
go get -u github.com/glycerine/go-capnproto/capnpc-go # conflicts with capnproto2
3535
capnp compile -I${GOPATH}/src -ogo structdef.capnp
36-
36+
3737
gencode.schema.gen.go: gencode.schema
3838
gencode go -schema=gencode.schema -package=goserbench
39-
39+
4040
gencode-unsafe.schema.gen.go: gencode-unsafe.schema
4141
gencode go -schema=gencode-unsafe.schema -package=goserbench -unsafe
4242

43+
structdefxdr_generated.go: structdefxdr.go
44+
go generate
45+
4346
.PHONY: clean
4447
clean:
45-
rm -f Colfer.go FlatBufferA.go msgp_gen.go structdef-gogo.pb.go structdef.pb.go structdef.capnp.go structdef.capnp2.go gencode.schema.gen.go gencode-unsafe.schema.gen.go
48+
rm -f Colfer.go FlatBufferA.go msgp_gen.go structdef-gogo.pb.go structdef.pb.go structdef.capnp.go structdef.capnp2.go gencode.schema.gen.go gencode-unsafe.schema.gen.go structdefxdr_generated.go
4649

4750
.PHONY: install
4851
install:
@@ -65,3 +68,4 @@ install:
6568
go get -u github.com/golang/protobuf/proto
6669
go get -u github.com/hprose/hprose-go/io
6770
go get -u github.com/pascaldekloe/colfer/cmd/colf
71+
go get -u github.com/calmh/xdr

serialization_benchmarks_test.go

+56
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"encoding/gob"
66
"encoding/json"
77
"fmt"
8+
"math"
89
"math/rand"
910
"os"
1011
"testing"
@@ -934,3 +935,58 @@ func BenchmarkGencodeUnsafeUnmarshal(b *testing.B) {
934935
}
935936
}
936937
}
938+
939+
// github.com/calmh/xdr
940+
941+
func generateXDR() []*XDRA {
942+
a := make([]*XDRA, 0, 1000)
943+
for i := 0; i < 1000; i++ {
944+
a = append(a, &XDRA{
945+
Name: randString(16),
946+
BirthDay: time.Now().UnixNano(),
947+
Phone: randString(10),
948+
Siblings: rand.Int31n(5),
949+
Spouse: rand.Intn(2) == 1,
950+
Money: math.Float64bits(rand.Float64()),
951+
})
952+
}
953+
return a
954+
}
955+
956+
func BenchmarkXDR2Marshal(b *testing.B) {
957+
b.StopTimer()
958+
data := generateXDR()
959+
b.ReportAllocs()
960+
b.StartTimer()
961+
for i := 0; i < b.N; i++ {
962+
data[rand.Intn(len(data))].MarshalXDR()
963+
}
964+
}
965+
966+
func BenchmarkXDR2Unmarshal(b *testing.B) {
967+
b.StopTimer()
968+
data := generateXDR()
969+
ser := make([][]byte, len(data))
970+
for i, d := range data {
971+
ser[i] = d.MustMarshalXDR()
972+
}
973+
b.ReportAllocs()
974+
b.StartTimer()
975+
for i := 0; i < b.N; i++ {
976+
n := rand.Intn(len(ser))
977+
o := XDRA{}
978+
err := o.UnmarshalXDR(ser[n])
979+
if err != nil {
980+
b.Fatalf("xdr failed to unmarshal: %s (%s)", err, ser[n])
981+
}
982+
// Validate unmarshalled data.
983+
if validate != "" {
984+
i := data[n]
985+
correct := o.Name == i.Name && o.Phone == i.Phone && o.Siblings == i.Siblings && o.Spouse == i.Spouse && o.Money == i.Money && o.BirthDay == i.BirthDay
986+
if !correct {
987+
b.Fatalf("unmarshaled object differed:\n%v\n%v", i, o)
988+
}
989+
}
990+
}
991+
}
992+

structdefxdr.go

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package goserbench
2+
3+
//go:generate -command genxdr go run ../../calmh/xdr/cmd/genxdr/main.go
4+
//go:generate genxdr -o structdefxdr_generated.go structdefxdr.go
5+
type XDRA struct {
6+
Name string
7+
BirthDay int64
8+
Phone string
9+
Siblings int32
10+
Spouse bool
11+
Money uint64
12+
}

structdefxdr_generated.go

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
// ************************************************************
2+
// This file is automatically generated by genxdr. Do not edit.
3+
// ************************************************************
4+
5+
package goserbench
6+
7+
import (
8+
"github.com/calmh/xdr"
9+
)
10+
11+
/*
12+
13+
XDRA Structure:
14+
15+
0 1 2 3
16+
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
17+
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
18+
/ /
19+
\ Name (length + padded data) \
20+
/ /
21+
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
22+
| |
23+
+ Birth Day (64 bits) +
24+
| |
25+
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
26+
/ /
27+
\ Phone (length + padded data) \
28+
/ /
29+
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
30+
| Siblings |
31+
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
32+
| Spouse (V=0 or 1) |V|
33+
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
34+
| |
35+
+ Money (64 bits) +
36+
| |
37+
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
38+
39+
40+
struct XDRA {
41+
string Name<>;
42+
hyper BirthDay;
43+
string Phone<>;
44+
int Siblings;
45+
bool Spouse;
46+
unsigned hyper Money;
47+
}
48+
49+
*/
50+
51+
func (o XDRA) XDRSize() int {
52+
return 4 + len(o.Name) + xdr.Padding(len(o.Name)) + 8 +
53+
4 + len(o.Phone) + xdr.Padding(len(o.Phone)) + 4 + 4 + 8
54+
}
55+
56+
func (o XDRA) MarshalXDR() ([]byte, error) {
57+
buf := make([]byte, o.XDRSize())
58+
m := &xdr.Marshaller{Data: buf}
59+
return buf, o.MarshalXDRInto(m)
60+
}
61+
62+
func (o XDRA) MustMarshalXDR() []byte {
63+
bs, err := o.MarshalXDR()
64+
if err != nil {
65+
panic(err)
66+
}
67+
return bs
68+
}
69+
70+
func (o XDRA) MarshalXDRInto(m *xdr.Marshaller) error {
71+
m.MarshalString(o.Name)
72+
m.MarshalUint64(uint64(o.BirthDay))
73+
m.MarshalString(o.Phone)
74+
m.MarshalUint32(uint32(o.Siblings))
75+
m.MarshalBool(o.Spouse)
76+
m.MarshalUint64(o.Money)
77+
return m.Error
78+
}
79+
80+
func (o *XDRA) UnmarshalXDR(bs []byte) error {
81+
u := &xdr.Unmarshaller{Data: bs}
82+
return o.UnmarshalXDRFrom(u)
83+
}
84+
func (o *XDRA) UnmarshalXDRFrom(u *xdr.Unmarshaller) error {
85+
o.Name = u.UnmarshalString()
86+
o.BirthDay = int64(u.UnmarshalUint64())
87+
o.Phone = u.UnmarshalString()
88+
o.Siblings = int32(u.UnmarshalUint32())
89+
o.Spouse = u.UnmarshalBool()
90+
o.Money = u.UnmarshalUint64()
91+
return u.Error
92+
}

0 commit comments

Comments
 (0)