| 1 | // Copyright 2024 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_testscip-go gomod google.golang.org/protobuf . `google.golang.org/protobuf/proto_test`/ |
| 6 | |
| 7 | import ( |
| 8 | "testingscip-go gomod github.com/golang/go/src go1.23 testing/" |
| 9 | |
| 10 | "google.golang.org/protobuf/protoscip-go gomod google.golang.org/protobuf . `google.golang.org/protobuf/proto`/" |
| 11 | |
| 12 | lazyopaquepbscip-go gomod google.golang.org/protobuf . `google.golang.org/protobuf/internal/testprotos/lazy/lazy_opaque`/ "google.golang.org/protobuf/internal/testprotos/lazy/lazy_opaquescip-go gomod google.golang.org/protobuf . `google.golang.org/protobuf/internal/testprotos/lazy/lazy_opaque`/" |
| 13 | ) |
| 14 | |
| 15 | // testMessageLinked returns a test message with a few fields of various |
| 16 | // possible types filled in that nests more messages like a linked list. |
| 17 | func testMessageLinkedscip-go gomod google.golang.org/protobuf . `google.golang.org/protobuf/proto_test`/testMessageLinked().(nesting int) *lazyopaquepbscip-go gomod google.golang.org/protobuf . `google.golang.org/protobuf/internal/testprotos/lazy/lazy_opaque`/.Nodescip-go gomod google.golang.org/protobuf . `google.golang.org/protobuf/internal/testprotos/lazy/lazy_opaque`/Node# { |
| 18 | const ( |
| 19 | shortVarint = 23 // encodes into 1 byte |
| 20 | longVarint = 562949953421312 // encodes into 8 bytes |
| 21 | ) |
| 22 | msg := lazyopaquepbscip-go gomod google.golang.org/protobuf . `google.golang.org/protobuf/internal/testprotos/lazy/lazy_opaque`/.Node_builderscip-go gomod google.golang.org/protobuf . `google.golang.org/protobuf/internal/testprotos/lazy/lazy_opaque`/Node_builder#{ |
| 23 | Int32scip-go gomod google.golang.org/protobuf . `google.golang.org/protobuf/internal/testprotos/lazy/lazy_opaque`/Node_builder#Int32.: protoscip-go gomod google.golang.org/protobuf . `google.golang.org/protobuf/proto`/.Int32scip-go gomod google.golang.org/protobuf . `google.golang.org/protobuf/proto`/Int32().(shortVarint), |
| 24 | Int64scip-go gomod google.golang.org/protobuf . `google.golang.org/protobuf/internal/testprotos/lazy/lazy_opaque`/Node_builder#Int64.: protoscip-go gomod google.golang.org/protobuf . `google.golang.org/protobuf/proto`/.Int64scip-go gomod google.golang.org/protobuf . `google.golang.org/protobuf/proto`/Int64().(longVarint), |
| 25 | Uint32scip-go gomod google.golang.org/protobuf . `google.golang.org/protobuf/internal/testprotos/lazy/lazy_opaque`/Node_builder#Uint32.: protoscip-go gomod google.golang.org/protobuf . `google.golang.org/protobuf/proto`/.Uint32scip-go gomod google.golang.org/protobuf . `google.golang.org/protobuf/proto`/Uint32().(shortVarint), |
| 26 | Uint64scip-go gomod google.golang.org/protobuf . `google.golang.org/protobuf/internal/testprotos/lazy/lazy_opaque`/Node_builder#Uint64.: protoscip-go gomod google.golang.org/protobuf . `google.golang.org/protobuf/proto`/.Uint64scip-go gomod google.golang.org/protobuf . `google.golang.org/protobuf/proto`/Uint64().(longVarint), |
| 27 | Sint32scip-go gomod google.golang.org/protobuf . `google.golang.org/protobuf/internal/testprotos/lazy/lazy_opaque`/Node_builder#Sint32.: protoscip-go gomod google.golang.org/protobuf . `google.golang.org/protobuf/proto`/.Int32scip-go gomod google.golang.org/protobuf . `google.golang.org/protobuf/proto`/Int32().(shortVarint), |
| 28 | Sint64scip-go gomod google.golang.org/protobuf . `google.golang.org/protobuf/internal/testprotos/lazy/lazy_opaque`/Node_builder#Sint64.: protoscip-go gomod google.golang.org/protobuf . `google.golang.org/protobuf/proto`/.Int64scip-go gomod google.golang.org/protobuf . `google.golang.org/protobuf/proto`/Int64().(longVarint), |
| 29 | Fixed32scip-go gomod google.golang.org/protobuf . `google.golang.org/protobuf/internal/testprotos/lazy/lazy_opaque`/Node_builder#Fixed32.: protoscip-go gomod google.golang.org/protobuf . `google.golang.org/protobuf/proto`/.Uint32scip-go gomod google.golang.org/protobuf . `google.golang.org/protobuf/proto`/Uint32().(shortVarint), |
| 30 | Fixed64scip-go gomod google.golang.org/protobuf . `google.golang.org/protobuf/internal/testprotos/lazy/lazy_opaque`/Node_builder#Fixed64.: protoscip-go gomod google.golang.org/protobuf . `google.golang.org/protobuf/proto`/.Uint64scip-go gomod google.golang.org/protobuf . `google.golang.org/protobuf/proto`/Uint64().(longVarint), |
| 31 | Sfixed32scip-go gomod google.golang.org/protobuf . `google.golang.org/protobuf/internal/testprotos/lazy/lazy_opaque`/Node_builder#Sfixed32.: protoscip-go gomod google.golang.org/protobuf . `google.golang.org/protobuf/proto`/.Int32scip-go gomod google.golang.org/protobuf . `google.golang.org/protobuf/proto`/Int32().(shortVarint), |
| 32 | Sfixed64scip-go gomod google.golang.org/protobuf . `google.golang.org/protobuf/internal/testprotos/lazy/lazy_opaque`/Node_builder#Sfixed64.: protoscip-go gomod google.golang.org/protobuf . `google.golang.org/protobuf/proto`/.Int64scip-go gomod google.golang.org/protobuf . `google.golang.org/protobuf/proto`/Int64().(longVarint), |
| 33 | Floatscip-go gomod google.golang.org/protobuf . `google.golang.org/protobuf/internal/testprotos/lazy/lazy_opaque`/Node_builder#Float.: protoscip-go gomod google.golang.org/protobuf . `google.golang.org/protobuf/proto`/.Float32scip-go gomod google.golang.org/protobuf . `google.golang.org/protobuf/proto`/Float32().(23.42), |
| 34 | Doublescip-go gomod google.golang.org/protobuf . `google.golang.org/protobuf/internal/testprotos/lazy/lazy_opaque`/Node_builder#Double.: protoscip-go gomod google.golang.org/protobuf . `google.golang.org/protobuf/proto`/.Float64scip-go gomod google.golang.org/protobuf . `google.golang.org/protobuf/proto`/Float64().(23.42), |
| 35 | Boolscip-go gomod google.golang.org/protobuf . `google.golang.org/protobuf/internal/testprotos/lazy/lazy_opaque`/Node_builder#Bool.: protoscip-go gomod google.golang.org/protobuf . `google.golang.org/protobuf/proto`/.Boolscip-go gomod google.golang.org/protobuf . `google.golang.org/protobuf/proto`/Bool().(true), |
| 36 | Stringscip-go gomod google.golang.org/protobuf . `google.golang.org/protobuf/internal/testprotos/lazy/lazy_opaque`/Node_builder#String.: protoscip-go gomod google.golang.org/protobuf . `google.golang.org/protobuf/proto`/.Stringscip-go gomod google.golang.org/protobuf . `google.golang.org/protobuf/proto`/String().("hello"), |
| 37 | Bytesscip-go gomod google.golang.org/protobuf . `google.golang.org/protobuf/internal/testprotos/lazy/lazy_opaque`/Node_builder#Bytes.: []byte("world"), |
| 38 | }.Buildscip-go gomod google.golang.org/protobuf . `google.golang.org/protobuf/internal/testprotos/lazy/lazy_opaque`/Node_builder#Build().() |
| 39 | if nesting > 0 { |
| 40 | msg.SetNestedscip-go gomod google.golang.org/protobuf . `google.golang.org/protobuf/internal/testprotos/lazy/lazy_opaque`/Node#SetNested().(testMessageLinkedscip-go gomod google.golang.org/protobuf . `google.golang.org/protobuf/proto_test`/testMessageLinked().(nesting - 1)) |
| 41 | } |
| 42 | return msg |
| 43 | } |
| 44 | |
| 45 | // A higher nesting level than 15 messages deep does not result in (relative) |
| 46 | // performance changes. In other words, the full effect of lazy decoding is |
| 47 | // visible with a nesting level of 15 messages deep. Lower nesting levels (like |
| 48 | // 5 messages deep) also show significant improvement. |
| 49 | const nestingscip-go gomod google.golang.org/protobuf . `google.golang.org/protobuf/proto_test`/nesting. = 15 |
| 50 | |
| 51 | func BenchmarkUnmarshalscip-go gomod google.golang.org/protobuf . `google.golang.org/protobuf/proto_test`/BenchmarkUnmarshal().(b *testingscip-go gomod github.com/golang/go/src go1.23 testing/.Bscip-go gomod github.com/golang/go/src go1.23 testing/B#) { |
| 52 | encoded, err := protoscip-go gomod google.golang.org/protobuf . `google.golang.org/protobuf/proto`/.Marshalscip-go gomod google.golang.org/protobuf . `google.golang.org/protobuf/proto`/Marshal().(testMessageLinkedscip-go gomod google.golang.org/protobuf . `google.golang.org/protobuf/proto_test`/testMessageLinked().(nestingscip-go gomod google.golang.org/protobuf . `google.golang.org/protobuf/proto_test`/nesting.)) |
| 53 | if err != nil { |
| 54 | b.Fatalscip-go gomod github.com/golang/go/src go1.23 testing/common#Fatal().(err) |
| 55 | } |
| 56 | |
| 57 | for _, tt := range []struct { |
| 58 | desc string |
| 59 | uopts protoscip-go gomod google.golang.org/protobuf . `google.golang.org/protobuf/proto`/.UnmarshalOptionsscip-go gomod google.golang.org/protobuf . `google.golang.org/protobuf/proto`/UnmarshalOptions# |
| 60 | }{ |
| 61 | { |
| 62 | desc: "lazy", |
| 63 | uopts: protoscip-go gomod google.golang.org/protobuf . `google.golang.org/protobuf/proto`/.UnmarshalOptionsscip-go gomod google.golang.org/protobuf . `google.golang.org/protobuf/proto`/UnmarshalOptions#{}, |
| 64 | }, |
| 65 | |
| 66 | // When running the benchmark directly, print lazy vs. nolazy in the |
| 67 | // same run. When using the benchstat tool, you can compare lazy |
| 68 | // vs. nolazy by running only the lazy variant and disabling lazy |
| 69 | // decoding with the -test_lazy_unmarshal command-line flag: |
| 70 | // |
| 71 | // benchstat \ |
| 72 | // nolazy=<(go test -run=^$ -bench=Unmarshal/^lazy -count=6) \ |
| 73 | // lazy=<(go test -run=^$ -bench=Unmarshal/^lazy -count=6 -test_lazy_unmarshal) |
| 74 | { |
| 75 | desc: "nolazy", |
| 76 | uopts: protoscip-go gomod google.golang.org/protobuf . `google.golang.org/protobuf/proto`/.UnmarshalOptionsscip-go gomod google.golang.org/protobuf . `google.golang.org/protobuf/proto`/UnmarshalOptions#{ |
| 77 | NoLazyDecodingscip-go gomod google.golang.org/protobuf . `google.golang.org/protobuf/proto`/UnmarshalOptions#NoLazyDecoding.: true, |
| 78 | }, |
| 79 | }, |
| 80 | } { |
| 81 | b.Runscip-go gomod github.com/golang/go/src go1.23 testing/B#Run().(tt.desc, func(b *testingscip-go gomod github.com/golang/go/src go1.23 testing/.Bscip-go gomod github.com/golang/go/src go1.23 testing/B#) { |
| 82 | b.ResetTimerscip-go gomod github.com/golang/go/src go1.23 testing/B#ResetTimer().() |
| 83 | b.ReportAllocsscip-go gomod github.com/golang/go/src go1.23 testing/B#ReportAllocs().() |
| 84 | for i := 0; i < b.Nscip-go gomod github.com/golang/go/src go1.23 testing/B#N.; i++ { |
| 85 | out := &lazyopaquepbscip-go gomod google.golang.org/protobuf . `google.golang.org/protobuf/internal/testprotos/lazy/lazy_opaque`/.Nodescip-go gomod google.golang.org/protobuf . `google.golang.org/protobuf/internal/testprotos/lazy/lazy_opaque`/Node#{} |
| 86 | if err := tt.uopts.Unmarshalscip-go gomod google.golang.org/protobuf . `google.golang.org/protobuf/proto`/UnmarshalOptions#Unmarshal().(encoded, out); err != nil { |
| 87 | b.Fatalfscip-go gomod github.com/golang/go/src go1.23 testing/common#Fatalf().("can't unmarshal message: %v", err) |
| 88 | } |
| 89 | } |
| 90 | }) |
| 91 | } |
| 92 | } |