MCPcopy
hub / github.com/protocolbuffers/protobuf-go / TestMarshalAppendAllocations

Function TestMarshalAppendAllocations

proto/encode_test.go:160–188  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

158}
159
160func TestMarshalAppendAllocations(t *testing.T) {
161 // This test ensures that MarshalAppend() has the same performance
162 // characteristics as the append() builtin, meaning that repeated calls do
163 // not allocate each time, but allocations are amortized.
164 m := &test3pb.TestAllTypes{SingularInt32: 1}
165 size := proto.Size(m)
166 const count = 1000
167 b := make([]byte, size)
168 // AllocsPerRun returns an integral value.
169 marshalAllocs := testing.AllocsPerRun(count, func() {
170 _, err := proto.MarshalOptions{}.MarshalAppend(b[:0], m)
171 if err != nil {
172 t.Fatal(err)
173 }
174 })
175 b = nil
176 marshalAppendAllocs := testing.AllocsPerRun(count, func() {
177 var err error
178 b, err = proto.MarshalOptions{}.MarshalAppend(b, m)
179 if err != nil {
180 t.Fatal(err)
181 }
182 })
183 if marshalAllocs != marshalAppendAllocs {
184 t.Errorf("%v allocs/op when writing to a preallocated buffer", marshalAllocs)
185 t.Errorf("%v allocs/op when repeatedly appending to a slice", marshalAppendAllocs)
186 t.Errorf("expect amortized allocs/op to be identical")
187 }
188}
189
190func TestEncodeOrder(t *testing.T) {
191 // We make no guarantees about the stability of wire marshal output.

Callers

nothing calls this directly

Calls 2

SizeFunction · 0.92
MarshalAppendMethod · 0.45

Tested by

no test coverage detected