blob: 554b9c6c09a1fae51c688bef6c7a88123fd4c338 [file] [log] [blame]
kesavand2cde6582020-06-22 04:56:23 -04001// Copyright 2019 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5package proto
6
7import (
8 "google.golang.org/protobuf/encoding/protowire"
9 "google.golang.org/protobuf/internal/encoding/messageset"
10 "google.golang.org/protobuf/reflect/protoreflect"
11 "google.golang.org/protobuf/runtime/protoiface"
12)
13
14// Size returns the size in bytes of the wire-format encoding of m.
15func Size(m Message) int {
16 return MarshalOptions{}.Size(m)
17}
18
19// Size returns the size in bytes of the wire-format encoding of m.
20func (o MarshalOptions) Size(m Message) int {
Andrea Campanella764f1ed2022-03-24 11:46:38 +010021 // Treat a nil message interface as an empty message; nothing to output.
22 if m == nil {
23 return 0
24 }
25
26 return o.size(m.ProtoReflect())
kesavand2cde6582020-06-22 04:56:23 -040027}
28
Andrea Campanella764f1ed2022-03-24 11:46:38 +010029// size is a centralized function that all size operations go through.
30// For profiling purposes, avoid changing the name of this function or
31// introducing other code paths for size that do not go through this.
32func (o MarshalOptions) size(m protoreflect.Message) (size int) {
kesavand2cde6582020-06-22 04:56:23 -040033 methods := protoMethods(m)
34 if methods != nil && methods.Size != nil {
35 out := methods.Size(protoiface.SizeInput{
36 Message: m,
37 })
38 return out.Size
39 }
40 if methods != nil && methods.Marshal != nil {
41 // This is not efficient, but we don't have any choice.
42 // This case is mainly used for legacy types with a Marshal method.
43 out, _ := methods.Marshal(protoiface.MarshalInput{
44 Message: m,
45 })
46 return len(out.Buf)
47 }
Andrea Campanella764f1ed2022-03-24 11:46:38 +010048 return o.sizeMessageSlow(m)
kesavand2cde6582020-06-22 04:56:23 -040049}
50
Andrea Campanella764f1ed2022-03-24 11:46:38 +010051func (o MarshalOptions) sizeMessageSlow(m protoreflect.Message) (size int) {
kesavand2cde6582020-06-22 04:56:23 -040052 if messageset.IsMessageSet(m.Descriptor()) {
Andrea Campanella764f1ed2022-03-24 11:46:38 +010053 return o.sizeMessageSet(m)
kesavand2cde6582020-06-22 04:56:23 -040054 }
55 m.Range(func(fd protoreflect.FieldDescriptor, v protoreflect.Value) bool {
Andrea Campanella764f1ed2022-03-24 11:46:38 +010056 size += o.sizeField(fd, v)
kesavand2cde6582020-06-22 04:56:23 -040057 return true
58 })
59 size += len(m.GetUnknown())
60 return size
61}
62
Andrea Campanella764f1ed2022-03-24 11:46:38 +010063func (o MarshalOptions) sizeField(fd protoreflect.FieldDescriptor, value protoreflect.Value) (size int) {
kesavand2cde6582020-06-22 04:56:23 -040064 num := fd.Number()
65 switch {
66 case fd.IsList():
Andrea Campanella764f1ed2022-03-24 11:46:38 +010067 return o.sizeList(num, fd, value.List())
kesavand2cde6582020-06-22 04:56:23 -040068 case fd.IsMap():
Andrea Campanella764f1ed2022-03-24 11:46:38 +010069 return o.sizeMap(num, fd, value.Map())
kesavand2cde6582020-06-22 04:56:23 -040070 default:
Andrea Campanella764f1ed2022-03-24 11:46:38 +010071 return protowire.SizeTag(num) + o.sizeSingular(num, fd.Kind(), value)
kesavand2cde6582020-06-22 04:56:23 -040072 }
73}
74
Andrea Campanella764f1ed2022-03-24 11:46:38 +010075func (o MarshalOptions) sizeList(num protowire.Number, fd protoreflect.FieldDescriptor, list protoreflect.List) (size int) {
kesavand2cde6582020-06-22 04:56:23 -040076 if fd.IsPacked() && list.Len() > 0 {
77 content := 0
78 for i, llen := 0, list.Len(); i < llen; i++ {
Andrea Campanella764f1ed2022-03-24 11:46:38 +010079 content += o.sizeSingular(num, fd.Kind(), list.Get(i))
kesavand2cde6582020-06-22 04:56:23 -040080 }
81 return protowire.SizeTag(num) + protowire.SizeBytes(content)
82 }
83
84 for i, llen := 0, list.Len(); i < llen; i++ {
Andrea Campanella764f1ed2022-03-24 11:46:38 +010085 size += protowire.SizeTag(num) + o.sizeSingular(num, fd.Kind(), list.Get(i))
kesavand2cde6582020-06-22 04:56:23 -040086 }
87 return size
88}
89
Andrea Campanella764f1ed2022-03-24 11:46:38 +010090func (o MarshalOptions) sizeMap(num protowire.Number, fd protoreflect.FieldDescriptor, mapv protoreflect.Map) (size int) {
kesavand2cde6582020-06-22 04:56:23 -040091 mapv.Range(func(key protoreflect.MapKey, value protoreflect.Value) bool {
92 size += protowire.SizeTag(num)
Andrea Campanella764f1ed2022-03-24 11:46:38 +010093 size += protowire.SizeBytes(o.sizeField(fd.MapKey(), key.Value()) + o.sizeField(fd.MapValue(), value))
kesavand2cde6582020-06-22 04:56:23 -040094 return true
95 })
96 return size
97}