cosmos-sdk/crypto
Emmanuel T Odeke 00ad3ecedc
perf: crypto/types: use native slice index producing for loop in (*CompactBitArry).NumTrueBitsBefore (#14000)
Noticed while examining a bunch of profiles, that the for loop inside
(*CompactBitArry).NumTrueBitsBefore unnecessarily consumed a bunch of
time:
```shell
     7.55s      9.88s (flat, cum) 93.38% of Total
     240ms      250ms     88:func (bA *CompactBitArray) NumTrueBitsBefore(index int) int {
         .          .     89:	onesCount := 0
      70ms      340ms     90:	max := bA.Count()
      70ms       70ms     91:	if index > max {
         .          .     92:		index = max
         .          .     93:	}
         .          .     94:	// below we iterate over the bytes then over bits (in low endian) and count bits set to 1
     2.54s      2.76s     95:	for elem := 0; elem < len(bA.Elems); elem++ {
```

but we can use the native for loop that produces indices while iterating
over slices. Just by simply changing the form results in an improvement

```shell
     7.50s      9.95s (flat, cum) 94.94% of Total
     240ms      320ms     88:func (bA *CompactBitArray) NumTrueBitsBefore(index int) int {
         .          .     89:	onesCount := 0
     170ms      420ms     90:	max := bA.Count()
      90ms      100ms     91:	if index > max {
         .          .     92:		index = max
         .          .     93:	}
         .          .     94:	// below we iterate over the bytes then over bits (in low endian) and count bits set to 1
     1.49s      1.62s     95:	for elem := range bA.Elems {
```

and an improvement in CPU time

```shell
$ benchstat before.txt after.txt
name                     old time/op    new time/op    delta
NumTrueBitsBefore/new-8    13.3ns ± 1%    12.5ns ± 1%  -6.07%  (p=0.000 n=10+10)

name                     old alloc/op   new alloc/op   delta
NumTrueBitsBefore/new-8     0.00B          0.00B         ~     (all equal)

name                     old allocs/op  new allocs/op  delta
NumTrueBitsBefore/new-8      0.00           0.00         ~     (all equal)
```

Fixes #13999
2022-11-24 10:46:04 +00:00
..
codec refactor!: Keyring migration (#9695) 2021-09-20 12:02:15 +00:00
hd feat: Add proto annotations for Amino JSON (#13501) 2022-11-07 22:51:51 +00:00
keyring docs: improve keyring package docs (#13835) 2022-11-10 22:03:11 +01:00
keys refactor: (secp256k1) cleaner way to check for lower S form signatures (#13884) 2022-11-23 17:16:00 +00:00
ledger ci: fix sims gh actions and build error (#13175) 2022-09-07 16:01:33 +00:00
types perf: crypto/types: use native slice index producing for loop in (*CompactBitArry).NumTrueBitsBefore (#14000) 2022-11-24 10:46:04 +00:00
xsalsa20symmetric chore: gofumpt (#11839) 2022-05-19 10:55:27 +02:00
armor_test.go refactor(crypto): remove dependency on simapp from tests (#12614) 2022-07-18 14:45:09 +00:00
armor.go refactor: migrate to cosmos/gogoproto (#13070) 2022-09-08 17:27:48 +00:00
bcrypt_readme.md docs: Improve markdownlint configuration (#11104) 2022-02-10 12:07:01 +00:00