-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
types/Coin: compile and reuse Regexps to reduce massive RAM+CPU burn (#…
…7989) * types/Coin: compile and reuse Regexps to reduce massive RAM+CPU burn Fixes a resource (CPU+RAM) burn from recompiling Regexps which would consume 1.2+MiB of RAM and 1.1ms per ParseCoin invocation, which means that 1,000 invocations would consume 1.2GiB of RAM and >1second. With the change, ParseCoin now takes 3.2us down from 1.1ms, with dramatic results: name old time/op new time/op delta ParseCoin-8 1.04ms ± 4% 0.00ms ± 1% -99.69% (p=0.000 n=9+10) name old alloc/op new alloc/op delta ParseCoin-8 1.20MB ± 0% 0.00MB ± 0% -99.94% (p=0.000 n=10+10) name old allocs/op new allocs/op delta ParseCoin-8 17.8k ± 0% 0.0k ± 0% -99.89% (p=0.000 n=10+10) Fixes #7986. * replace the CoinDenomRegex variable with SetCoinDenomRegex() (#7998) Co-authored-by: Federico Kunze <31522760+fedekunze@users.noreply.github.com> Co-authored-by: Alessio Treglia <alessio@tendermint.com>
- Loading branch information
1 parent
a7296d3
commit 6344d62
Showing
6 changed files
with
54 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package types_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/cosmos/cosmos-sdk/types" | ||
) | ||
|
||
var coinStrs = []string{ | ||
"2000ATM", | ||
"5000AMX", | ||
"192XXX", | ||
"1e9BTC", | ||
} | ||
|
||
func BenchmarkParseCoin(b *testing.B) { | ||
var blankCoin types.Coin | ||
b.ReportAllocs() | ||
for i := 0; i < b.N; i++ { | ||
for _, coinStr := range coinStrs { | ||
coin, err := types.ParseCoin(coinStr) | ||
if err != nil { | ||
b.Fatal(err) | ||
} | ||
if coin == blankCoin { | ||
b.Fatal("Unexpectedly returned a blank coin") | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters