refactor: use b.Loop() to simplify the code and improve performance (#25368)

Signed-off-by: withtimezone <with_timezone@outlook.com>
This commit is contained in:
withtimezone 2025-09-29 23:32:54 +08:00 committed by GitHub
parent 39c56716bd
commit fc6df7e651
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 5 additions and 10 deletions

View File

@ -33,8 +33,7 @@ func BenchmarkLegacyGetSigners(b *testing.B) {
Amount: nil,
}}
b.ResetTimer()
for i := 0; i < b.N; i++ {
for b.Loop() {
_ = msg.GetSigners()
}
}
@ -50,8 +49,7 @@ func BenchmarkProtoreflectGetSigners(b *testing.B) {
Amount: nil,
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
for b.Loop() {
_, err := signingCtx.GetSigners(msg)
if err != nil {
panic(err)
@ -72,8 +70,7 @@ func BenchmarkProtoreflectGetSignersWithUnmarshal(b *testing.B) {
a, err := codectypes.NewAnyWithValue(msg)
require.NoError(b, err)
b.ResetTimer()
for i := 0; i < b.N; i++ {
for b.Loop() {
_, _, err := cdc.GetMsgAnySigners(a)
if err != nil {
panic(err)
@ -97,8 +94,7 @@ func BenchmarkProtoreflectGetSignersDynamicpb(b *testing.B) {
err = protov2.Unmarshal(bz, dynamicmsg)
require.NoError(b, err)
b.ResetTimer()
for i := 0; i < b.N; i++ {
for b.Loop() {
_, err := signingCtx.GetSigners(dynamicmsg)
if err != nil {
panic(err)

View File

@ -165,10 +165,9 @@ func BenchmarkProtoCodecMarshalLengthPrefixed(b *testing.B) {
}),
}
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
for b.Loop() {
blob, err := pCdc.MarshalLengthPrefixed(msg)
if err != nil {
b.Fatal(err)