| 41 | type codecV2 struct{} |
| 42 | |
| 43 | func (c *codecV2) Marshal(v any) (data mem.BufferSlice, err error) { |
| 44 | vv := messageV2Of(v) |
| 45 | if vv == nil { |
| 46 | return nil, fmt.Errorf("proto: failed to marshal, message is %T, want proto.Message", v) |
| 47 | } |
| 48 | |
| 49 | // Important: if we remove this Size call then we cannot use |
| 50 | // UseCachedSize in MarshalOptions below. |
| 51 | size := proto.Size(vv) |
| 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 |
| 64 | marshalOptions := proto.MarshalOptions{UseCachedSize: true} |
| 65 | |
| 66 | if mem.IsBelowBufferPoolingThreshold(size) { |
| 67 | buf, err := marshalOptions.Marshal(vv) |
| 68 | if err != nil { |
| 69 | return nil, err |
| 70 | } |
| 71 | data = append(data, mem.SliceBuffer(buf)) |
| 72 | } else { |
| 73 | pool := mem.DefaultBufferPool() |
| 74 | buf := pool.Get(size) |
| 75 | if _, err := marshalOptions.MarshalAppend((*buf)[:0], vv); err != nil { |
| 76 | pool.Put(buf) |
| 77 | return nil, err |
| 78 | } |
| 79 | data = append(data, mem.NewBuffer(buf, pool)) |
| 80 | } |
| 81 | |
| 82 | return data, nil |
| 83 | } |
| 84 | |
| 85 | func (c *codecV2) Unmarshal(data mem.BufferSlice, v any) (err error) { |
| 86 | vv := messageV2Of(v) |