Skip to content

Commit

Permalink
feat(lazyproto): add lazyproto Decoder optimization
Browse files Browse the repository at this point in the history
Creates a reusable Decoder struct for individual definitions.
Adds performance optimizations for reusing DecodeResult structs
  • Loading branch information
tlyons-cs authored and wmorgan6796 committed Jan 17, 2025
1 parent 0f33464 commit fbae855
Show file tree
Hide file tree
Showing 6 changed files with 1,401 additions and 294 deletions.
35 changes: 33 additions & 2 deletions example/proto2/googlev2_benchmarks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,46 @@ func BenchmarkLazyDecodeGoogleV2(b *testing.B) {
evt = createGoogleV2Event(b)
def = lazyproto.NewDef(1, 2, 3, 4)
)
_ = def.NestedTag(100, 1)
_ = def.NestedTag(100, 1, 2, 3, 4, 5, 6, 7, 8, 9)
data, _ := proto.Marshal(evt)
b.ResetTimer()
for n := 0; n < b.N; n++ {
r, _ := lazyproto.Decode(data, def)
r, _ := lazyproto.Decode(data, def) //nolint: staticcheck // benchmarking deprecated function to demonstrate the difference
_ = r.Close()
}
}

func BenchmarkLazyDecoder(b *testing.B) {
var (
evt = createGoogleV2Event(b)
def = lazyproto.NewDef(1, 2, 3, 4)
)
_ = def.NestedTag(100, 1, 2, 3, 4, 5, 6, 7, 8, 9)
data, _ := proto.Marshal(evt)
b.Run("safe", func(b *testing.B) {
dec, _ := lazyproto.NewDecoder(def, lazyproto.WithMaxBufferSize(3))
b.ResetTimer()
for n := 0; n < b.N; n++ {
r, _ := dec.Decode(data)
nest, _ := r.NestedResult(100)
discardStrings, _ = nest.StringValues(4)
_ = r.Close()
}
})
b.Run("unsafe", func(b *testing.B) {
dec, _ := lazyproto.NewDecoder(def, lazyproto.WithMode(csproto.DecoderModeFast), lazyproto.WithMaxBufferSize(3))
b.ResetTimer()
for n := 0; n < b.N; n++ {
r, _ := dec.Decode(data)
nest, _ := r.NestedResult(100)
discardStrings, _ = nest.StringValues(4)
_ = r.Close()
}
})
}

var discardStrings []string

func createGoogleV2Event(t interface{ Errorf(string, ...interface{}) }) *googlev2.BaseEvent {
eventType := googlev2.EventType_EVENT_TYPE_ONE
baseEvent := googlev2.BaseEvent{
Expand Down
34 changes: 32 additions & 2 deletions example/proto3/googlev2_benchmarks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,45 @@ func BenchmarkLazyDecodeGoogleV2(b *testing.B) {
evt = createGoogleV2Event()
def = lazyproto.NewDef(1)
)
_ = def.NestedTag(5, 1)
_ = def.NestedTag(5, 1, 2, 3, 4)
data, _ := proto.Marshal(evt)
b.ResetTimer()
for n := 0; n < b.N; n++ {
r, _ := lazyproto.Decode(data, def)
r, _ := lazyproto.Decode(data, def) //nolint: staticcheck // benchmarking deprecated function to demonstrate the difference
_ = r.Close()
}
}

func BenchmarkLazyDecoder(b *testing.B) {
var (
evt = createGoogleV2Event()
def = lazyproto.NewDef(1, 2, 3, 4, 5, 6, 7, 8, 9)
)
_ = def.NestedTag(5, 1, 2, 3, 4)
data, _ := proto.Marshal(evt)
b.Run("safe", func(b *testing.B) {
dec, _ := lazyproto.NewDecoder(def)
b.ResetTimer()
for n := 0; n < b.N; n++ {
r, _ := dec.Decode(data)
discardStrings, _ = r.StringValues(4)
_ = r.Close()
}
})

b.Run("unsafe", func(b *testing.B) {
dec, _ := lazyproto.NewDecoder(def, lazyproto.WithMode(csproto.DecoderModeFast))
b.ResetTimer()
for n := 0; n < b.N; n++ {
r, _ := dec.Decode(data)
discardStrings, _ = r.StringValues(4)
_ = r.Close()
}
})
}

var discardStrings []string

func createGoogleV2Event() *googlev2.TestEvent {
event := googlev2.TestEvent{
Name: "test-event",
Expand Down
Loading

0 comments on commit fbae855

Please sign in to comment.