Skip to content

Commit fdae5ae

Browse files
feat: implement ProtoMethods.CheckInitialized to avoid the slow path
CheckInitialized checks for missing required fields, which is not relevant for a proto3 implementation such as Pulsar. However, without it protobuf will fall back to a slow fallback that allocates memory per serialization. Before: Benchmark_Pulsar_Marshal-6 7251019 153.9 ns/op 51.57 B/serial 96 B/op 2 allocs/op Benchmark_Pulsar_Unmarshal-6 6409070 187.2 ns/op 51.63 B/serial 160 B/op 5 allocs/op After: Benchmark_Pulsar_Marshal-6 12480471 91.47 ns/op 51.61 B/serial 128 B/op 2 allocs/op Benchmark_Pulsar_Unmarshal-6 10919157 108.6 ns/op 51.58 B/serial 128 B/op 3 allocs/op
1 parent 743ad54 commit fdae5ae

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

features/fastreflection/proto_message.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,15 +306,16 @@ func (g *fastGenerator) genProtoMethods() {
306306
g.genSizeMethod()
307307
g.genMarshalMethod()
308308
g.genUnmarshalMethod()
309+
g.genCheckInitializedMethod()
309310

310311
g.P(varName, " = &", protoifacePkg.Ident("Methods"), "{ ")
311312
g.P("NoUnkeyedLiterals: struct{}{},")
312313
g.P("Flags: ", protoifacePkg.Ident("SupportMarshalDeterministic"), "|", protoifacePkg.Ident("SupportUnmarshalDiscardUnknown"), ",")
313314
g.P("Size: size,")
314315
g.P("Marshal: marshal,")
315316
g.P("Unmarshal: unmarshal,")
317+
g.P("CheckInitialized: checkInitialized,")
316318
g.P("Merge: nil,")
317-
g.P("CheckInitialized: nil,")
318319
g.P("}")
319320
g.P("}")
320321
}

features/fastreflection/proto_unmarshal.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,13 @@ func (g *fastGenerator) genUnmarshalMethod() {
126126
g.P(`}`)
127127
}
128128

129+
func (g *fastGenerator) genCheckInitializedMethod() {
130+
// checkInitialized method.
131+
g.P(`checkInitialized := func(input `, protoifacePkg.Ident("CheckInitializedInput"), `) (`, protoifacePkg.Ident("CheckInitializedOutput"), `, error) {`)
132+
g.P(`return `, protoifacePkg.Ident("CheckInitializedOutput"), `{}, nil`)
133+
g.P(`}`)
134+
}
135+
129136
func (g *fastGenerator) decodeVarint(varName string, typName string) {
130137
g.P(`for shift := uint(0); ; shift += 7 {`)
131138
g.P(`if shift >= 64 {`)

0 commit comments

Comments
 (0)