Package size
Overview ▹
Overview ▾
The size plugin generates a Size or ProtoSize method for each message. This is useful with the MarshalTo method generated by the marshalto plugin and the gogoproto.marshaler and gogoproto.marshaler_all extensions.
It is enabled by the following extensions:
- sizer - sizer_all - protosizer - protosizer_all
The size plugin also generates a test given it is enabled using one of the following extensions:
- testgen - testgen_all
And a benchmark given it is enabled using one of the following extensions:
- benchgen - benchgen_all
Let us look at:
github.com/gogo/protobuf/test/example/example.proto
Btw all the output can be seen at:
github.com/gogo/protobuf/test/example/*
The following message:
option (gogoproto.sizer_all) = true; message B { option (gogoproto.description) = true; optional A A = 1 [(gogoproto.nullable) = false, (gogoproto.embed) = true]; repeated bytes G = 2 [(gogoproto.customtype) = "github.com/gogo/protobuf/test/custom.Uint128", (gogoproto.nullable) = false]; }
given to the size plugin, will generate the following code:
func (m *B) Size() (n int) { var l int _ = l l = m.A.Size() n += 1 + l + sovExample(uint64(l)) if len(m.G) > 0 { for _, e := range m.G { l = e.Size() n += 1 + l + sovExample(uint64(l)) } } if m.XXX_unrecognized != nil { n += len(m.XXX_unrecognized) } return n }
and the following test code:
func TestBSize(t *testing5.T) { popr := math_rand5.New(math_rand5.NewSource(time5.Now().UnixNano())) p := NewPopulatedB(popr, true) dAtA, err := github_com_gogo_protobuf_proto2.Marshal(p) if err != nil { panic(err) } size := p.Size() if len(dAtA) != size { t.Fatalf("size %v != marshalled size %v", size, len(dAtA)) } } func BenchmarkBSize(b *testing5.B) { popr := math_rand5.New(math_rand5.NewSource(616)) total := 0 pops := make([]*B, 1000) for i := 0; i < 1000; i++ { pops[i] = NewPopulatedB(popr, false) } b.ResetTimer() for i := 0; i < b.N; i++ { total += pops[i%1000].Size() } b.SetBytes(int64(total / b.N)) }
The sovExample function is a size of varint function for the example.pb.go file.
Index ▹
Index ▾
Package files
Internal call graph ▹
Internal call graph ▾
In the call graph viewer below, each node is a function belonging to this package and its children are the functions it calls—perhaps dynamically.
The root nodes are the entry points of the package: functions that may be called from outside the package. There may be non-exported or anonymous functions among them if they are called dynamically from another package.
Click a node to visit that function's source code.
From there you can visit its callers by
clicking its declaring func
token.
Functions may be omitted if they were determined to be unreachable in the particular programs or tests that were analyzed.
func NewSize ¶
func NewSize() *size
func NewTest ¶
func NewTest(g *generator.Generator) testgen.TestPlugin
Portions of this page are modifications based on work created and shared by Google
and used according to terms described in the Creative Commons 3.0 Attribution License.
© 2018 ActiveState Software Inc. All rights reserved. Trademarks.