Skip to content

Commit

Permalink
Don't copy []byte slices (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
outofforest authored Aug 31, 2024
1 parent 379e8aa commit 905e939
Show file tree
Hide file tree
Showing 3 changed files with 203 additions and 154 deletions.
309 changes: 168 additions & 141 deletions test/benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,37 +10,6 @@ import (
// go test -bench=. -run=^$ -cpuprofile profile.out
// go tool pprof -http="localhost:8000" pprofbin ./profile.out

var _ iface = implValue{}
var _ iface = &implPointer{}

type iface interface {
Method(arg string) string
}

type implValue struct {
}

func (i implValue) Method(arg string) string {
return arg
}

type implPointer struct {
}

func (i *implPointer) Method(arg string) string {
return arg
}

func method(arg string) string {
return arg
}

var methodVar = func(arg string) string {
return arg
}

var str = ""

func BenchmarkValue(b *testing.B) {
b.StopTimer()
b.ResetTimer()
Expand Down Expand Up @@ -227,126 +196,184 @@ func BenchmarkInterfacePointerMethodInVar(b *testing.B) {
}
}

var msg = pkg1.MsgMixed{
Value1: map[string]spkg1.SubMsg{
"aa": {
Value: 143443,
},
"fdfsdsd": {
Value: 433,
func BenchmarkMarshalingMixed(b *testing.B) {
b.StopTimer()
b.ResetTimer()

var msg2 pkg1.MsgMixed
buf := make([]byte, msgMixed.Size())

b.StartTimer()
for range b.N {
msgMixed.Marshal(buf)
msg2.Unmarshal(buf)
}
b.StopTimer()
}

func BenchmarkMarshalingByteSlices(b *testing.B) {
b.StopTimer()
b.ResetTimer()

var msg2 pkg1.MsgSliceUint8
buf := make([]byte, msgBytes.Size())

b.StartTimer()
for range b.N {
msgBytes.Marshal(buf)
msg2.Unmarshal(buf)
}
b.StopTimer()
}

var (
msgMixed = pkg1.MsgMixed{
Value1: map[string]spkg1.SubMsg{
"aa": {
Value: 143443,
},
"fdfsdsd": {
Value: 433,
},
"frewrwerwerwedfsdsd": {
Value: -433,
},
"dfsdfsd": {
Value: 100000,
},
"dsjklfjdsklfjdslkjflsdkfsfjlskdjflskdjfkldsjklfsdjklfjsdlkslioisufsdlf": {
Value: 43423423,
},
},
"frewrwerwerwedfsdsd": {
Value: -433,
Value2: map[uint8][]string{
4: {"aafsdfsd", "fdsfsdfdsbb", "fdsfsdfsdfstwsfsdfsdfdsfdsfsd", "fdfsdfsdffdsfds"},
1: {"fdsfsdfsdfsdaafsdfsd", "fdsfsdffsdfsdfdsdsbb", "fdsfsfdsfsdfsddfsdfstwsfsdfsdfdsfdsfsd",
"fdfdsfsdfsdfsdfsdffdsfds"},
8: {"fdsfsdsdaafsdfsd", "fdsfsdffsdfsdfdsdsbb", "fdsfsfdfsdfstwsfsdfsdfdsfdsfsd",
"fdfdsfsfsdfsdfsdfsfsfsdsddfsdfsdfsdffdsfds"},
10: {"fdsfsdsdaafsdfsd", "fdsfsdffsdfsdfdsdsbb", "fdsfsfdfsdfstwsfsdfsdfdsfdsfsd",
"fdfdsfsfsdfsdfsdfsfsfsdsddfsdfsdfsdffdsfds"},
41: {"aafsdfsd", "fdsfsdfdsbb", "fdsfsdfsdfstwsfsdfsdfdsfdsfsd", "fdfsdfsdffdsfds"},
11: {"fdsfsdfsdfsdaafsdfsd", "fdsfsdffsdfsdfdsdsbb", "fdsfsfdsfsdfsddfsdfstwsfsdfsdfdsfdsfsd",
"fdfdsfsdfsdfsdfsdffdsfds"},
81: {"fdsfsdsdaafsdfsd", "fdsfsdffsdfsdfdsdsbb", "fdsfsfdfsdfstwsfsdfsdfdsfdsfsd",
"fdfdsfsfsdfsdfsdfsfsfsdsddfsdfsdfsdffdsfds"},
110: {"fdsfsdsdaafsdfsd", "fdsfsdffsdfsdfdsdsbb", "fdsfsfdfsdfstwsfsdfsdfdsfdsfsd",
"fdfdsfsfsdfsdfsdfsfsfsdsddfsdfsdfsdffdsfds"},
42: {"aafsdfsd", "fdsfsdfdsbb", "fdsfsdfsdfstwsfsdfsdfdsfdsfsd", "fdfsdfsdffdsfds"},
13: {"fdsfsdfsdfsdaafsdfsd", "fdsfsdffsdfsdfdsdsbb", "fdsfsfdsfsdfsddfsdfstwsfsdfsdfdsfdsfsd",
"fdfdsfsdfsdfsdfsdffdsfds"},
84: {"fdsfsdsdaafsdfsd", "fdsfsdffsdfsdfdsdsbb", "fdsfsfdfsdfstwsfsdfsdfdsfdsfsd",
"fdfdsfsfsdfsdfsdfsfsfsdsddfsdfsdfsdffdsfds"},
105: {"fdsfsdsdaafsdfsd", "fdsfsdffsdfsdfdsdsbb", "fdsfsfdfsdfstwsfsdfsdfdsfdsfsd",
"fdfdsfsfsdfsdfsdfsfsfsdsddfsdfsdfsdffdsfds"},
234: {"aafsdfsd", "fdsfsdfdsbb", "fdsfsdfsdfstwsfsdfsdfdsfdsfsd", "fdfsdfsdffdsfds"},
12: {"fdsfsdfsdfsdaafsdfsd", "fdsfsdffsdfsdfdsdsbb", "fdsfsfdsfsdfsddfsdfstwsfsdfsdfdsfdsfsd",
"fdfdsfsdfsdfsdfsdffdsfds"},
85: {"fdsfsdsdaafsdfsd", "fdsfsdffsdfsdfdsdsbb", "fdsfsfdfsdfstwsfsdfsdfdsfdsfsd",
"fdfdsfsfsdfsdfsdfsfsfsdsddfsdfsdfsdffdsfds"},
15: {"fdsfsdsdaafsdfsd", "fdsfsdffsdfsdfdsdsbb", "fdsfsfdfsdfstwsfsdfsdfdsfdsfsd",
"fdfdsfsfsdfsdfsdfsfsfsdsddfsdfsdfsdffdsfds"},
},
"dfsdfsd": {
Value: 100000,
Value3: [][32]uint16{
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
30, 31},
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
30, 31},
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
30, 31},
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
30, 31},
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
30, 31},
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
30, 31},
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
30, 31},
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
30, 31},
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
30, 31},
},
"dsjklfjdsklfjdslkjflsdkfsfjlskdjflskdjfkldsjklfsdjklfjsdlkslioisufsdlf": {
Value: 43423423,
Value4: [12]map[int8]float32{
{0: 1., 2: 5., 10: 11., 12: 56, 89: 100, 54: 43, 125: 435},
{0: 1., 2: 5., 10: 11., 12: 56, 89: 100, 54: 43, 125: 435},
{0: 1., 2: 5., 10: 11., 12: 56, 89: 100, 54: 43, 125: 435},
{0: 1., 2: 5., 10: 11., 12: 56, 89: 100, 54: 43, 125: 435},
{0: 1., 2: 5., 10: 11., 12: 56, 89: 100, 54: 43, 125: 435},
{0: 1., 2: 5., 10: 11., 12: 56, 89: 100, 54: 43, 125: 435},
},
},
Value2: map[uint8][]string{
4: {"aafsdfsd", "fdsfsdfdsbb", "fdsfsdfsdfstwsfsdfsdfdsfdsfsd", "fdfsdfsdffdsfds"},
1: {"fdsfsdfsdfsdaafsdfsd", "fdsfsdffsdfsdfdsdsbb", "fdsfsfdsfsdfsddfsdfstwsfsdfsdfdsfdsfsd",
"fdfdsfsdfsdfsdfsdffdsfds"},
8: {"fdsfsdsdaafsdfsd", "fdsfsdffsdfsdfdsdsbb", "fdsfsfdfsdfstwsfsdfsdfdsfdsfsd",
"fdfdsfsfsdfsdfsdfsfsfsdsddfsdfsdfsdffdsfds"},
10: {"fdsfsdsdaafsdfsd", "fdsfsdffsdfsdfdsdsbb", "fdsfsfdfsdfstwsfsdfsdfdsfdsfsd",
"fdfdsfsfsdfsdfsdfsfsfsdsddfsdfsdfsdffdsfds"},
41: {"aafsdfsd", "fdsfsdfdsbb", "fdsfsdfsdfstwsfsdfsdfdsfdsfsd", "fdfsdfsdffdsfds"},
11: {"fdsfsdfsdfsdaafsdfsd", "fdsfsdffsdfsdfdsdsbb", "fdsfsfdsfsdfsddfsdfstwsfsdfsdfdsfdsfsd",
"fdfdsfsdfsdfsdfsdffdsfds"},
81: {"fdsfsdsdaafsdfsd", "fdsfsdffsdfsdfdsdsbb", "fdsfsfdfsdfstwsfsdfsdfdsfdsfsd",
"fdfdsfsfsdfsdfsdfsfsfsdsddfsdfsdfsdffdsfds"},
110: {"fdsfsdsdaafsdfsd", "fdsfsdffsdfsdfdsdsbb", "fdsfsfdfsdfstwsfsdfsdfdsfdsfsd",
"fdfdsfsfsdfsdfsdfsfsfsdsddfsdfsdfsdffdsfds"},
42: {"aafsdfsd", "fdsfsdfdsbb", "fdsfsdfsdfstwsfsdfsdfdsfdsfsd", "fdfsdfsdffdsfds"},
13: {"fdsfsdfsdfsdaafsdfsd", "fdsfsdffsdfsdfdsdsbb", "fdsfsfdsfsdfsddfsdfstwsfsdfsdfdsfdsfsd",
"fdfdsfsdfsdfsdfsdffdsfds"},
84: {"fdsfsdsdaafsdfsd", "fdsfsdffsdfsdfdsdsbb", "fdsfsfdfsdfstwsfsdfsdfdsfdsfsd",
"fdfdsfsfsdfsdfsdfsfsfsdsddfsdfsdfsdffdsfds"},
105: {"fdsfsdsdaafsdfsd", "fdsfsdffsdfsdfdsdsbb", "fdsfsfdfsdfstwsfsdfsdfdsfdsfsd",
"fdfdsfsfsdfsdfsdfsfsfsdsddfsdfsdfsdffdsfds"},
234: {"aafsdfsd", "fdsfsdfdsbb", "fdsfsdfsdfstwsfsdfsdfdsfdsfsd", "fdfsdfsdffdsfds"},
12: {"fdsfsdfsdfsdaafsdfsd", "fdsfsdffsdfsdfdsdsbb", "fdsfsfdsfsdfsddfsdfstwsfsdfsdfdsfdsfsd",
"fdfdsfsdfsdfsdfsdffdsfds"},
85: {"fdsfsdsdaafsdfsd", "fdsfsdffsdfsdfdsdsbb", "fdsfsfdfsdfstwsfsdfsdfdsfdsfsd",
"fdfdsfsfsdfsdfsdfsfsfsdsddfsdfsdfsdffdsfds"},
15: {"fdsfsdsdaafsdfsd", "fdsfsdffsdfsdfdsdsbb", "fdsfsfdfsdfstwsfsdfsdfdsfdsfsd",
"fdfdsfsfsdfsdfsdfsfsfsdsddfsdfsdfsdffdsfds"},
},
Value3: [][32]uint16{
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
30, 31},
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
30, 31},
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
30, 31},
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
30, 31},
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
30, 31},
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
30, 31},
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
30, 31},
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
30, 31},
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
30, 31},
},
Value4: [12]map[int8]float32{
{0: 1., 2: 5., 10: 11., 12: 56, 89: 100, 54: 43, 125: 435},
{0: 1., 2: 5., 10: 11., 12: 56, 89: 100, 54: 43, 125: 435},
{0: 1., 2: 5., 10: 11., 12: 56, 89: 100, 54: 43, 125: 435},
{0: 1., 2: 5., 10: 11., 12: 56, 89: 100, 54: 43, 125: 435},
{0: 1., 2: 5., 10: 11., 12: 56, 89: 100, 54: 43, 125: 435},
{0: 1., 2: 5., 10: 11., 12: 56, 89: 100, 54: 43, 125: 435},
},
Value5: [][3][]map[int16][2]int64{
{
{
{2: {1, 2}, 4: {14321, 553234234232}, 40: {14321, 553234234232}},
{2: {1, 2}, 4: {14321, 553234234232}, 40: {14321, 553234234232}},
{2: {1, 2}, 4: {14321, 553234234232}, 40: {14321, 553234234232}},
{2: {1, 2}, 4: {14321, 553234234232}, 40: {14321, 553234234232}},
{2: {1, 2}, 4: {14321, 553234234232}, 40: {14321, 553234234232}},
},
Value5: [][3][]map[int16][2]int64{
{
{2: {1, 2}, 4: {14321, 553234234232}, 40: {14321, 553234234232}},
{2: {1, 2}, 4: {14321, 553234234232}, 40: {14321, 553234234232}},
{2: {1, 2}, 4: {14321, 553234234232}, 40: {14321, 553234234232}},
{2: {1, 2}, 4: {14321, 553234234232}, 40: {14321, 553234234232}},
{2: {1, 2}, 4: {14321, 553234234232}, 40: {14321, 553234234232}},
},
{
{2: {1, 2}, 4: {14321, 553234234232}, 40: {14321, 553234234232}},
{2: {1, 2}, 4: {14321, 553234234232}, 40: {14321, 553234234232}},
{2: {1, 2}, 4: {14321, 553234234232}, 40: {14321, 553234234232}},
{2: {1, 2}, 4: {14321, 553234234232}, 40: {14321, 553234234232}},
{2: {1, 2}, 4: {14321, 553234234232}, 40: {14321, 553234234232}},
{
{2: {1, 2}, 4: {14321, 553234234232}, 40: {14321, 553234234232}},
{2: {1, 2}, 4: {14321, 553234234232}, 40: {14321, 553234234232}},
{2: {1, 2}, 4: {14321, 553234234232}, 40: {14321, 553234234232}},
{2: {1, 2}, 4: {14321, 553234234232}, 40: {14321, 553234234232}},
{2: {1, 2}, 4: {14321, 553234234232}, 40: {14321, 553234234232}},
},
{
{2: {1, 2}, 4: {14321, 553234234232}, 40: {14321, 553234234232}},
{2: {1, 2}, 4: {14321, 553234234232}, 40: {14321, 553234234232}},
{2: {1, 2}, 4: {14321, 553234234232}, 40: {14321, 553234234232}},
{2: {1, 2}, 4: {14321, 553234234232}, 40: {14321, 553234234232}},
{2: {1, 2}, 4: {14321, 553234234232}, 40: {14321, 553234234232}},
},
{
{2: {1, 2}, 4: {14321, 553234234232}, 40: {14321, 553234234232}},
{2: {1, 2}, 4: {14321, 553234234232}, 40: {14321, 553234234232}},
{2: {1, 2}, 4: {14321, 553234234232}, 40: {14321, 553234234232}},
{2: {1, 2}, 4: {14321, 553234234232}, 40: {14321, 553234234232}},
{2: {1, 2}, 4: {14321, 553234234232}, 40: {14321, 553234234232}},
},
},
},
},
Value6: true,
Value7: true,
Value8: "fdfsd",
Value6: true,
Value7: true,
Value8: "fdfsd",
}
msgBytes = pkg1.MsgSliceUint8{
Value: []uint8{
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
},
}
)

var _ iface = implValue{}
var _ iface = &implPointer{}

type iface interface {
Method(arg string) string
}

func BenchmarkMarshaling(b *testing.B) {
b.StopTimer()
b.ResetTimer()
type implValue struct {
}

var msg2 pkg1.MsgMixed
func (i implValue) Method(arg string) string {
return arg
}

for range b.N {
buf := make([]byte, msg.Size())
type implPointer struct {
}

b.StartTimer()
for range 10000 {
msg.Marshal(buf)
msg2.Unmarshal(buf)
}
b.StopTimer()
}
func (i *implPointer) Method(arg string) string {
return arg
}

func method(arg string) string {
return arg
}

var methodVar = func(arg string) string {
return arg
}

var str = ""
21 changes: 11 additions & 10 deletions types/tsliceuint8/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,21 +81,22 @@ func (b Builder) UnmarshalCodeTemplate(_ *uint64) string {
helpers.Execute(buf, types.UInt64Unmarshal("uint64"), "l")
code += buf.String() + "\n"

code += fmt.Sprintf(`{{ . }} = make(%[1]s, l)
`, b.tm.TypeName(b.msgType, b.fieldType))

unsafe := b.tm.Import("unsafe")
code += "if l > 0 {\n "
code += "if l > 0 {\n"

if b.fieldType.Elem().Name() == "uint8" {
code += fmt.Sprintf(`copy(%[1]s.Slice(&{{ . }}[0], l), b[o:o+l])
o += l`, unsafe)
code += ` {{ . }} = b[o:o+l]`
} else {
code += fmt.Sprintf(`copy(%[1]s.Slice((*byte)(&{{ . }}[0]), l), b[o:o+l])
o += l`, unsafe)
unsafe := b.tm.Import("unsafe")
code += fmt.Sprintf(` {{ . }} = make(%[1]s, l)
`, b.tm.TypeName(b.msgType, b.fieldType))
code += fmt.Sprintf(` copy(%[1]s.Slice((*byte)(&{{ . }}[0]), l), b[o:o+l])`, unsafe)
}

code += "\n}"
code += `
o += l
} else {
{{ . }} = nil
}`

return code
}
Loading

0 comments on commit 905e939

Please sign in to comment.