Damien Neil | 61e93c7 | 2019-03-27 09:23:20 -0700 | [diff] [blame] | 1 | // 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 | |
| 5 | package proto |
| 6 | |
| 7 | import ( |
Damien Neil | 302cb32 | 2019-06-19 15:22:13 -0700 | [diff] [blame^] | 8 | "google.golang.org/protobuf/internal/encoding/messageset" |
Damien Neil | e89e624 | 2019-05-13 23:55:40 -0700 | [diff] [blame] | 9 | "google.golang.org/protobuf/internal/encoding/wire" |
| 10 | "google.golang.org/protobuf/reflect/protoreflect" |
Joe Tsai | f8b855d | 2019-07-12 13:37:59 -0700 | [diff] [blame] | 11 | "google.golang.org/protobuf/runtime/protoiface" |
Damien Neil | 61e93c7 | 2019-03-27 09:23:20 -0700 | [diff] [blame] | 12 | ) |
| 13 | |
| 14 | // Size returns the size in bytes of the wire-format encoding of m. |
| 15 | func Size(m Message) int { |
Damien Neil | 03e7486 | 2019-04-07 18:18:31 -0700 | [diff] [blame] | 16 | return MarshalOptions{}.Size(m) |
| 17 | } |
| 18 | |
| 19 | // Size returns the size in bytes of the wire-format encoding of m. |
| 20 | func (o MarshalOptions) Size(m Message) int { |
Damien Neil | 61e93c7 | 2019-03-27 09:23:20 -0700 | [diff] [blame] | 21 | return sizeMessage(m.ProtoReflect()) |
| 22 | } |
| 23 | |
Joe Tsai | 0f81b38 | 2019-07-10 23:14:31 -0700 | [diff] [blame] | 24 | func sizeMessage(m protoreflect.Message) (size int) { |
| 25 | if methods := protoMethods(m); methods != nil && methods.Size != nil { |
Joe Tsai | f8b855d | 2019-07-12 13:37:59 -0700 | [diff] [blame] | 26 | return methods.Size(m, protoiface.MarshalOptions{}) |
Damien Neil | 0d3e8cc | 2019-04-01 13:31:55 -0700 | [diff] [blame] | 27 | } |
Joe Tsai | 0f81b38 | 2019-07-10 23:14:31 -0700 | [diff] [blame] | 28 | return sizeMessageSlow(m) |
Damien Neil | 0d3e8cc | 2019-04-01 13:31:55 -0700 | [diff] [blame] | 29 | } |
| 30 | |
Joe Tsai | 0f81b38 | 2019-07-10 23:14:31 -0700 | [diff] [blame] | 31 | func sizeMessageSlow(m protoreflect.Message) (size int) { |
Damien Neil | 302cb32 | 2019-06-19 15:22:13 -0700 | [diff] [blame^] | 32 | if messageset.IsMessageSet(m.Descriptor()) { |
| 33 | return sizeMessageSet(m) |
| 34 | } |
Joe Tsai | 378c132 | 2019-04-25 23:48:08 -0700 | [diff] [blame] | 35 | m.Range(func(fd protoreflect.FieldDescriptor, v protoreflect.Value) bool { |
| 36 | size += sizeField(fd, v) |
Damien Neil | 61e93c7 | 2019-03-27 09:23:20 -0700 | [diff] [blame] | 37 | return true |
| 38 | }) |
Joe Tsai | 378c132 | 2019-04-25 23:48:08 -0700 | [diff] [blame] | 39 | size += len(m.GetUnknown()) |
Damien Neil | 61e93c7 | 2019-03-27 09:23:20 -0700 | [diff] [blame] | 40 | return size |
| 41 | } |
| 42 | |
Joe Tsai | ac31a35 | 2019-05-13 14:32:56 -0700 | [diff] [blame] | 43 | func sizeField(fd protoreflect.FieldDescriptor, value protoreflect.Value) (size int) { |
| 44 | num := fd.Number() |
Damien Neil | 61e93c7 | 2019-03-27 09:23:20 -0700 | [diff] [blame] | 45 | switch { |
Joe Tsai | ac31a35 | 2019-05-13 14:32:56 -0700 | [diff] [blame] | 46 | case fd.IsList(): |
| 47 | return sizeList(num, fd, value.List()) |
| 48 | case fd.IsMap(): |
| 49 | return sizeMap(num, fd, value.Map()) |
Damien Neil | 61e93c7 | 2019-03-27 09:23:20 -0700 | [diff] [blame] | 50 | default: |
Joe Tsai | ac31a35 | 2019-05-13 14:32:56 -0700 | [diff] [blame] | 51 | return wire.SizeTag(num) + sizeSingular(num, fd.Kind(), value) |
Damien Neil | 61e93c7 | 2019-03-27 09:23:20 -0700 | [diff] [blame] | 52 | } |
| 53 | } |
| 54 | |
Joe Tsai | ac31a35 | 2019-05-13 14:32:56 -0700 | [diff] [blame] | 55 | func sizeList(num wire.Number, fd protoreflect.FieldDescriptor, list protoreflect.List) (size int) { |
Joe Tsai | 378c132 | 2019-04-25 23:48:08 -0700 | [diff] [blame] | 56 | if fd.IsPacked() && list.Len() > 0 { |
Joe Tsai | ac31a35 | 2019-05-13 14:32:56 -0700 | [diff] [blame] | 57 | content := 0 |
| 58 | for i, llen := 0, list.Len(); i < llen; i++ { |
| 59 | content += sizeSingular(num, fd.Kind(), list.Get(i)) |
| 60 | } |
| 61 | return wire.SizeTag(num) + wire.SizeBytes(content) |
| 62 | } |
| 63 | |
| 64 | for i, llen := 0, list.Len(); i < llen; i++ { |
| 65 | size += wire.SizeTag(num) + sizeSingular(num, fd.Kind(), list.Get(i)) |
| 66 | } |
Damien Neil | 61e93c7 | 2019-03-27 09:23:20 -0700 | [diff] [blame] | 67 | return size |
| 68 | } |
| 69 | |
Joe Tsai | ac31a35 | 2019-05-13 14:32:56 -0700 | [diff] [blame] | 70 | func sizeMap(num wire.Number, fd protoreflect.FieldDescriptor, mapv protoreflect.Map) (size int) { |
| 71 | mapv.Range(func(key protoreflect.MapKey, value protoreflect.Value) bool { |
| 72 | size += wire.SizeTag(num) |
| 73 | size += wire.SizeBytes(sizeField(fd.MapKey(), key.Value()) + sizeField(fd.MapValue(), value)) |
| 74 | return true |
| 75 | }) |
Damien Neil | 61e93c7 | 2019-03-27 09:23:20 -0700 | [diff] [blame] | 76 | return size |
| 77 | } |