From d6d824b69d327bbcdde4aa6cca8528e3ba2d92aa Mon Sep 17 00:00:00 2001 From: "qiheng.zhou" Date: Wed, 29 May 2024 11:29:35 +0800 Subject: [PATCH] perf: use span cache for read --- benchmark/fastpb_gen/testcase.pb.fast.go | 212 ++++++++++--------- benchmark/fastpb_gen/testcase.pb.go | 9 +- benchmark/gen_code.sh | 2 +- benchmark/gogo_gen/testcase.pb.go | 183 ++++++++++------ examples/fastpb_gen/base.pb.fast.go | 1 + examples/fastpb_gen/base.pb.go | 5 +- examples/fastpb_gen/echo.pb.fast.go | 1 + examples/fastpb_gen/echo.pb.go | 5 +- examples/fastpb_gen/nested/nested.pb.fast.go | 1 + examples/fastpb_gen/nested/nested.pb.go | 5 +- examples/fastpb_gen/pet/pet.pb.fast.go | 1 + examples/fastpb_gen/pet/pet.pb.go | 5 +- examples/fastpb_gen/user/user.pb.fast.go | 1 + examples/fastpb_gen/user/user.pb.go | 5 +- fastpb_impl.go | 13 +- go.mod | 5 +- go.sum | 43 ++-- mem/span.go | 110 ++++++++++ util.go | 9 + 19 files changed, 396 insertions(+), 220 deletions(-) create mode 100644 mem/span.go create mode 100644 util.go diff --git a/benchmark/fastpb_gen/testcase.pb.fast.go b/benchmark/fastpb_gen/testcase.pb.fast.go index 42afd2e..8ed5f67 100644 --- a/benchmark/fastpb_gen/testcase.pb.fast.go +++ b/benchmark/fastpb_gen/testcase.pb.fast.go @@ -1,12 +1,18 @@ -// Code generated by FastPB v0.0.1. DO NOT EDIT. +// Code generated by Fastpb v0.0.2. DO NOT EDIT. package testcase import ( fmt "fmt" + fastpb "github.com/cloudwego/fastpb" ) +var ( + _ = fmt.Errorf + _ = fastpb.Skip +) + func (x *Number) FastRead(buf []byte, _type int8, number int32) (offset int, err error) { switch number { case 1: @@ -680,7 +686,7 @@ func (x *Number) fastWriteField1(buf []byte) (offset int) { if x.Field1 == 0 { return offset } - offset += fastpb.WriteInt32(buf[offset:], 1, x.Field1) + offset += fastpb.WriteInt32(buf[offset:], 1, x.GetField1()) return offset } @@ -688,7 +694,7 @@ func (x *Number) fastWriteField2(buf []byte) (offset int) { if x.Field2 == 0 { return offset } - offset += fastpb.WriteInt64(buf[offset:], 2, x.Field2) + offset += fastpb.WriteInt64(buf[offset:], 2, x.GetField2()) return offset } @@ -696,7 +702,7 @@ func (x *Number) fastWriteField3(buf []byte) (offset int) { if x.Field3 == 0 { return offset } - offset += fastpb.WriteUint32(buf[offset:], 3, x.Field3) + offset += fastpb.WriteUint32(buf[offset:], 3, x.GetField3()) return offset } @@ -704,7 +710,7 @@ func (x *Number) fastWriteField4(buf []byte) (offset int) { if x.Field4 == 0 { return offset } - offset += fastpb.WriteUint64(buf[offset:], 4, x.Field4) + offset += fastpb.WriteUint64(buf[offset:], 4, x.GetField4()) return offset } @@ -712,7 +718,7 @@ func (x *Number) fastWriteField5(buf []byte) (offset int) { if x.Field5 == 0 { return offset } - offset += fastpb.WriteSint32(buf[offset:], 5, x.Field5) + offset += fastpb.WriteSint32(buf[offset:], 5, x.GetField5()) return offset } @@ -720,7 +726,7 @@ func (x *Number) fastWriteField6(buf []byte) (offset int) { if x.Field6 == 0 { return offset } - offset += fastpb.WriteSint64(buf[offset:], 6, x.Field6) + offset += fastpb.WriteSint64(buf[offset:], 6, x.GetField6()) return offset } @@ -728,7 +734,7 @@ func (x *Number) fastWriteField7(buf []byte) (offset int) { if !x.Field7 { return offset } - offset += fastpb.WriteBool(buf[offset:], 7, x.Field7) + offset += fastpb.WriteBool(buf[offset:], 7, x.GetField7()) return offset } @@ -736,7 +742,7 @@ func (x *Number) fastWriteField8(buf []byte) (offset int) { if x.Field8 == 0 { return offset } - offset += fastpb.WriteFloat(buf[offset:], 8, x.Field8) + offset += fastpb.WriteFloat(buf[offset:], 8, x.GetField8()) return offset } @@ -744,7 +750,7 @@ func (x *Number) fastWriteField9(buf []byte) (offset int) { if x.Field9 == 0 { return offset } - offset += fastpb.WriteDouble(buf[offset:], 9, x.Field9) + offset += fastpb.WriteDouble(buf[offset:], 9, x.GetField9()) return offset } @@ -752,7 +758,7 @@ func (x *Number) fastWriteField10(buf []byte) (offset int) { if x.Field10 == 0 { return offset } - offset += fastpb.WriteFixed32(buf[offset:], 10, x.Field10) + offset += fastpb.WriteFixed32(buf[offset:], 10, x.GetField10()) return offset } @@ -760,7 +766,7 @@ func (x *Number) fastWriteField11(buf []byte) (offset int) { if x.Field11 == 0 { return offset } - offset += fastpb.WriteFixed64(buf[offset:], 11, x.Field11) + offset += fastpb.WriteFixed64(buf[offset:], 11, x.GetField11()) return offset } @@ -768,7 +774,7 @@ func (x *Number) fastWriteField12(buf []byte) (offset int) { if x.Field12 == 0 { return offset } - offset += fastpb.WriteSfixed32(buf[offset:], 12, x.Field12) + offset += fastpb.WriteSfixed32(buf[offset:], 12, x.GetField12()) return offset } @@ -776,7 +782,7 @@ func (x *Number) fastWriteField13(buf []byte) (offset int) { if x.Field13 == 0 { return offset } - offset += fastpb.WriteSfixed64(buf[offset:], 13, x.Field13) + offset += fastpb.WriteSfixed64(buf[offset:], 13, x.GetField13()) return offset } @@ -793,7 +799,7 @@ func (x *String) fastWriteField1(buf []byte) (offset int) { if x.Field1 == "" { return offset } - offset += fastpb.WriteString(buf[offset:], 1, x.Field1) + offset += fastpb.WriteString(buf[offset:], 1, x.GetField1()) return offset } @@ -801,7 +807,7 @@ func (x *String) fastWriteField2(buf []byte) (offset int) { if len(x.Field2) == 0 { return offset } - offset += fastpb.WriteBytes(buf[offset:], 2, x.Field2) + offset += fastpb.WriteBytes(buf[offset:], 2, x.GetField2()) return offset } @@ -831,10 +837,10 @@ func (x *List) fastWriteField1(buf []byte) (offset int) { if len(x.Field1) == 0 { return offset } - offset += fastpb.WriteListPacked(buf[offset:], 1, len(x.Field1), + offset += fastpb.WriteListPacked(buf[offset:], 1, len(x.GetField1()), func(buf []byte, numTagOrKey, numIdxOrVal int32) int { offset := 0 - offset += fastpb.WriteInt32(buf[offset:], numTagOrKey, x.Field1[numIdxOrVal]) + offset += fastpb.WriteInt32(buf[offset:], numTagOrKey, x.GetField1()[numIdxOrVal]) return offset }) return offset @@ -844,10 +850,10 @@ func (x *List) fastWriteField2(buf []byte) (offset int) { if len(x.Field2) == 0 { return offset } - offset += fastpb.WriteListPacked(buf[offset:], 2, len(x.Field2), + offset += fastpb.WriteListPacked(buf[offset:], 2, len(x.GetField2()), func(buf []byte, numTagOrKey, numIdxOrVal int32) int { offset := 0 - offset += fastpb.WriteInt64(buf[offset:], numTagOrKey, x.Field2[numIdxOrVal]) + offset += fastpb.WriteInt64(buf[offset:], numTagOrKey, x.GetField2()[numIdxOrVal]) return offset }) return offset @@ -857,10 +863,10 @@ func (x *List) fastWriteField3(buf []byte) (offset int) { if len(x.Field3) == 0 { return offset } - offset += fastpb.WriteListPacked(buf[offset:], 3, len(x.Field3), + offset += fastpb.WriteListPacked(buf[offset:], 3, len(x.GetField3()), func(buf []byte, numTagOrKey, numIdxOrVal int32) int { offset := 0 - offset += fastpb.WriteUint32(buf[offset:], numTagOrKey, x.Field3[numIdxOrVal]) + offset += fastpb.WriteUint32(buf[offset:], numTagOrKey, x.GetField3()[numIdxOrVal]) return offset }) return offset @@ -870,10 +876,10 @@ func (x *List) fastWriteField4(buf []byte) (offset int) { if len(x.Field4) == 0 { return offset } - offset += fastpb.WriteListPacked(buf[offset:], 4, len(x.Field4), + offset += fastpb.WriteListPacked(buf[offset:], 4, len(x.GetField4()), func(buf []byte, numTagOrKey, numIdxOrVal int32) int { offset := 0 - offset += fastpb.WriteUint64(buf[offset:], numTagOrKey, x.Field4[numIdxOrVal]) + offset += fastpb.WriteUint64(buf[offset:], numTagOrKey, x.GetField4()[numIdxOrVal]) return offset }) return offset @@ -883,10 +889,10 @@ func (x *List) fastWriteField5(buf []byte) (offset int) { if len(x.Field5) == 0 { return offset } - offset += fastpb.WriteListPacked(buf[offset:], 5, len(x.Field5), + offset += fastpb.WriteListPacked(buf[offset:], 5, len(x.GetField5()), func(buf []byte, numTagOrKey, numIdxOrVal int32) int { offset := 0 - offset += fastpb.WriteSint32(buf[offset:], numTagOrKey, x.Field5[numIdxOrVal]) + offset += fastpb.WriteSint32(buf[offset:], numTagOrKey, x.GetField5()[numIdxOrVal]) return offset }) return offset @@ -896,10 +902,10 @@ func (x *List) fastWriteField6(buf []byte) (offset int) { if len(x.Field6) == 0 { return offset } - offset += fastpb.WriteListPacked(buf[offset:], 6, len(x.Field6), + offset += fastpb.WriteListPacked(buf[offset:], 6, len(x.GetField6()), func(buf []byte, numTagOrKey, numIdxOrVal int32) int { offset := 0 - offset += fastpb.WriteSint64(buf[offset:], numTagOrKey, x.Field6[numIdxOrVal]) + offset += fastpb.WriteSint64(buf[offset:], numTagOrKey, x.GetField6()[numIdxOrVal]) return offset }) return offset @@ -909,10 +915,10 @@ func (x *List) fastWriteField7(buf []byte) (offset int) { if len(x.Field7) == 0 { return offset } - offset += fastpb.WriteListPacked(buf[offset:], 7, len(x.Field7), + offset += fastpb.WriteListPacked(buf[offset:], 7, len(x.GetField7()), func(buf []byte, numTagOrKey, numIdxOrVal int32) int { offset := 0 - offset += fastpb.WriteBool(buf[offset:], numTagOrKey, x.Field7[numIdxOrVal]) + offset += fastpb.WriteBool(buf[offset:], numTagOrKey, x.GetField7()[numIdxOrVal]) return offset }) return offset @@ -922,10 +928,10 @@ func (x *List) fastWriteField8(buf []byte) (offset int) { if len(x.Field8) == 0 { return offset } - offset += fastpb.WriteListPacked(buf[offset:], 8, len(x.Field8), + offset += fastpb.WriteListPacked(buf[offset:], 8, len(x.GetField8()), func(buf []byte, numTagOrKey, numIdxOrVal int32) int { offset := 0 - offset += fastpb.WriteFloat(buf[offset:], numTagOrKey, x.Field8[numIdxOrVal]) + offset += fastpb.WriteFloat(buf[offset:], numTagOrKey, x.GetField8()[numIdxOrVal]) return offset }) return offset @@ -935,10 +941,10 @@ func (x *List) fastWriteField9(buf []byte) (offset int) { if len(x.Field9) == 0 { return offset } - offset += fastpb.WriteListPacked(buf[offset:], 9, len(x.Field9), + offset += fastpb.WriteListPacked(buf[offset:], 9, len(x.GetField9()), func(buf []byte, numTagOrKey, numIdxOrVal int32) int { offset := 0 - offset += fastpb.WriteDouble(buf[offset:], numTagOrKey, x.Field9[numIdxOrVal]) + offset += fastpb.WriteDouble(buf[offset:], numTagOrKey, x.GetField9()[numIdxOrVal]) return offset }) return offset @@ -948,10 +954,10 @@ func (x *List) fastWriteField10(buf []byte) (offset int) { if len(x.Field10) == 0 { return offset } - offset += fastpb.WriteListPacked(buf[offset:], 10, len(x.Field10), + offset += fastpb.WriteListPacked(buf[offset:], 10, len(x.GetField10()), func(buf []byte, numTagOrKey, numIdxOrVal int32) int { offset := 0 - offset += fastpb.WriteFixed32(buf[offset:], numTagOrKey, x.Field10[numIdxOrVal]) + offset += fastpb.WriteFixed32(buf[offset:], numTagOrKey, x.GetField10()[numIdxOrVal]) return offset }) return offset @@ -961,10 +967,10 @@ func (x *List) fastWriteField11(buf []byte) (offset int) { if len(x.Field11) == 0 { return offset } - offset += fastpb.WriteListPacked(buf[offset:], 11, len(x.Field11), + offset += fastpb.WriteListPacked(buf[offset:], 11, len(x.GetField11()), func(buf []byte, numTagOrKey, numIdxOrVal int32) int { offset := 0 - offset += fastpb.WriteFixed64(buf[offset:], numTagOrKey, x.Field11[numIdxOrVal]) + offset += fastpb.WriteFixed64(buf[offset:], numTagOrKey, x.GetField11()[numIdxOrVal]) return offset }) return offset @@ -974,10 +980,10 @@ func (x *List) fastWriteField12(buf []byte) (offset int) { if len(x.Field12) == 0 { return offset } - offset += fastpb.WriteListPacked(buf[offset:], 12, len(x.Field12), + offset += fastpb.WriteListPacked(buf[offset:], 12, len(x.GetField12()), func(buf []byte, numTagOrKey, numIdxOrVal int32) int { offset := 0 - offset += fastpb.WriteSfixed32(buf[offset:], numTagOrKey, x.Field12[numIdxOrVal]) + offset += fastpb.WriteSfixed32(buf[offset:], numTagOrKey, x.GetField12()[numIdxOrVal]) return offset }) return offset @@ -987,10 +993,10 @@ func (x *List) fastWriteField13(buf []byte) (offset int) { if len(x.Field13) == 0 { return offset } - offset += fastpb.WriteListPacked(buf[offset:], 13, len(x.Field13), + offset += fastpb.WriteListPacked(buf[offset:], 13, len(x.GetField13()), func(buf []byte, numTagOrKey, numIdxOrVal int32) int { offset := 0 - offset += fastpb.WriteSfixed64(buf[offset:], numTagOrKey, x.Field13[numIdxOrVal]) + offset += fastpb.WriteSfixed64(buf[offset:], numTagOrKey, x.GetField13()[numIdxOrVal]) return offset }) return offset @@ -1000,8 +1006,8 @@ func (x *List) fastWriteField14(buf []byte) (offset int) { if len(x.Field14) == 0 { return offset } - for i := range x.Field14 { - offset += fastpb.WriteString(buf[offset:], 14, x.Field14[i]) + for i := range x.GetField14() { + offset += fastpb.WriteString(buf[offset:], 14, x.GetField14()[i]) } return offset } @@ -1010,8 +1016,8 @@ func (x *List) fastWriteField15(buf []byte) (offset int) { if len(x.Field15) == 0 { return offset } - for i := range x.Field15 { - offset += fastpb.WriteBytes(buf[offset:], 15, x.Field15[i]) + for i := range x.GetField15() { + offset += fastpb.WriteBytes(buf[offset:], 15, x.GetField15()[i]) } return offset } @@ -1033,7 +1039,7 @@ func (x *Map) fastWriteField1(buf []byte) (offset int) { if x.Field1 == nil { return offset } - for k, v := range x.Field1 { + for k, v := range x.GetField1() { offset += fastpb.WriteMapEntry(buf[offset:], 1, func(buf []byte, numTagOrKey, numIdxOrVal int32) int { offset := 0 @@ -1049,7 +1055,7 @@ func (x *Map) fastWriteField2(buf []byte) (offset int) { if x.Field2 == nil { return offset } - for k, v := range x.Field2 { + for k, v := range x.GetField2() { offset += fastpb.WriteMapEntry(buf[offset:], 2, func(buf []byte, numTagOrKey, numIdxOrVal int32) int { offset := 0 @@ -1065,7 +1071,7 @@ func (x *Map) fastWriteField3(buf []byte) (offset int) { if x.Field3 == nil { return offset } - for k, v := range x.Field3 { + for k, v := range x.GetField3() { offset += fastpb.WriteMapEntry(buf[offset:], 3, func(buf []byte, numTagOrKey, numIdxOrVal int32) int { offset := 0 @@ -1081,7 +1087,7 @@ func (x *Map) fastWriteField4(buf []byte) (offset int) { if x.Field4 == nil { return offset } - for k, v := range x.Field4 { + for k, v := range x.GetField4() { offset += fastpb.WriteMapEntry(buf[offset:], 4, func(buf []byte, numTagOrKey, numIdxOrVal int32) int { offset := 0 @@ -1097,7 +1103,7 @@ func (x *Map) fastWriteField5(buf []byte) (offset int) { if x.Field5 == nil { return offset } - for k, v := range x.Field5 { + for k, v := range x.GetField5() { offset += fastpb.WriteMapEntry(buf[offset:], 5, func(buf []byte, numTagOrKey, numIdxOrVal int32) int { offset := 0 @@ -1113,7 +1119,7 @@ func (x *Map) fastWriteField6(buf []byte) (offset int) { if x.Field6 == nil { return offset } - for k, v := range x.Field6 { + for k, v := range x.GetField6() { offset += fastpb.WriteMapEntry(buf[offset:], 6, func(buf []byte, numTagOrKey, numIdxOrVal int32) int { offset := 0 @@ -1149,7 +1155,7 @@ func (x *Number) sizeField1() (n int) { if x.Field1 == 0 { return n } - n += fastpb.SizeInt32(1, x.Field1) + n += fastpb.SizeInt32(1, x.GetField1()) return n } @@ -1157,7 +1163,7 @@ func (x *Number) sizeField2() (n int) { if x.Field2 == 0 { return n } - n += fastpb.SizeInt64(2, x.Field2) + n += fastpb.SizeInt64(2, x.GetField2()) return n } @@ -1165,7 +1171,7 @@ func (x *Number) sizeField3() (n int) { if x.Field3 == 0 { return n } - n += fastpb.SizeUint32(3, x.Field3) + n += fastpb.SizeUint32(3, x.GetField3()) return n } @@ -1173,7 +1179,7 @@ func (x *Number) sizeField4() (n int) { if x.Field4 == 0 { return n } - n += fastpb.SizeUint64(4, x.Field4) + n += fastpb.SizeUint64(4, x.GetField4()) return n } @@ -1181,7 +1187,7 @@ func (x *Number) sizeField5() (n int) { if x.Field5 == 0 { return n } - n += fastpb.SizeSint32(5, x.Field5) + n += fastpb.SizeSint32(5, x.GetField5()) return n } @@ -1189,7 +1195,7 @@ func (x *Number) sizeField6() (n int) { if x.Field6 == 0 { return n } - n += fastpb.SizeSint64(6, x.Field6) + n += fastpb.SizeSint64(6, x.GetField6()) return n } @@ -1197,7 +1203,7 @@ func (x *Number) sizeField7() (n int) { if !x.Field7 { return n } - n += fastpb.SizeBool(7, x.Field7) + n += fastpb.SizeBool(7, x.GetField7()) return n } @@ -1205,7 +1211,7 @@ func (x *Number) sizeField8() (n int) { if x.Field8 == 0 { return n } - n += fastpb.SizeFloat(8, x.Field8) + n += fastpb.SizeFloat(8, x.GetField8()) return n } @@ -1213,7 +1219,7 @@ func (x *Number) sizeField9() (n int) { if x.Field9 == 0 { return n } - n += fastpb.SizeDouble(9, x.Field9) + n += fastpb.SizeDouble(9, x.GetField9()) return n } @@ -1221,7 +1227,7 @@ func (x *Number) sizeField10() (n int) { if x.Field10 == 0 { return n } - n += fastpb.SizeFixed32(10, x.Field10) + n += fastpb.SizeFixed32(10, x.GetField10()) return n } @@ -1229,7 +1235,7 @@ func (x *Number) sizeField11() (n int) { if x.Field11 == 0 { return n } - n += fastpb.SizeFixed64(11, x.Field11) + n += fastpb.SizeFixed64(11, x.GetField11()) return n } @@ -1237,7 +1243,7 @@ func (x *Number) sizeField12() (n int) { if x.Field12 == 0 { return n } - n += fastpb.SizeSfixed32(12, x.Field12) + n += fastpb.SizeSfixed32(12, x.GetField12()) return n } @@ -1245,7 +1251,7 @@ func (x *Number) sizeField13() (n int) { if x.Field13 == 0 { return n } - n += fastpb.SizeSfixed64(13, x.Field13) + n += fastpb.SizeSfixed64(13, x.GetField13()) return n } @@ -1262,7 +1268,7 @@ func (x *String) sizeField1() (n int) { if x.Field1 == "" { return n } - n += fastpb.SizeString(1, x.Field1) + n += fastpb.SizeString(1, x.GetField1()) return n } @@ -1270,7 +1276,7 @@ func (x *String) sizeField2() (n int) { if len(x.Field2) == 0 { return n } - n += fastpb.SizeBytes(2, x.Field2) + n += fastpb.SizeBytes(2, x.GetField2()) return n } @@ -1300,10 +1306,10 @@ func (x *List) sizeField1() (n int) { if len(x.Field1) == 0 { return n } - n += fastpb.SizeListPacked(1, len(x.Field1), + n += fastpb.SizeListPacked(1, len(x.GetField1()), func(numTagOrKey, numIdxOrVal int32) int { n := 0 - n += fastpb.SizeInt32(numTagOrKey, x.Field1[numIdxOrVal]) + n += fastpb.SizeInt32(numTagOrKey, x.GetField1()[numIdxOrVal]) return n }) return n @@ -1313,10 +1319,10 @@ func (x *List) sizeField2() (n int) { if len(x.Field2) == 0 { return n } - n += fastpb.SizeListPacked(2, len(x.Field2), + n += fastpb.SizeListPacked(2, len(x.GetField2()), func(numTagOrKey, numIdxOrVal int32) int { n := 0 - n += fastpb.SizeInt64(numTagOrKey, x.Field2[numIdxOrVal]) + n += fastpb.SizeInt64(numTagOrKey, x.GetField2()[numIdxOrVal]) return n }) return n @@ -1326,10 +1332,10 @@ func (x *List) sizeField3() (n int) { if len(x.Field3) == 0 { return n } - n += fastpb.SizeListPacked(3, len(x.Field3), + n += fastpb.SizeListPacked(3, len(x.GetField3()), func(numTagOrKey, numIdxOrVal int32) int { n := 0 - n += fastpb.SizeUint32(numTagOrKey, x.Field3[numIdxOrVal]) + n += fastpb.SizeUint32(numTagOrKey, x.GetField3()[numIdxOrVal]) return n }) return n @@ -1339,10 +1345,10 @@ func (x *List) sizeField4() (n int) { if len(x.Field4) == 0 { return n } - n += fastpb.SizeListPacked(4, len(x.Field4), + n += fastpb.SizeListPacked(4, len(x.GetField4()), func(numTagOrKey, numIdxOrVal int32) int { n := 0 - n += fastpb.SizeUint64(numTagOrKey, x.Field4[numIdxOrVal]) + n += fastpb.SizeUint64(numTagOrKey, x.GetField4()[numIdxOrVal]) return n }) return n @@ -1352,10 +1358,10 @@ func (x *List) sizeField5() (n int) { if len(x.Field5) == 0 { return n } - n += fastpb.SizeListPacked(5, len(x.Field5), + n += fastpb.SizeListPacked(5, len(x.GetField5()), func(numTagOrKey, numIdxOrVal int32) int { n := 0 - n += fastpb.SizeSint32(numTagOrKey, x.Field5[numIdxOrVal]) + n += fastpb.SizeSint32(numTagOrKey, x.GetField5()[numIdxOrVal]) return n }) return n @@ -1365,10 +1371,10 @@ func (x *List) sizeField6() (n int) { if len(x.Field6) == 0 { return n } - n += fastpb.SizeListPacked(6, len(x.Field6), + n += fastpb.SizeListPacked(6, len(x.GetField6()), func(numTagOrKey, numIdxOrVal int32) int { n := 0 - n += fastpb.SizeSint64(numTagOrKey, x.Field6[numIdxOrVal]) + n += fastpb.SizeSint64(numTagOrKey, x.GetField6()[numIdxOrVal]) return n }) return n @@ -1378,10 +1384,10 @@ func (x *List) sizeField7() (n int) { if len(x.Field7) == 0 { return n } - n += fastpb.SizeListPacked(7, len(x.Field7), + n += fastpb.SizeListPacked(7, len(x.GetField7()), func(numTagOrKey, numIdxOrVal int32) int { n := 0 - n += fastpb.SizeBool(numTagOrKey, x.Field7[numIdxOrVal]) + n += fastpb.SizeBool(numTagOrKey, x.GetField7()[numIdxOrVal]) return n }) return n @@ -1391,10 +1397,10 @@ func (x *List) sizeField8() (n int) { if len(x.Field8) == 0 { return n } - n += fastpb.SizeListPacked(8, len(x.Field8), + n += fastpb.SizeListPacked(8, len(x.GetField8()), func(numTagOrKey, numIdxOrVal int32) int { n := 0 - n += fastpb.SizeFloat(numTagOrKey, x.Field8[numIdxOrVal]) + n += fastpb.SizeFloat(numTagOrKey, x.GetField8()[numIdxOrVal]) return n }) return n @@ -1404,10 +1410,10 @@ func (x *List) sizeField9() (n int) { if len(x.Field9) == 0 { return n } - n += fastpb.SizeListPacked(9, len(x.Field9), + n += fastpb.SizeListPacked(9, len(x.GetField9()), func(numTagOrKey, numIdxOrVal int32) int { n := 0 - n += fastpb.SizeDouble(numTagOrKey, x.Field9[numIdxOrVal]) + n += fastpb.SizeDouble(numTagOrKey, x.GetField9()[numIdxOrVal]) return n }) return n @@ -1417,10 +1423,10 @@ func (x *List) sizeField10() (n int) { if len(x.Field10) == 0 { return n } - n += fastpb.SizeListPacked(10, len(x.Field10), + n += fastpb.SizeListPacked(10, len(x.GetField10()), func(numTagOrKey, numIdxOrVal int32) int { n := 0 - n += fastpb.SizeFixed32(numTagOrKey, x.Field10[numIdxOrVal]) + n += fastpb.SizeFixed32(numTagOrKey, x.GetField10()[numIdxOrVal]) return n }) return n @@ -1430,10 +1436,10 @@ func (x *List) sizeField11() (n int) { if len(x.Field11) == 0 { return n } - n += fastpb.SizeListPacked(11, len(x.Field11), + n += fastpb.SizeListPacked(11, len(x.GetField11()), func(numTagOrKey, numIdxOrVal int32) int { n := 0 - n += fastpb.SizeFixed64(numTagOrKey, x.Field11[numIdxOrVal]) + n += fastpb.SizeFixed64(numTagOrKey, x.GetField11()[numIdxOrVal]) return n }) return n @@ -1443,10 +1449,10 @@ func (x *List) sizeField12() (n int) { if len(x.Field12) == 0 { return n } - n += fastpb.SizeListPacked(12, len(x.Field12), + n += fastpb.SizeListPacked(12, len(x.GetField12()), func(numTagOrKey, numIdxOrVal int32) int { n := 0 - n += fastpb.SizeSfixed32(numTagOrKey, x.Field12[numIdxOrVal]) + n += fastpb.SizeSfixed32(numTagOrKey, x.GetField12()[numIdxOrVal]) return n }) return n @@ -1456,10 +1462,10 @@ func (x *List) sizeField13() (n int) { if len(x.Field13) == 0 { return n } - n += fastpb.SizeListPacked(13, len(x.Field13), + n += fastpb.SizeListPacked(13, len(x.GetField13()), func(numTagOrKey, numIdxOrVal int32) int { n := 0 - n += fastpb.SizeSfixed64(numTagOrKey, x.Field13[numIdxOrVal]) + n += fastpb.SizeSfixed64(numTagOrKey, x.GetField13()[numIdxOrVal]) return n }) return n @@ -1469,8 +1475,8 @@ func (x *List) sizeField14() (n int) { if len(x.Field14) == 0 { return n } - for i := range x.Field14 { - n += fastpb.SizeString(14, x.Field14[i]) + for i := range x.GetField14() { + n += fastpb.SizeString(14, x.GetField14()[i]) } return n } @@ -1479,8 +1485,8 @@ func (x *List) sizeField15() (n int) { if len(x.Field15) == 0 { return n } - for i := range x.Field15 { - n += fastpb.SizeBytes(15, x.Field15[i]) + for i := range x.GetField15() { + n += fastpb.SizeBytes(15, x.GetField15()[i]) } return n } @@ -1502,7 +1508,7 @@ func (x *Map) sizeField1() (n int) { if x.Field1 == nil { return n } - for k, v := range x.Field1 { + for k, v := range x.GetField1() { n += fastpb.SizeMapEntry(1, func(numTagOrKey, numIdxOrVal int32) int { n := 0 @@ -1518,7 +1524,7 @@ func (x *Map) sizeField2() (n int) { if x.Field2 == nil { return n } - for k, v := range x.Field2 { + for k, v := range x.GetField2() { n += fastpb.SizeMapEntry(2, func(numTagOrKey, numIdxOrVal int32) int { n := 0 @@ -1534,7 +1540,7 @@ func (x *Map) sizeField3() (n int) { if x.Field3 == nil { return n } - for k, v := range x.Field3 { + for k, v := range x.GetField3() { n += fastpb.SizeMapEntry(3, func(numTagOrKey, numIdxOrVal int32) int { n := 0 @@ -1550,7 +1556,7 @@ func (x *Map) sizeField4() (n int) { if x.Field4 == nil { return n } - for k, v := range x.Field4 { + for k, v := range x.GetField4() { n += fastpb.SizeMapEntry(4, func(numTagOrKey, numIdxOrVal int32) int { n := 0 @@ -1566,7 +1572,7 @@ func (x *Map) sizeField5() (n int) { if x.Field5 == nil { return n } - for k, v := range x.Field5 { + for k, v := range x.GetField5() { n += fastpb.SizeMapEntry(5, func(numTagOrKey, numIdxOrVal int32) int { n := 0 @@ -1582,7 +1588,7 @@ func (x *Map) sizeField6() (n int) { if x.Field6 == nil { return n } - for k, v := range x.Field6 { + for k, v := range x.GetField6() { n += fastpb.SizeMapEntry(6, func(numTagOrKey, numIdxOrVal int32) int { n := 0 diff --git a/benchmark/fastpb_gen/testcase.pb.go b/benchmark/fastpb_gen/testcase.pb.go index baabe71..6e3e968 100644 --- a/benchmark/fastpb_gen/testcase.pb.go +++ b/benchmark/fastpb_gen/testcase.pb.go @@ -1,16 +1,17 @@ // Code generated by protoc-gen-go. DO NOT EDIT. // versions: -// protoc-gen-go v1.26.0 -// protoc v3.21.2 +// protoc-gen-go v1.34.1 +// protoc v5.26.1 // source: testcase.proto package testcase import ( - protoreflect "google.golang.org/protobuf/reflect/protoreflect" - protoimpl "google.golang.org/protobuf/runtime/protoimpl" reflect "reflect" sync "sync" + + protoreflect "google.golang.org/protobuf/reflect/protoreflect" + protoimpl "google.golang.org/protobuf/runtime/protoimpl" ) const ( diff --git a/benchmark/gen_code.sh b/benchmark/gen_code.sh index 67966b8..b3f6c4e 100755 --- a/benchmark/gen_code.sh +++ b/benchmark/gen_code.sh @@ -18,4 +18,4 @@ protoc --go_opt=paths=source_relative --go_out=./fastpb_gen --fastpb_opt=paths=s # gogo rm -rf ./gogo_gen && mkdir ./gogo_gen -protoc --gogofaster_opt=paths=source_relative --gogofaster_out=./gogo_gen -I ./idl/ ./idl/testcase.proto \ No newline at end of file +protoc --gofast_opt=paths=source_relative --gofast_out=./gogo_gen -I ./idl/ ./idl/testcase.proto \ No newline at end of file diff --git a/benchmark/gogo_gen/testcase.pb.go b/benchmark/gogo_gen/testcase.pb.go index 23153e1..3936898 100644 --- a/benchmark/gogo_gen/testcase.pb.go +++ b/benchmark/gogo_gen/testcase.pb.go @@ -6,10 +6,11 @@ package testcase import ( encoding_binary "encoding/binary" fmt "fmt" - proto "github.com/gogo/protobuf/proto" io "io" math "math" math_bits "math/bits" + + proto "github.com/golang/protobuf/proto" ) // Reference imports to suppress errors if they are not otherwise used. @@ -21,22 +22,25 @@ var _ = math.Inf // is compatible with the proto package it is being compiled against. // A compilation error at this line likely means your copy of the // proto package needs to be updated. -const _ = proto.GoGoProtoPackageIsVersion3 // please upgrade the proto package +const _ = proto.ProtoPackageIsVersion3 // please upgrade the proto package type Number struct { - Field1 int32 `protobuf:"varint,1,opt,name=field1,proto3" json:"field1,omitempty"` - Field2 int64 `protobuf:"varint,2,opt,name=field2,proto3" json:"field2,omitempty"` - Field3 uint32 `protobuf:"varint,3,opt,name=field3,proto3" json:"field3,omitempty"` - Field4 uint64 `protobuf:"varint,4,opt,name=field4,proto3" json:"field4,omitempty"` - Field5 int32 `protobuf:"zigzag32,5,opt,name=field5,proto3" json:"field5,omitempty"` - Field6 int64 `protobuf:"zigzag64,6,opt,name=field6,proto3" json:"field6,omitempty"` - Field7 bool `protobuf:"varint,7,opt,name=field7,proto3" json:"field7,omitempty"` - Field8 float32 `protobuf:"fixed32,8,opt,name=field8,proto3" json:"field8,omitempty"` - Field9 float64 `protobuf:"fixed64,9,opt,name=field9,proto3" json:"field9,omitempty"` - Field10 uint32 `protobuf:"fixed32,10,opt,name=field10,proto3" json:"field10,omitempty"` - Field11 uint64 `protobuf:"fixed64,11,opt,name=field11,proto3" json:"field11,omitempty"` - Field12 int32 `protobuf:"fixed32,12,opt,name=field12,proto3" json:"field12,omitempty"` - Field13 int64 `protobuf:"fixed64,13,opt,name=field13,proto3" json:"field13,omitempty"` + Field1 int32 `protobuf:"varint,1,opt,name=field1,proto3" json:"field1,omitempty"` + Field2 int64 `protobuf:"varint,2,opt,name=field2,proto3" json:"field2,omitempty"` + Field3 uint32 `protobuf:"varint,3,opt,name=field3,proto3" json:"field3,omitempty"` + Field4 uint64 `protobuf:"varint,4,opt,name=field4,proto3" json:"field4,omitempty"` + Field5 int32 `protobuf:"zigzag32,5,opt,name=field5,proto3" json:"field5,omitempty"` + Field6 int64 `protobuf:"zigzag64,6,opt,name=field6,proto3" json:"field6,omitempty"` + Field7 bool `protobuf:"varint,7,opt,name=field7,proto3" json:"field7,omitempty"` + Field8 float32 `protobuf:"fixed32,8,opt,name=field8,proto3" json:"field8,omitempty"` + Field9 float64 `protobuf:"fixed64,9,opt,name=field9,proto3" json:"field9,omitempty"` + Field10 uint32 `protobuf:"fixed32,10,opt,name=field10,proto3" json:"field10,omitempty"` + Field11 uint64 `protobuf:"fixed64,11,opt,name=field11,proto3" json:"field11,omitempty"` + Field12 int32 `protobuf:"fixed32,12,opt,name=field12,proto3" json:"field12,omitempty"` + Field13 int64 `protobuf:"fixed64,13,opt,name=field13,proto3" json:"field13,omitempty"` + XXX_NoUnkeyedLiteral struct{} `json:"-"` + XXX_unrecognized []byte `json:"-"` + XXX_sizecache int32 `json:"-"` } func (m *Number) Reset() { *m = Number{} } @@ -164,8 +168,11 @@ func (m *Number) GetField13() int64 { } type String struct { - Field1 string `protobuf:"bytes,1,opt,name=field1,proto3" json:"field1,omitempty"` - Field2 []byte `protobuf:"bytes,2,opt,name=field2,proto3" json:"field2,omitempty"` + Field1 string `protobuf:"bytes,1,opt,name=field1,proto3" json:"field1,omitempty"` + Field2 []byte `protobuf:"bytes,2,opt,name=field2,proto3" json:"field2,omitempty"` + XXX_NoUnkeyedLiteral struct{} `json:"-"` + XXX_unrecognized []byte `json:"-"` + XXX_sizecache int32 `json:"-"` } func (m *String) Reset() { *m = String{} } @@ -216,21 +223,24 @@ func (m *String) GetField2() []byte { } type List struct { - Field1 []int32 `protobuf:"varint,1,rep,packed,name=field1,proto3" json:"field1,omitempty"` - Field2 []int64 `protobuf:"varint,2,rep,packed,name=field2,proto3" json:"field2,omitempty"` - Field3 []uint32 `protobuf:"varint,3,rep,packed,name=field3,proto3" json:"field3,omitempty"` - Field4 []uint64 `protobuf:"varint,4,rep,packed,name=field4,proto3" json:"field4,omitempty"` - Field5 []int32 `protobuf:"zigzag32,5,rep,packed,name=field5,proto3" json:"field5,omitempty"` - Field6 []int64 `protobuf:"zigzag64,6,rep,packed,name=field6,proto3" json:"field6,omitempty"` - Field7 []bool `protobuf:"varint,7,rep,packed,name=field7,proto3" json:"field7,omitempty"` - Field8 []float32 `protobuf:"fixed32,8,rep,packed,name=field8,proto3" json:"field8,omitempty"` - Field9 []float64 `protobuf:"fixed64,9,rep,packed,name=field9,proto3" json:"field9,omitempty"` - Field10 []uint32 `protobuf:"fixed32,10,rep,packed,name=field10,proto3" json:"field10,omitempty"` - Field11 []uint64 `protobuf:"fixed64,11,rep,packed,name=field11,proto3" json:"field11,omitempty"` - Field12 []int32 `protobuf:"fixed32,12,rep,packed,name=field12,proto3" json:"field12,omitempty"` - Field13 []int64 `protobuf:"fixed64,13,rep,packed,name=field13,proto3" json:"field13,omitempty"` - Field14 []string `protobuf:"bytes,14,rep,name=field14,proto3" json:"field14,omitempty"` - Field15 [][]byte `protobuf:"bytes,15,rep,name=field15,proto3" json:"field15,omitempty"` + Field1 []int32 `protobuf:"varint,1,rep,packed,name=field1,proto3" json:"field1,omitempty"` + Field2 []int64 `protobuf:"varint,2,rep,packed,name=field2,proto3" json:"field2,omitempty"` + Field3 []uint32 `protobuf:"varint,3,rep,packed,name=field3,proto3" json:"field3,omitempty"` + Field4 []uint64 `protobuf:"varint,4,rep,packed,name=field4,proto3" json:"field4,omitempty"` + Field5 []int32 `protobuf:"zigzag32,5,rep,packed,name=field5,proto3" json:"field5,omitempty"` + Field6 []int64 `protobuf:"zigzag64,6,rep,packed,name=field6,proto3" json:"field6,omitempty"` + Field7 []bool `protobuf:"varint,7,rep,packed,name=field7,proto3" json:"field7,omitempty"` + Field8 []float32 `protobuf:"fixed32,8,rep,packed,name=field8,proto3" json:"field8,omitempty"` + Field9 []float64 `protobuf:"fixed64,9,rep,packed,name=field9,proto3" json:"field9,omitempty"` + Field10 []uint32 `protobuf:"fixed32,10,rep,packed,name=field10,proto3" json:"field10,omitempty"` + Field11 []uint64 `protobuf:"fixed64,11,rep,packed,name=field11,proto3" json:"field11,omitempty"` + Field12 []int32 `protobuf:"fixed32,12,rep,packed,name=field12,proto3" json:"field12,omitempty"` + Field13 []int64 `protobuf:"fixed64,13,rep,packed,name=field13,proto3" json:"field13,omitempty"` + Field14 []string `protobuf:"bytes,14,rep,name=field14,proto3" json:"field14,omitempty"` + Field15 [][]byte `protobuf:"bytes,15,rep,name=field15,proto3" json:"field15,omitempty"` + XXX_NoUnkeyedLiteral struct{} `json:"-"` + XXX_unrecognized []byte `json:"-"` + XXX_sizecache int32 `json:"-"` } func (m *List) Reset() { *m = List{} } @@ -372,12 +382,15 @@ func (m *List) GetField15() [][]byte { } type Map struct { - Field1 map[int32]int64 `protobuf:"bytes,1,rep,name=field1,proto3" json:"field1,omitempty" protobuf_key:"varint,1,opt,name=key,proto3" protobuf_val:"varint,2,opt,name=value,proto3"` - Field2 map[uint32]uint64 `protobuf:"bytes,2,rep,name=field2,proto3" json:"field2,omitempty" protobuf_key:"varint,1,opt,name=key,proto3" protobuf_val:"varint,2,opt,name=value,proto3"` - Field3 map[int32]int64 `protobuf:"bytes,3,rep,name=field3,proto3" json:"field3,omitempty" protobuf_key:"zigzag32,1,opt,name=key,proto3" protobuf_val:"zigzag64,2,opt,name=value,proto3"` - Field4 map[uint32]uint64 `protobuf:"bytes,4,rep,name=field4,proto3" json:"field4,omitempty" protobuf_key:"fixed32,1,opt,name=key,proto3" protobuf_val:"fixed64,2,opt,name=value,proto3"` - Field5 map[int32]int64 `protobuf:"bytes,5,rep,name=field5,proto3" json:"field5,omitempty" protobuf_key:"fixed32,1,opt,name=key,proto3" protobuf_val:"fixed64,2,opt,name=value,proto3"` - Field6 map[string][]byte `protobuf:"bytes,6,rep,name=field6,proto3" json:"field6,omitempty" protobuf_key:"bytes,1,opt,name=key,proto3" protobuf_val:"bytes,2,opt,name=value,proto3"` + Field1 map[int32]int64 `protobuf:"bytes,1,rep,name=field1,proto3" json:"field1,omitempty" protobuf_key:"varint,1,opt,name=key,proto3" protobuf_val:"varint,2,opt,name=value,proto3"` + Field2 map[uint32]uint64 `protobuf:"bytes,2,rep,name=field2,proto3" json:"field2,omitempty" protobuf_key:"varint,1,opt,name=key,proto3" protobuf_val:"varint,2,opt,name=value,proto3"` + Field3 map[int32]int64 `protobuf:"bytes,3,rep,name=field3,proto3" json:"field3,omitempty" protobuf_key:"zigzag32,1,opt,name=key,proto3" protobuf_val:"zigzag64,2,opt,name=value,proto3"` + Field4 map[uint32]uint64 `protobuf:"bytes,4,rep,name=field4,proto3" json:"field4,omitempty" protobuf_key:"fixed32,1,opt,name=key,proto3" protobuf_val:"fixed64,2,opt,name=value,proto3"` + Field5 map[int32]int64 `protobuf:"bytes,5,rep,name=field5,proto3" json:"field5,omitempty" protobuf_key:"fixed32,1,opt,name=key,proto3" protobuf_val:"fixed64,2,opt,name=value,proto3"` + Field6 map[string][]byte `protobuf:"bytes,6,rep,name=field6,proto3" json:"field6,omitempty" protobuf_key:"bytes,1,opt,name=key,proto3" protobuf_val:"bytes,2,opt,name=value,proto3"` + XXX_NoUnkeyedLiteral struct{} `json:"-"` + XXX_unrecognized []byte `json:"-"` + XXX_sizecache int32 `json:"-"` } func (m *Map) Reset() { *m = Map{} } @@ -471,38 +484,38 @@ func init() { func init() { proto.RegisterFile("testcase.proto", fileDescriptor_d7c4c3b74ab4cfaa) } var fileDescriptor_d7c4c3b74ab4cfaa = []byte{ - // 494 bytes of a gzipped FileDescriptorProto + // 485 bytes of a gzipped FileDescriptorProto 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x74, 0x94, 0x3f, 0x6f, 0xd3, 0x40, - 0x18, 0x87, 0xf3, 0xfa, 0x4d, 0x9c, 0xfa, 0x92, 0x34, 0xa9, 0x85, 0xd0, 0xa9, 0x83, 0x75, 0x8a, - 0x18, 0x6e, 0x32, 0xe4, 0xfc, 0xa7, 0x09, 0x23, 0x12, 0x4c, 0x94, 0xa1, 0x6c, 0x6c, 0x4e, 0x31, - 0x10, 0xb5, 0x4d, 0x23, 0xc7, 0x45, 0xea, 0xb7, 0xe0, 0x1b, 0xf0, 0x75, 0x3a, 0x76, 0x64, 0x44, - 0xc9, 0xb7, 0x60, 0x42, 0xd4, 0xe7, 0xb3, 0x83, 0xfd, 0x6e, 0xb9, 0xe7, 0xa7, 0x67, 0xc8, 0x3d, - 0xc9, 0xb1, 0xe3, 0x3c, 0xdd, 0xe6, 0x97, 0xc9, 0x36, 0xf5, 0x37, 0xd9, 0x6d, 0x7e, 0xeb, 0x3a, - 0xcb, 0x74, 0x7d, 0xf9, 0xed, 0x26, 0xc9, 0xae, 0xa6, 0x0f, 0x16, 0xb3, 0x3f, 0xdc, 0xdd, 0x2c, - 0xd3, 0xcc, 0x7d, 0xce, 0xec, 0x2f, 0xab, 0xf4, 0xfa, 0xf3, 0x8c, 0x83, 0x00, 0xd9, 0xbb, 0xd0, - 0x27, 0xc3, 0x15, 0xb7, 0x04, 0x48, 0xd4, 0x5c, 0x19, 0x1e, 0x70, 0x14, 0x20, 0x47, 0x9a, 0x07, - 0x86, 0x87, 0xbc, 0x2b, 0x40, 0x76, 0x35, 0x0f, 0x0d, 0x8f, 0x78, 0x4f, 0x80, 0x3c, 0xd1, 0x3c, - 0x32, 0x3c, 0xe6, 0xb6, 0x00, 0xe9, 0x6a, 0x1e, 0x1b, 0x7e, 0xc6, 0xfb, 0x02, 0xe4, 0x91, 0xe6, - 0x67, 0x86, 0xcf, 0xf9, 0x91, 0x00, 0x69, 0x69, 0x3e, 0x37, 0x7c, 0xc1, 0x1d, 0x01, 0x12, 0x34, - 0x5f, 0xb8, 0x9c, 0xf5, 0x8b, 0x6f, 0xf2, 0x8a, 0x33, 0x01, 0xb2, 0x7f, 0x51, 0x1e, 0xab, 0x65, - 0xc6, 0x07, 0x02, 0xa4, 0x5d, 0x2e, 0xb3, 0x6a, 0x51, 0x7c, 0x28, 0x40, 0x8e, 0xcb, 0x45, 0x55, - 0x4b, 0xc0, 0x47, 0x02, 0xe4, 0xa4, 0x5c, 0x82, 0xe9, 0x9c, 0xd9, 0x1f, 0xf3, 0x6c, 0xb5, 0xfe, - 0xfa, 0xdf, 0x4d, 0x3a, 0xc4, 0x4d, 0x0e, 0xcb, 0x9b, 0x9c, 0xfe, 0xb1, 0x58, 0xf7, 0xfd, 0x6a, - 0x9b, 0x1f, 0x88, 0x48, 0x24, 0x40, 0x22, 0x01, 0x12, 0x09, 0x90, 0x48, 0x80, 0x44, 0x02, 0x24, - 0x12, 0x20, 0x91, 0x00, 0x89, 0x04, 0x48, 0x25, 0x40, 0x32, 0x01, 0x92, 0x09, 0x90, 0x4c, 0x80, - 0xb5, 0x04, 0xd5, 0x12, 0xf2, 0x63, 0x81, 0xd2, 0x29, 0x97, 0xb0, 0x5a, 0x22, 0x3e, 0x16, 0x28, - 0x87, 0xe5, 0x12, 0x4d, 0x7f, 0xf6, 0x18, 0x9e, 0x27, 0x1b, 0x57, 0x1d, 0xdc, 0xfd, 0x40, 0x9d, - 0xfa, 0xe6, 0x5f, 0xe2, 0x9f, 0x27, 0x1b, 0xff, 0xdd, 0xd3, 0xf8, 0x76, 0x9d, 0x67, 0xf7, 0xa6, - 0x8b, 0x3a, 0xe8, 0x42, 0x38, 0xaa, 0xee, 0x28, 0xe3, 0x14, 0xcd, 0x08, 0x27, 0xa8, 0x3b, 0x81, - 0x71, 0x8a, 0x9e, 0x84, 0x13, 0xd6, 0x9d, 0xd0, 0x38, 0x45, 0x6b, 0xc2, 0x89, 0xea, 0x4e, 0x64, - 0x9c, 0xe2, 0x77, 0x40, 0x38, 0x71, 0xdd, 0x89, 0x4f, 0x17, 0x6c, 0x50, 0xbb, 0x1a, 0x77, 0xc2, - 0xf0, 0x2a, 0xbd, 0xd7, 0x4f, 0xc8, 0xbf, 0x8f, 0xee, 0x33, 0xd6, 0xfb, 0x9e, 0x5c, 0xdf, 0xa5, - 0xfa, 0xf9, 0x28, 0x0e, 0xaf, 0xad, 0x39, 0x18, 0x55, 0x35, 0xd4, 0x51, 0x8b, 0xda, 0x6d, 0x53, - 0x83, 0x86, 0x7a, 0xd2, 0xa2, 0xba, 0x6d, 0x6a, 0xd8, 0x50, 0xfb, 0x2d, 0xaa, 0xdd, 0xa6, 0x46, - 0x0d, 0x75, 0xdc, 0xa2, 0x4e, 0xda, 0xd4, 0xb8, 0xa1, 0x3a, 0x2d, 0xea, 0xb0, 0xa6, 0xbe, 0x79, - 0xf1, 0xb0, 0xf3, 0xe0, 0x71, 0xe7, 0xc1, 0xef, 0x9d, 0x07, 0x3f, 0xf6, 0x5e, 0xe7, 0x71, 0xef, - 0x75, 0x7e, 0xed, 0xbd, 0xce, 0x27, 0xe6, 0xbf, 0x2c, 0x9f, 0xf6, 0xa5, 0xfd, 0xf4, 0xb6, 0x07, - 0x7f, 0x03, 0x00, 0x00, 0xff, 0xff, 0xac, 0x69, 0x8a, 0x2d, 0xed, 0x05, 0x00, 0x00, + 0x18, 0x87, 0x79, 0xfd, 0x26, 0x4e, 0x7d, 0x49, 0x9a, 0xd4, 0x42, 0xe8, 0x54, 0xa1, 0xe8, 0x94, + 0xe9, 0x26, 0x43, 0xce, 0x7f, 0xea, 0x30, 0x22, 0xc1, 0x44, 0x19, 0xca, 0xc6, 0xe6, 0x14, 0x03, + 0x51, 0xdb, 0x34, 0x72, 0x5c, 0xa4, 0x7e, 0x13, 0x36, 0xbe, 0x4e, 0x47, 0x3e, 0x02, 0x0a, 0xdf, + 0x82, 0x09, 0x51, 0x9f, 0xcf, 0x0e, 0xbe, 0x77, 0xcb, 0x3d, 0x3f, 0x3d, 0x43, 0xee, 0x49, 0x8e, + 0x1d, 0x97, 0xf9, 0xae, 0xbc, 0xcc, 0x76, 0x79, 0xb0, 0x2d, 0x6e, 0xcb, 0x5b, 0xdf, 0x5b, 0xe5, + 0x9b, 0xcb, 0xaf, 0x37, 0x59, 0x71, 0x35, 0x7f, 0x70, 0x98, 0xfb, 0xfe, 0xee, 0x66, 0x95, 0x17, + 0xfe, 0x33, 0xe6, 0x7e, 0x5e, 0xe7, 0xd7, 0x9f, 0x16, 0x1c, 0x04, 0xc8, 0xfe, 0x85, 0x3e, 0x19, + 0xae, 0xb8, 0x23, 0x40, 0xa2, 0xe6, 0xca, 0xf0, 0x90, 0xa3, 0x00, 0x39, 0xd6, 0x3c, 0x34, 0x3c, + 0xe2, 0x3d, 0x01, 0xb2, 0xa7, 0x79, 0x64, 0x78, 0xcc, 0xfb, 0x02, 0xe4, 0x89, 0xe6, 0xb1, 0xe1, + 0x09, 0x77, 0x05, 0x48, 0x5f, 0xf3, 0xc4, 0xf0, 0x33, 0x3e, 0x10, 0x20, 0x8f, 0x34, 0x3f, 0x33, + 0x3c, 0xe5, 0x47, 0x02, 0xa4, 0xa3, 0x79, 0x6a, 0xf8, 0x92, 0x7b, 0x02, 0x24, 0x68, 0xbe, 0xf4, + 0x39, 0x1b, 0x54, 0xdf, 0xe4, 0x25, 0x67, 0x02, 0xe4, 0xe0, 0xa2, 0x3e, 0x36, 0xcb, 0x82, 0x0f, + 0x05, 0x48, 0xb7, 0x5e, 0x16, 0xcd, 0xa2, 0xf8, 0x48, 0x80, 0x9c, 0xd4, 0x8b, 0x6a, 0x96, 0x90, + 0x8f, 0x05, 0xc8, 0x69, 0xbd, 0x84, 0xf3, 0x94, 0xb9, 0x1f, 0xca, 0x62, 0xbd, 0xf9, 0xf2, 0xdf, + 0x4d, 0x7a, 0xc4, 0x4d, 0x8e, 0xea, 0x9b, 0x9c, 0xff, 0x71, 0x58, 0xef, 0xdd, 0x7a, 0x57, 0x1e, + 0x88, 0x48, 0x24, 0x40, 0x22, 0x01, 0x12, 0x09, 0x90, 0x48, 0x80, 0x44, 0x02, 0x24, 0x12, 0x20, + 0x91, 0x00, 0x89, 0x04, 0x48, 0x25, 0x40, 0x32, 0x01, 0x92, 0x09, 0x90, 0x4c, 0x80, 0xad, 0x04, + 0xcd, 0x12, 0xf1, 0x63, 0x81, 0xd2, 0xab, 0x97, 0xa8, 0x59, 0x62, 0x3e, 0x11, 0x28, 0x47, 0xf5, + 0x12, 0xcf, 0x7f, 0xf4, 0x19, 0x9e, 0x67, 0x5b, 0x5f, 0x1d, 0xdc, 0xfd, 0x50, 0x9d, 0x06, 0xe6, + 0x5f, 0x12, 0x9c, 0x67, 0xdb, 0xe0, 0xed, 0xe3, 0xf8, 0x66, 0x53, 0x16, 0xf7, 0xa6, 0x8b, 0x3a, + 0xe8, 0x42, 0x38, 0xaa, 0xed, 0x28, 0xe3, 0x54, 0xcd, 0x08, 0x27, 0x6c, 0x3b, 0xa1, 0x71, 0xaa, + 0x9e, 0x84, 0x13, 0xb5, 0x9d, 0xc8, 0x38, 0x55, 0x6b, 0xc2, 0x89, 0xdb, 0x4e, 0x6c, 0x9c, 0xea, + 0x77, 0x40, 0x38, 0x49, 0xdb, 0x49, 0x4e, 0x97, 0x6c, 0xd8, 0xba, 0x1a, 0x7f, 0xca, 0xf0, 0x2a, + 0xbf, 0xd7, 0x4f, 0xc8, 0xbf, 0x8f, 0xfe, 0x53, 0xd6, 0xff, 0x96, 0x5d, 0xdf, 0xe5, 0xfa, 0xf9, + 0xa8, 0x0e, 0xaf, 0x9c, 0x14, 0x8c, 0xaa, 0x3a, 0xea, 0xd8, 0xa2, 0xf6, 0x6c, 0x6a, 0xd8, 0x51, + 0x4f, 0x2c, 0xaa, 0x6f, 0x53, 0xa3, 0x8e, 0x3a, 0xb0, 0xa8, 0xae, 0x4d, 0x8d, 0x3b, 0xea, 0xc4, + 0xa2, 0x4e, 0x6d, 0x6a, 0xd2, 0x51, 0x3d, 0x8b, 0x3a, 0x6a, 0xa9, 0xaf, 0x9f, 0x3f, 0xec, 0x67, + 0xf0, 0x73, 0x3f, 0x83, 0x5f, 0xfb, 0x19, 0x7c, 0xff, 0x3d, 0x7b, 0xf2, 0x91, 0x05, 0x2f, 0xea, + 0x27, 0x7d, 0xe5, 0x3e, 0xbe, 0xe9, 0xe1, 0xdf, 0x00, 0x00, 0x00, 0xff, 0xff, 0x0f, 0x71, 0x65, + 0x63, 0xe5, 0x05, 0x00, 0x00, } func (m *Number) Marshal() (dAtA []byte, err error) { @@ -525,6 +538,10 @@ func (m *Number) MarshalToSizedBuffer(dAtA []byte) (int, error) { _ = i var l int _ = l + if m.XXX_unrecognized != nil { + i -= len(m.XXX_unrecognized) + copy(dAtA[i:], m.XXX_unrecognized) + } if m.Field13 != 0 { i -= 8 encoding_binary.LittleEndian.PutUint64(dAtA[i:], uint64(m.Field13)) @@ -624,6 +641,10 @@ func (m *String) MarshalToSizedBuffer(dAtA []byte) (int, error) { _ = i var l int _ = l + if m.XXX_unrecognized != nil { + i -= len(m.XXX_unrecognized) + copy(dAtA[i:], m.XXX_unrecognized) + } if len(m.Field2) > 0 { i -= len(m.Field2) copy(dAtA[i:], m.Field2) @@ -661,6 +682,10 @@ func (m *List) MarshalToSizedBuffer(dAtA []byte) (int, error) { _ = i var l int _ = l + if m.XXX_unrecognized != nil { + i -= len(m.XXX_unrecognized) + copy(dAtA[i:], m.XXX_unrecognized) + } if len(m.Field15) > 0 { for iNdEx := len(m.Field15) - 1; iNdEx >= 0; iNdEx-- { i -= len(m.Field15[iNdEx]) @@ -883,6 +908,10 @@ func (m *Map) MarshalToSizedBuffer(dAtA []byte) (int, error) { _ = i var l int _ = l + if m.XXX_unrecognized != nil { + i -= len(m.XXX_unrecognized) + copy(dAtA[i:], m.XXX_unrecognized) + } if len(m.Field6) > 0 { for k := range m.Field6 { v := m.Field6[k] @@ -1042,6 +1071,9 @@ func (m *Number) Size() (n int) { if m.Field13 != 0 { n += 9 } + if m.XXX_unrecognized != nil { + n += len(m.XXX_unrecognized) + } return n } @@ -1059,6 +1091,9 @@ func (m *String) Size() (n int) { if l > 0 { n += 1 + l + sovTestcase(uint64(l)) } + if m.XXX_unrecognized != nil { + n += len(m.XXX_unrecognized) + } return n } @@ -1143,6 +1178,9 @@ func (m *List) Size() (n int) { n += 1 + l + sovTestcase(uint64(l)) } } + if m.XXX_unrecognized != nil { + n += len(m.XXX_unrecognized) + } return n } @@ -1204,6 +1242,9 @@ func (m *Map) Size() (n int) { n += mapEntrySize + 1 + sovTestcase(uint64(mapEntrySize)) } } + if m.XXX_unrecognized != nil { + n += len(m.XXX_unrecognized) + } return n } @@ -1454,6 +1495,7 @@ func (m *Number) Unmarshal(dAtA []byte) error { if (iNdEx + skippy) > l { return io.ErrUnexpectedEOF } + m.XXX_unrecognized = append(m.XXX_unrecognized, dAtA[iNdEx:iNdEx+skippy]...) iNdEx += skippy } } @@ -1570,6 +1612,7 @@ func (m *String) Unmarshal(dAtA []byte) error { if (iNdEx + skippy) > l { return io.ErrUnexpectedEOF } + m.XXX_unrecognized = append(m.XXX_unrecognized, dAtA[iNdEx:iNdEx+skippy]...) iNdEx += skippy } } @@ -2530,6 +2573,7 @@ func (m *List) Unmarshal(dAtA []byte) error { if (iNdEx + skippy) > l { return io.ErrUnexpectedEOF } + m.XXX_unrecognized = append(m.XXX_unrecognized, dAtA[iNdEx:iNdEx+skippy]...) iNdEx += skippy } } @@ -3173,6 +3217,7 @@ func (m *Map) Unmarshal(dAtA []byte) error { if (iNdEx + skippy) > l { return io.ErrUnexpectedEOF } + m.XXX_unrecognized = append(m.XXX_unrecognized, dAtA[iNdEx:iNdEx+skippy]...) iNdEx += skippy } } diff --git a/examples/fastpb_gen/base.pb.fast.go b/examples/fastpb_gen/base.pb.fast.go index d9f51be..2890e8f 100644 --- a/examples/fastpb_gen/base.pb.fast.go +++ b/examples/fastpb_gen/base.pb.fast.go @@ -4,6 +4,7 @@ package fastpb_gen import ( fmt "fmt" + fastpb "github.com/cloudwego/fastpb" ) diff --git a/examples/fastpb_gen/base.pb.go b/examples/fastpb_gen/base.pb.go index 6c5af2a..be7b5cf 100644 --- a/examples/fastpb_gen/base.pb.go +++ b/examples/fastpb_gen/base.pb.go @@ -7,10 +7,11 @@ package fastpb_gen import ( - protoreflect "google.golang.org/protobuf/reflect/protoreflect" - protoimpl "google.golang.org/protobuf/runtime/protoimpl" reflect "reflect" sync "sync" + + protoreflect "google.golang.org/protobuf/reflect/protoreflect" + protoimpl "google.golang.org/protobuf/runtime/protoimpl" ) const ( diff --git a/examples/fastpb_gen/echo.pb.fast.go b/examples/fastpb_gen/echo.pb.fast.go index 1d36d8c..251ee7e 100644 --- a/examples/fastpb_gen/echo.pb.fast.go +++ b/examples/fastpb_gen/echo.pb.fast.go @@ -4,6 +4,7 @@ package fastpb_gen import ( fmt "fmt" + fastpb "github.com/cloudwego/fastpb" nested "github.com/cloudwego/fastpb/examples/fastpb_gen/nested" ) diff --git a/examples/fastpb_gen/echo.pb.go b/examples/fastpb_gen/echo.pb.go index b0f22da..bb0325e 100644 --- a/examples/fastpb_gen/echo.pb.go +++ b/examples/fastpb_gen/echo.pb.go @@ -7,11 +7,12 @@ package fastpb_gen import ( + reflect "reflect" + sync "sync" + nested "github.com/cloudwego/fastpb/examples/fastpb_gen/nested" protoreflect "google.golang.org/protobuf/reflect/protoreflect" protoimpl "google.golang.org/protobuf/runtime/protoimpl" - reflect "reflect" - sync "sync" ) const ( diff --git a/examples/fastpb_gen/nested/nested.pb.fast.go b/examples/fastpb_gen/nested/nested.pb.fast.go index 3e92d54..b3f8e81 100644 --- a/examples/fastpb_gen/nested/nested.pb.fast.go +++ b/examples/fastpb_gen/nested/nested.pb.fast.go @@ -4,6 +4,7 @@ package nested import ( fmt "fmt" + fastpb "github.com/cloudwego/fastpb" ) diff --git a/examples/fastpb_gen/nested/nested.pb.go b/examples/fastpb_gen/nested/nested.pb.go index 91c25f4..c98ab58 100644 --- a/examples/fastpb_gen/nested/nested.pb.go +++ b/examples/fastpb_gen/nested/nested.pb.go @@ -7,10 +7,11 @@ package nested import ( - protoreflect "google.golang.org/protobuf/reflect/protoreflect" - protoimpl "google.golang.org/protobuf/runtime/protoimpl" reflect "reflect" sync "sync" + + protoreflect "google.golang.org/protobuf/reflect/protoreflect" + protoimpl "google.golang.org/protobuf/runtime/protoimpl" ) const ( diff --git a/examples/fastpb_gen/pet/pet.pb.fast.go b/examples/fastpb_gen/pet/pet.pb.fast.go index 5d775f4..10232c3 100644 --- a/examples/fastpb_gen/pet/pet.pb.fast.go +++ b/examples/fastpb_gen/pet/pet.pb.fast.go @@ -4,6 +4,7 @@ package pet import ( fmt "fmt" + fastpb "github.com/cloudwego/fastpb" user "github.com/cloudwego/fastpb/examples/fastpb_gen/user" ) diff --git a/examples/fastpb_gen/pet/pet.pb.go b/examples/fastpb_gen/pet/pet.pb.go index bd55bf4..4a411c4 100644 --- a/examples/fastpb_gen/pet/pet.pb.go +++ b/examples/fastpb_gen/pet/pet.pb.go @@ -7,11 +7,12 @@ package pet import ( + reflect "reflect" + sync "sync" + user "github.com/cloudwego/fastpb/examples/fastpb_gen/user" protoreflect "google.golang.org/protobuf/reflect/protoreflect" protoimpl "google.golang.org/protobuf/runtime/protoimpl" - reflect "reflect" - sync "sync" ) const ( diff --git a/examples/fastpb_gen/user/user.pb.fast.go b/examples/fastpb_gen/user/user.pb.fast.go index b391262..eba30cf 100644 --- a/examples/fastpb_gen/user/user.pb.fast.go +++ b/examples/fastpb_gen/user/user.pb.fast.go @@ -4,6 +4,7 @@ package user import ( fmt "fmt" + fastpb "github.com/cloudwego/fastpb" ) diff --git a/examples/fastpb_gen/user/user.pb.go b/examples/fastpb_gen/user/user.pb.go index a7f3821..c1cf061 100644 --- a/examples/fastpb_gen/user/user.pb.go +++ b/examples/fastpb_gen/user/user.pb.go @@ -7,10 +7,11 @@ package user import ( - protoreflect "google.golang.org/protobuf/reflect/protoreflect" - protoimpl "google.golang.org/protobuf/runtime/protoimpl" reflect "reflect" sync "sync" + + protoreflect "google.golang.org/protobuf/reflect/protoreflect" + protoimpl "google.golang.org/protobuf/runtime/protoimpl" ) const ( diff --git a/fastpb_impl.go b/fastpb_impl.go index 02b6d2c..cabfa43 100644 --- a/fastpb_impl.go +++ b/fastpb_impl.go @@ -20,6 +20,8 @@ import ( "math" "unicode/utf8" + "github.com/cloudwego/fastpb/mem" + "google.golang.org/protobuf/encoding/protowire" ) @@ -31,7 +33,10 @@ var Impl impl // to make room). const speculativeLength = 1 -var _ Protocol = impl{} +var ( + _ Protocol = impl{} + spanCache = mem.NewSpanCache(1024 * 1024) // 1MB +) type impl struct{} @@ -412,7 +417,8 @@ func (b impl) ReadString(buf []byte, _type int8) (value string, n int, err error if EnforceUTF8() && !utf8.Valid(v) { return value, 0, errInvalidUTF8 } - return string(v), n, nil + value = SliceByteToString(spanCache.Copy(v)) + return value, n, nil } // ReadBytes . @@ -425,8 +431,7 @@ func (b impl) ReadBytes(buf []byte, _type int8) (value []byte, n int, err error) if n < 0 { return value, 0, errDecode } - value = make([]byte, len(v)) - copy(value, v) + value = spanCache.Copy(v) return value, n, nil } diff --git a/go.mod b/go.mod index 9cd7ed3..c3b0c96 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,9 @@ module github.com/cloudwego/fastpb go 1.17 require ( - github.com/gogo/protobuf v1.3.2 + github.com/bytedance/gopkg v0.0.0-20240514070511-01b2cbcf35e1 + github.com/golang/protobuf v1.5.0 google.golang.org/protobuf v1.28.0 ) + +require golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect diff --git a/go.sum b/go.sum index 376a1aa..1d5cb07 100644 --- a/go.sum +++ b/go.sum @@ -1,38 +1,25 @@ -github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q= -github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q= +github.com/bytedance/gopkg v0.0.0-20240514070511-01b2cbcf35e1 h1:rT7Mm6uUpHeZQzfs2v0Mlj0SL02CzyVi+EB7VYPM/z4= +github.com/bytedance/gopkg v0.0.0-20240514070511-01b2cbcf35e1/go.mod h1:FtQG3YbQG9L/91pbKSw787yBQPutC+457AvDW77fgUQ= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/golang/protobuf v1.5.0 h1:LUVKkCeviFUMKqHa4tXIIij/lbhnMbP7Fn5wKdKkRh4= github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= -github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8= -github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= -github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= -github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= -golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= -golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= -golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= -golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= -golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= -golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= -golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= -golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= -golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= -golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= -golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +golang.org/x/net v0.0.0-20221014081412-f15817d10f9b/go.mod h1:YDH+HFinaLZZlnHAfSS6ZXJJ9M9t4Dl22yv3iI2vPwk= +golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220728004956-3c1f35247d10/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= +golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= -golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= -golang.org/x/tools v0.0.0-20200619180055-7c47624df98f/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE= -golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= -golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE= golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= google.golang.org/protobuf v1.28.0 h1:w43yiav+6bVFTBQFZX0r7ipe9JQ1QsbMgHwbBziscLw= google.golang.org/protobuf v1.28.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/mem/span.go b/mem/span.go new file mode 100644 index 0000000..8d9242a --- /dev/null +++ b/mem/span.go @@ -0,0 +1,110 @@ +/* + * Copyright 2024 CloudWeGo Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package mem + +import ( + "math/bits" + "sync/atomic" + + "github.com/bytedance/gopkg/lang/dirtmake" +) + +const ( + spanCacheSize = 10 + minSpanObject = 128 // 128 B + maxSpanObject = (minSpanObject << spanCacheSize) - 1 // 128 KB + minSpanClass = 8 // = spanClass(minSpanObject) +) + +type spanCache struct { + spans [spanCacheSize]*span +} + +// NewSpanCache returns *spanCache with the given spanSize, +// each span is used to allocate a binary of a specific size level. +func NewSpanCache(spanSize int) *spanCache { + c := new(spanCache) + for i := 0; i < len(c.spans); i++ { + c.spans[i] = NewSpan(spanSize) + } + return c +} + +// Make allocates a binary but does not clear the memory it references. +// NOTE: MUST set any byte element before it's read. +func (c *spanCache) Make(n int) []byte { + sclass := spanClass(n) - minSpanClass + if sclass < 0 || sclass >= len(c.spans) { + return dirtmake.Bytes(n, n) + } + return c.spans[sclass].Make(n) +} + +func (c *spanCache) Copy(buf []byte) (p []byte) { + p = c.Make(len(buf)) + copy(p, buf) + return p +} + +func NewSpan(size int) *span { + sp := new(span) + sp.size = uint32(size) + sp.buffer = dirtmake.Bytes(0, size) + return sp +} + +type span struct { + lock uint32 + read uint32 // read index of buffer + size uint32 // size of buffer + buffer []byte +} + +func (b *span) Make(_n int) []byte { + n := uint32(_n) + if n >= b.size || !atomic.CompareAndSwapUint32(&b.lock, 0, 1) { + // fallback path: make a new byte slice if current goroutine cannot get the lock or n is out of size + return dirtmake.Bytes(int(n), int(n)) + } +START: + b.read += n + // fast path + if b.read <= b.size { + buf := b.buffer[b.read-n : b.read : b.read] + atomic.StoreUint32(&b.lock, 0) + return buf + } + // slow path: create a new buffer + b.buffer = dirtmake.Bytes(int(b.size), int(b.size)) + b.read = 0 + goto START +} + +func (b *span) Copy(buf []byte) (p []byte) { + p = b.Make(len(buf)) + copy(p, buf) + return p +} + +// spanClass calc the minimum number of bits required to represent x +// [2^sclass,2^(sclass+1)) bytes in a same span class +func spanClass(size int) int { + if size == 0 { + return 0 + } + return bits.Len(uint(size)) +} diff --git a/util.go b/util.go new file mode 100644 index 0000000..67a6edf --- /dev/null +++ b/util.go @@ -0,0 +1,9 @@ +package fastpb + +import "unsafe" + +// SliceByteToString converts []byte to string without copy. +// DO NOT USE unless you know what you're doing. +func SliceByteToString(b []byte) string { + return *(*string)(unsafe.Pointer(&b)) +}