Skip to content

Instantly share code, notes, and snippets.

@misberner
Created August 16, 2022 15:17
Show Gist options
  • Save misberner/69a28f5f0206a6c98b66608a6aaaace4 to your computer and use it in GitHub Desktop.
Save misberner/69a28f5f0206a6c98b66608a6aaaace4 to your computer and use it in GitHub Desktop.
package conformance
import (
"google.golang.org/protobuf/proto"
"testing"
)
const (
numObjects = 1000
)
func BenchmarkOptimizedClone(b *testing.B) {
x := &TestAllTypesProto3{}
for i := 0; i < numObjects; i++ {
x.RepeatedNestedMessage = append(x.RepeatedNestedMessage, &TestAllTypesProto3_NestedMessage{A: int32(i)})
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = x.CloneVT()
}
}
func BenchmarkProtoCloneWholeObject(b *testing.B) {
x := &TestAllTypesProto3{}
for i := 0; i < numObjects; i++ {
x.RepeatedNestedMessage = append(x.RepeatedNestedMessage, &TestAllTypesProto3_NestedMessage{A: int32(i)})
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = proto.Clone(x)
}
}
func BenchmarkProtoCloneIndividualObjects(b *testing.B) {
x := &TestAllTypesProto3{}
for i := 0; i < numObjects; i++ {
x.RepeatedNestedMessage = append(x.RepeatedNestedMessage, &TestAllTypesProto3_NestedMessage{A: int32(i)})
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
y := &TestAllTypesProto3{
RepeatedNestedMessage: make([]*TestAllTypesProto3_NestedMessage, len(x.RepeatedNestedMessage)),
}
for j, v := range x.RepeatedNestedMessage {
y.RepeatedNestedMessage[j] = proto.Clone(v).(*TestAllTypesProto3_NestedMessage)
}
}
}
@misberner
Copy link
Author

Results:

goos: darwin
goarch: amd64
pkg: github.com/planetscale/vtprotobuf/conformance/internal/conformance
cpu: Intel(R) Core(TM) i9-9980HK CPU @ 2.40GHz
BenchmarkOptimizedClone
BenchmarkOptimizedClone-16                 	   24648	     49900 ns/op
BenchmarkProtoCloneWholeObject
BenchmarkProtoCloneWholeObject-16          	   12634	     87447 ns/op
BenchmarkProtoCloneIndividualObjects
BenchmarkProtoCloneIndividualObjects-16    	    6740	    154005 ns/op
PASS

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment