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