Skip to content

Commit ec0c47e

Browse files
committed
proto: improve comment
1 parent 50dfe64 commit ec0c47e

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

encoding/proto/proto.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,21 @@ func (c *codecV2) Marshal(v any) (data mem.BufferSlice, err error) {
4646
return nil, fmt.Errorf("proto: failed to marshal, message is %T, want proto.Message", v)
4747
}
4848

49+
// Important: if we remove this Size call then we cannot use
50+
// UseCachedSize in MarshalOptions below.
4951
size := proto.Size(vv)
50-
// proto.Size caches the size, enabling UseCachedSize
51-
// lets us reuse that value instead of recomputing it during marshal.
52+
53+
// MarshalOptions with UseCachedSize allows reusing the result from the
54+
// previous Size call. This is safe here because:
55+
//
56+
// 1. We just computed the size.
57+
// 2. We assume the message is not being mutated concurrently.
58+
//
59+
// Important: If the proto.Size call above is removed, using UseCachedSize
60+
// becomes unsafe and may lead to incorrect marshaling.
61+
//
62+
// For more details, see the doc of UseCachedSize:
63+
// https://pkg.go.dev/google.golang.org/protobuf/proto#MarshalOptions
5264
marshalOptions := proto.MarshalOptions{UseCachedSize: true}
5365

5466
if mem.IsBelowBufferPoolingThreshold(size) {

0 commit comments

Comments
 (0)