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