cosmos-sdk/types
Cuong Manh Le 3e6c4a6c79
types: make chopPrecisionAndTruncate always non-mutative (#9250)
Currently, chopPrecisionAndTruncate/chopPrecisionAndTruncateNonMutative
are doing the same thing, except that the former do it mutatively.

Looking at the usages of chopPrecisionAndTruncate, the callers must
alloc a new big.Int before calling it anyway, so any mutation will be
done with this new allocated big.Int, instead of input argument.

So, by making chopPrecisionAndTruncate always non-mutative, we simplify
the code, and make the code faster, less allocation in some benchmarks.

Benchmark for github.com/cosmos/cosmos-sdk/types:

name                                           old time/op    new time/op    delta
CoinsAdditionIntersect/sizes:_A_1,_B_1-8         35.4ns ± 0%    35.1ns ± 0%  -0.92%  (p=0.008 n=5+5)
CoinsAdditionIntersect/sizes:_A_5,_B_5-8          366ns ± 1%     363ns ± 1%    ~     (p=0.056 n=5+5)
CoinsAdditionIntersect/sizes:_A_5,_B_20-8         557ns ± 0%     553ns ± 0%  -0.64%  (p=0.008 n=5+5)
CoinsAdditionIntersect/sizes:_A_1,_B_1000-8      6.17µs ± 0%    6.18µs ± 0%    ~     (p=0.151 n=5+5)
CoinsAdditionIntersect/sizes:_A_2,_B_1000-8      6.30µs ± 0%    6.32µs ± 0%  +0.23%  (p=0.008 n=5+5)
CoinsAdditionNoIntersect/sizes:_A_1,_B_1-8       30.2ns ± 0%    29.9ns ± 1%  -0.98%  (p=0.008 n=5+5)
CoinsAdditionNoIntersect/sizes:_A_5,_B_5-8        226ns ± 0%     224ns ± 0%  -0.54%  (p=0.008 n=5+5)
CoinsAdditionNoIntersect/sizes:_A_5,_B_20-8       373ns ± 0%     372ns ± 0%  -0.26%  (p=0.040 n=5+5)
CoinsAdditionNoIntersect/sizes:_A_1,_B_1000-8    6.21µs ± 0%    6.22µs ± 0%    ~     (p=0.421 n=5+5)
CoinsAdditionNoIntersect/sizes:_A_2,_B_1000-8    6.22µs ± 0%    6.23µs ± 0%    ~     (p=0.286 n=5+5)
CoinsAdditionNoIntersect/sizes:_A_1000,_B_2-8    6.22µs ± 0%    6.23µs ± 0%    ~     (p=0.143 n=5+5)
ParseCoin-8                                      5.46µs ± 0%    5.39µs ± 0%  -1.26%  (p=0.008 n=5+5)
UintMarshal-8                                    1.01µs ± 0%    1.00µs ± 0%  -1.10%  (p=0.008 n=5+5)
IntMarshal-8                                      764ns ± 0%     758ns ± 1%  -0.79%  (p=0.024 n=5+5)
MarshalTo-8                                       192ns ± 0%     190ns ± 0%  -1.03%  (p=0.008 n=5+5)

name                                           old alloc/op   new alloc/op   delta
CoinsAdditionIntersect/sizes:_A_1,_B_1-8          32.0B ± 0%     32.0B ± 0%    ~     (all equal)
CoinsAdditionIntersect/sizes:_A_5,_B_5-8           520B ± 0%      520B ± 0%    ~     (all equal)
CoinsAdditionIntersect/sizes:_A_5,_B_20-8        1.38kB ± 0%    1.38kB ± 0%    ~     (all equal)
CoinsAdditionIntersect/sizes:_A_1,_B_1000-8      49.2kB ± 0%    49.2kB ± 0%    ~     (all equal)
CoinsAdditionIntersect/sizes:_A_2,_B_1000-8      49.3kB ± 0%    49.3kB ± 0%    ~     (all equal)
CoinsAdditionNoIntersect/sizes:_A_1,_B_1-8        24.0B ± 0%     24.0B ± 0%    ~     (all equal)
CoinsAdditionNoIntersect/sizes:_A_5,_B_5-8         488B ± 0%      488B ± 0%    ~     (all equal)
CoinsAdditionNoIntersect/sizes:_A_5,_B_20-8      1.35kB ± 0%    1.35kB ± 0%    ~     (all equal)
CoinsAdditionNoIntersect/sizes:_A_1,_B_1000-8    49.2kB ± 0%    49.2kB ± 0%    ~     (all equal)
CoinsAdditionNoIntersect/sizes:_A_2,_B_1000-8    49.2kB ± 0%    49.2kB ± 0%    ~     (all equal)
CoinsAdditionNoIntersect/sizes:_A_1000,_B_2-8    49.2kB ± 0%    49.2kB ± 0%    ~     (all equal)
ParseCoin-8                                      2.21kB ± 0%    2.21kB ± 0%    ~     (all equal)
UintMarshal-8                                      392B ± 0%      392B ± 0%    ~     (all equal)
IntMarshal-8                                       168B ± 0%      168B ± 0%    ~     (all equal)
MarshalTo-8                                       80.0B ± 0%     80.0B ± 0%    ~     (all equal)

name                                           old allocs/op  new allocs/op  delta
CoinsAdditionIntersect/sizes:_A_1,_B_1-8           1.00 ± 0%      1.00 ± 0%    ~     (all equal)
CoinsAdditionIntersect/sizes:_A_5,_B_5-8           12.0 ± 0%      12.0 ± 0%    ~     (all equal)
CoinsAdditionIntersect/sizes:_A_5,_B_20-8          14.0 ± 0%      14.0 ± 0%    ~     (all equal)
CoinsAdditionIntersect/sizes:_A_1,_B_1000-8        3.00 ± 0%      3.00 ± 0%    ~     (all equal)
CoinsAdditionIntersect/sizes:_A_2,_B_1000-8        6.00 ± 0%      6.00 ± 0%    ~     (all equal)
CoinsAdditionNoIntersect/sizes:_A_1,_B_1-8         1.00 ± 0%      1.00 ± 0%    ~     (all equal)
CoinsAdditionNoIntersect/sizes:_A_5,_B_5-8         5.00 ± 0%      5.00 ± 0%    ~     (all equal)
CoinsAdditionNoIntersect/sizes:_A_5,_B_20-8        6.00 ± 0%      6.00 ± 0%    ~     (all equal)
CoinsAdditionNoIntersect/sizes:_A_1,_B_1000-8      3.00 ± 0%      3.00 ± 0%    ~     (all equal)
CoinsAdditionNoIntersect/sizes:_A_2,_B_1000-8      4.00 ± 0%      4.00 ± 0%    ~     (all equal)
CoinsAdditionNoIntersect/sizes:_A_1000,_B_2-8      3.00 ± 0%      3.00 ± 0%    ~     (all equal)
ParseCoin-8                                        71.0 ± 0%      71.0 ± 0%    ~     (all equal)
UintMarshal-8                                      25.0 ± 0%      25.0 ± 0%    ~     (all equal)
IntMarshal-8                                       18.0 ± 0%      18.0 ± 0%    ~     (all equal)
MarshalTo-8                                        2.00 ± 0%      2.00 ± 0%    ~     (all equal)

name                                           old speed      new speed      delta
UintMarshal-8                                  6.91MB/s ± 0%  6.99MB/s ± 0%  +1.04%  (p=0.008 n=5+5)
IntMarshal-8                                   9.16MB/s ± 0%  9.23MB/s ± 1%  +0.83%  (p=0.032 n=5+5)

Benchmark for github.com/cosmos/cosmos-sdk/x/mint/types:

name              old time/op    new time/op    delta
BlockProvision-8     263ns ± 0%     257ns ± 0%   -2.23%  (p=0.008 n=5+5)

name              old alloc/op   new alloc/op   delta
BlockProvision-8      112B ± 0%      104B ± 0%   -7.14%  (p=0.008 n=5+5)

name              old allocs/op  new allocs/op  delta
BlockProvision-8      6.00 ± 0%      5.00 ± 0%  -16.67%  (p=0.008 n=5+5)

Fixes #9249

Co-authored-by: Emmanuel T Odeke <emmanuel@orijtech.com>
2021-05-04 00:32:15 +01:00
..
address ADR-28 derive address functions (#9088) 2021-04-15 21:32:45 +00:00
bech32 codec: Rename codec and marshaler interfaces (#9226) 2021-04-29 10:46:22 +00:00
errors Remove bech32 PubKey support (#7477) 2021-03-25 14:53:22 +00:00
grpc Add GRPCBlockHeightHeader in clientCtx gRPC requests (#7021) 2020-08-12 14:42:10 +00:00
kv store/cachekv: use typed types/kv.List instead of container/list.List (#8811) 2021-03-08 09:16:23 -08:00
module codec: Rename codec and marshaler interfaces (#9226) 2021-04-29 10:46:22 +00:00
msgservice Remove ServiceMsgs from ADR-031 (#9139) 2021-04-30 11:00:47 +00:00
query codec: Rename codec and marshaler interfaces (#9226) 2021-04-29 10:46:22 +00:00
rest cli: convert coins to smallest unit registered (#7777) 2020-11-16 11:34:54 +00:00
simulation Remove ServiceMsgs from ADR-031 (#9139) 2021-04-30 11:00:47 +00:00
tx Remove ServiceMsgs from ADR-031 (#9139) 2021-04-30 11:00:47 +00:00
abci.go Merge PR #4613: Only change rootmultistore hash when substore hashes change 2019-06-25 13:11:22 -04:00
abci.pb.go Update gogo proto deps with v1.3.2 security fixes (#8350) 2021-01-15 19:45:34 +00:00
address.go cosmos-reflection: extend to support writing by reflection clients (#8965) 2021-04-07 00:33:54 +01:00
address_race_test.go cosmos-reflection: extend to support writing by reflection clients (#8965) 2021-04-07 00:33:54 +01:00
address_test.go Remove bech32 PubKey support (#7477) 2021-03-25 14:53:22 +00:00
bench_test.go run make format (#8642) 2021-02-19 15:02:53 +00:00
codec.go Remove ServiceMsgs from ADR-031 (#9139) 2021-04-30 11:00:47 +00:00
coin.go add AddAmount/SubAmount methods to sdk.Coin (#9091) 2021-04-13 15:01:21 +00:00
coin.pb.go Update gogo proto deps with v1.3.2 security fixes (#8350) 2021-01-15 19:45:34 +00:00
coin_benchmark_test.go all: skip noisy/faulty benchmarks + add b.ReportAllocs for every benchmark (#8856) 2021-03-11 15:59:13 +00:00
coin_test.go codec: Rename codec and marshaler interfaces (#9226) 2021-04-29 10:46:22 +00:00
config.go Deprecate SetFullFundraiserPath in favor of SetPurpose and SetCoinType (#8629) 2021-02-22 15:14:09 +00:00
config_test.go Deprecate SetFullFundraiserPath in favor of SetPurpose and SetCoinType (#8629) 2021-02-22 15:14:09 +00:00
context.go tendermint: update to rc3 (#6892) 2020-08-14 13:58:53 -04:00
context_test.go Add in-place store migrations (#8485) 2021-02-10 17:49:31 +00:00
dec_coin.go types/Coin: compile and reuse Regexps to reduce massive RAM+CPU burn (#7989) 2020-11-20 19:37:45 +00:00
dec_coin_test.go cli: convert coins to smallest unit registered (#7777) 2020-11-16 11:34:54 +00:00
decimal.go types: make chopPrecisionAndTruncate always non-mutative (#9250) 2021-05-04 00:32:15 +01:00
decimal_internal_test.go types: tests -> test suites migration (#7400) 2020-09-28 12:46:49 +02:00
decimal_test.go types: make NewDecFromStr returns error for too large decimal (#9157) 2021-04-22 11:21:27 +01:00
denom.go cli: convert coins to smallest unit registered (#7777) 2020-11-16 11:34:54 +00:00
denom_internal_test.go cli: convert coins to smallest unit registered (#7777) 2020-11-16 11:34:54 +00:00
events.go Rename GRPCRouter (#8079) 2020-12-04 15:06:50 +00:00
events_test.go init: Implement ADR 032 typed events (#7564) 2020-10-28 11:02:39 +00:00
handler.go fix types.ChainAnteDecorators() panic (#5742) 2020-03-03 16:57:33 +00:00
handler_test.go types: tests -> test suites migration (#7405) 2020-09-29 13:29:10 +02:00
int.go Minor docs update to sdk.Int (#9126) 2021-04-16 08:43:49 +00:00
int_internal_test.go types: tests -> test suites migration (#7405) 2020-09-29 13:29:10 +02:00
int_test.go types: add Abs() method to sdk.Int (#8963) 2021-03-24 16:33:55 +00:00
invariant.go Merge PR #4832: print all failed invariants only 2019-08-05 14:21:44 -04:00
invariant_test.go types: tests -> test suites migration (#7405) 2020-09-29 13:29:10 +02:00
proto.go linter: enable nolintlint (#6162) 2020-05-08 08:46:12 +00:00
queryable.go Merge PR #5421: Refactor Error Handling 2019-12-27 12:57:54 -05:00
result.go Refactor x/bank according to ADR 031 (#7520) 2020-10-13 09:05:46 +00:00
result_test.go Refactor x/bank according to ADR 031 (#7520) 2020-10-13 09:05:46 +00:00
router.go Remove duplicated Module.Route calls (#7716) 2020-10-29 22:01:09 +00:00
router_test.go types: tests -> test suites migration (#7405) 2020-09-29 13:29:10 +02:00
staking.go simply default power reduction definition 2021-03-21 20:29:39 -04:00
staking_test.go on-chain power reduction param conversion basic work 2021-01-27 20:24:00 +10:00
store.go init commit (#7360) 2020-09-22 19:46:48 -04:00
store_test.go types: tests -> test suites migration (#7405) 2020-09-29 13:29:10 +02:00
tx_msg.go Remove ServiceMsgs from ADR-031 (#9139) 2021-04-30 11:00:47 +00:00
tx_msg_test.go types: tests -> test suites migration (#7405) 2020-09-29 13:29:10 +02:00
uint.go various linter fixes (#8666) 2021-02-23 08:46:01 +00:00
uint_internal_test.go types: tests -> test suites migration (#7405) 2020-09-29 13:29:10 +02:00
uint_test.go types: use (*math/big.Int).BitLen() == 0 to check if value is 0 (#8580) 2021-02-15 10:53:10 +00:00
utils.go tendermint: update to rc3 (#6892) 2020-08-14 13:58:53 -04:00
utils_test.go types: tests -> test suites migration (#7405) 2020-09-29 13:29:10 +02:00