Skip to content

Commit 188bece

Browse files
committed
proto: move MarshalOptions closer to size call and add comment clarifying the use of UseCachedSize
1 parent 19a8cef commit 188bece

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

encoding/proto/proto.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,20 @@ func (c *codecV2) Marshal(v any) (data mem.BufferSlice, err error) {
4848
}
4949

5050
size := proto.Size(vv)
51+
// proto.Size caches the size, enabling UseCachedSize
52+
// lets us reuse that value instead of recomputing it during marshal.
53+
marshalOptions := proto.MarshalOptions{UseCachedSize: true}
54+
5155
if mem.IsBelowBufferPoolingThreshold(size) {
52-
buf, err := proto.MarshalOptions{UseCachedSize: true}.Marshal(vv)
56+
buf, err := marshalOptions.Marshal(vv)
5357
if err != nil {
5458
return nil, err
5559
}
5660
data = append(data, mem.SliceBuffer(buf))
5761
} else {
5862
pool := mem.DefaultBufferPool()
5963
buf := pool.Get(size)
60-
if _, err := (proto.MarshalOptions{UseCachedSize: true}).MarshalAppend((*buf)[:0], vv); err != nil {
64+
if _, err := marshalOptions.MarshalAppend((*buf)[:0], vv); err != nil {
6165
pool.Put(buf)
6266
return nil, err
6367
}

0 commit comments

Comments
 (0)