From e61ee1d389a6b47231c855d96f09aa35488bfc0a Mon Sep 17 00:00:00 2001 From: vastonus Date: Wed, 1 Oct 2025 22:34:05 +0800 Subject: [PATCH] refactor: use b.Loop() to simplify the code and improve performance (#25379) Signed-off-by: vastonus --- crypto/keys/secp256k1/internal/secp256k1/secp256_test.go | 6 ++---- x/auth/keeper/keeper_bench_test.go | 4 +--- x/bank/bench_test.go | 6 ++---- x/mint/types/minter_test.go | 6 +++--- x/upgrade/internal/conv/string_test.go | 2 +- 5 files changed, 9 insertions(+), 15 deletions(-) diff --git a/crypto/keys/secp256k1/internal/secp256k1/secp256_test.go b/crypto/keys/secp256k1/internal/secp256k1/secp256_test.go index 954845d74b..d19fa5dd10 100644 --- a/crypto/keys/secp256k1/internal/secp256k1/secp256_test.go +++ b/crypto/keys/secp256k1/internal/secp256k1/secp256_test.go @@ -222,9 +222,8 @@ func TestRecoverSanity(t *testing.T) { func BenchmarkSign(b *testing.B) { _, seckey := generateKeyPair() msg := csprngEntropy(32) - b.ResetTimer() - for i := 0; i < b.N; i++ { + for b.Loop() { Sign(msg, seckey) } } @@ -233,9 +232,8 @@ func BenchmarkRecover(b *testing.B) { msg := csprngEntropy(32) _, seckey := generateKeyPair() sig, _ := Sign(msg, seckey) - b.ResetTimer() - for i := 0; i < b.N; i++ { + for b.Loop() { RecoverPubkey(msg, sig) } } diff --git a/x/auth/keeper/keeper_bench_test.go b/x/auth/keeper/keeper_bench_test.go index 4033569d5b..2bede5e4af 100644 --- a/x/auth/keeper/keeper_bench_test.go +++ b/x/auth/keeper/keeper_bench_test.go @@ -55,10 +55,8 @@ func BenchmarkAccountMapperSetAccount(b *testing.B) { ctx := app.NewContext(false) - b.ResetTimer() - // assumes b.N < 2**24 - for i := 0; i < b.N; i++ { + for i := 0; b.Loop(); i++ { arr := []byte{byte((i & 0xFF0000) >> 16), byte((i & 0xFF00) >> 8), byte(i & 0xFF)} addr := sdk.AccAddress(arr) acc := accountKeeper.NewAccountWithAddress(ctx, addr) diff --git a/x/bank/bench_test.go b/x/bank/bench_test.go index 908ffa91f1..981cee4241 100644 --- a/x/bank/bench_test.go +++ b/x/bank/bench_test.go @@ -86,13 +86,12 @@ func BenchmarkOneBankSendTxPerBlock(b *testing.B) { // pre-compute all txs txs, err := genSequenceOfTxs(txGen, []sdk.Msg{sendMsg1}, []uint64{0}, []uint64{uint64(0)}, b.N, priv1) require.NoError(b, err) - b.ResetTimer() height := int64(2) // Run this with a profiler, so its easy to distinguish what time comes from // Committing, and what time comes from Check/Deliver Tx. - for i := 0; i < b.N; i++ { + for i := 0; b.Loop(); i++ { _, _, err := baseApp.SimCheck(txEncoder, txs[i]) if err != nil { panic(fmt.Errorf("failed to simulate tx: %w", err)) @@ -144,13 +143,12 @@ func BenchmarkOneBankMultiSendTxPerBlock(b *testing.B) { // pre-compute all txs txs, err := genSequenceOfTxs(txGen, []sdk.Msg{multiSendMsg1}, []uint64{0}, []uint64{uint64(0)}, b.N, priv1) require.NoError(b, err) - b.ResetTimer() height := int64(2) // Run this with a profiler, so its easy to distinguish what time comes from // Committing, and what time comes from Check/Deliver Tx. - for i := 0; i < b.N; i++ { + for i := 0; b.Loop(); i++ { _, _, err := baseApp.SimCheck(txEncoder, txs[i]) if err != nil { panic(fmt.Errorf("failed to simulate tx: %w", err)) diff --git a/x/mint/types/minter_test.go b/x/mint/types/minter_test.go index dbfdf4cd2a..01b81f8117 100644 --- a/x/mint/types/minter_test.go +++ b/x/mint/types/minter_test.go @@ -104,7 +104,7 @@ func BenchmarkBlockProvision(b *testing.B) { minter.AnnualProvisions = math.LegacyNewDec(r1.Int63n(1000000)) // run the BlockProvision function b.N times - for n := 0; n < b.N; n++ { + for b.Loop() { minter.BlockProvision(params) } } @@ -118,7 +118,7 @@ func BenchmarkNextInflation(b *testing.B) { bondedRatio := math.LegacyNewDecWithPrec(1, 1) // run the NextInflationRate function b.N times - for n := 0; n < b.N; n++ { + for b.Loop() { minter.NextInflationRate(params, bondedRatio) } } @@ -132,7 +132,7 @@ func BenchmarkNextAnnualProvisions(b *testing.B) { totalSupply := math.NewInt(100000000000000) // run the NextAnnualProvisions function b.N times - for n := 0; n < b.N; n++ { + for b.Loop() { minter.NextAnnualProvisions(params, totalSupply) } } diff --git a/x/upgrade/internal/conv/string_test.go b/x/upgrade/internal/conv/string_test.go index 974b40fc72..b9459bef57 100644 --- a/x/upgrade/internal/conv/string_test.go +++ b/x/upgrade/internal/conv/string_test.go @@ -35,7 +35,7 @@ func (s *StringSuite) TestUnsafeStrToBytes() { } func BenchmarkUnsafeStrToBytes(b *testing.B) { - for i := 0; i < b.N; i++ { + for i := 0; b.Loop(); i++ { UnsafeStrToBytes(strconv.Itoa(i)) } }