Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use ReverseWordMap to make O(1) lookups for IsMnemonicValid (#2)
Noticed while auditing a dependency of Cosmos-sdk code from PR cosmos/cosmos-sdk#8099, WordList is loaded at init time, and so is ReverseWordMap. Usually a slice is sufficient for doing linear lookups for the existence of a string. However, given that IsMnemonicValid requires word segements with 12 to 24 lengths, this means that 2048 * ~12 lookups in the worst case, and given that we already at init time load up ReverseWordMap, it is important for us perform a constant time lookup. The data also backs up the speed up with the benchmark results below: ```shell name old time/op new time/op delta IsMnemonicValid-8 24.3µs ± 1% 1.3µs ± 1% -94.64% (p=0.000 n=9+10) name old alloc/op new alloc/op delta IsMnemonicValid-8 576B ± 0% 576B ± 0% ~ (all equal) name old allocs/op new allocs/op delta IsMnemonicValid-8 3.00 ± 0% 3.00 ± 0% ~ (all equal) ```
- Loading branch information