Commit 342c7ce
committed
Optimize decode for ~27-75% better performance.
This optimizes the decode function to significantly improve its
performance.
It accomplishes this by making the following overall changes:
- uses uint32 words to reduce the total number of multiplications that
need to be done
- changes to a more efficient total size approximation which reduces the
overall internal buffer sizes
- employs a stack allocated array for the internal array when working
with typical sizes
- skips all leading zeros
Note that there is in an additional heap allocation for larger inputs as
compared to the existing code, but the overall perf gains on such large
inputs is even better than on the more typical smaller inputs due to
fewer overall calculations which more than makes up for it as can be
seen in the benchmarks.
Finally, in an effort to help ensure correctness, the new code was fuzz
tested for 24 hours on 16 cores for a total effective fuzz time of 384
hours with no issues found.
name old time/op new time/op delta
--------------------------------------------------------------------
Base58Decode/20_bytes_addrhash 240ns ± 2% 133ns ± 1% -44.34%
Base58Decode/53_chars_wif 798ns ± 2% 299ns ± 1% -62.51%
Base58Decode/111_chars_extkey 3.54µs ± 1% 1.01µs ± 2% -71.51%
Base58Decode/50_zeros 128ns ± 1% 69ns ± 1% -46.14%
Base58Decode/200_bytes_large 20.0µs ± 1% 5.1µs ± 0% -74.29%
CheckDecode 677ns ± 1% 493ns ± 0% -27.24%
name old allocs/op new allocs/op delta
----------------------------------------------------------------------
Base58Decode/20_bytes_addrhash 1.00 ± 0% 1.00 ± 0% ~
Base58Decode/53_chars_wif 1.00 ± 0% 1.00 ± 0% ~
Base58Decode/111_chars_extkey 1.00 ± 0% 1.00 ± 0% ~
Base58Decode/50_zeros 1.00 ± 0% 1.00 ± 0% ~
Base58Decode/200_bytes_large 1.00 ± 0% 2.00 ± 0% +100.00%
CheckDecode 1.00 ± 0% 1.00 ± 0% ~1 parent a94b676 commit 342c7ce
1 file changed
Lines changed: 79 additions & 28 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | | - | |
| 2 | + | |
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
8 | 12 | | |
9 | 13 | | |
10 | 14 | | |
| |||
13 | 17 | | |
14 | 18 | | |
15 | 19 | | |
16 | | - | |
17 | | - | |
18 | | - | |
19 | | - | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
20 | 67 | | |
21 | | - | |
22 | | - | |
23 | | - | |
24 | | - | |
25 | | - | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
26 | 72 | | |
27 | | - | |
| 73 | + | |
28 | 74 | | |
29 | 75 | | |
30 | 76 | | |
31 | 77 | | |
32 | | - | |
33 | | - | |
34 | | - | |
35 | | - | |
36 | | - | |
37 | | - | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
38 | 82 | | |
39 | | - | |
40 | | - | |
41 | | - | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
42 | 86 | | |
43 | 87 | | |
44 | 88 | | |
45 | | - | |
46 | | - | |
47 | | - | |
48 | | - | |
49 | | - | |
50 | | - | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
51 | 98 | | |
52 | | - | |
53 | | - | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
54 | 104 | | |
| 105 | + | |
55 | 106 | | |
56 | 107 | | |
57 | 108 | | |
| |||
0 commit comments