Optimize code paths
This PR significantly increases the speed of encoding by optimizing blocklist checking, which were very expensive. It also adds a simple performance testing script.
The performance improvements are made possible by pre-filtering the blocklist into groups:
* Words that must be matched exactly (3 characters long)
* Words that must be matched at the start or end of the ID (those containing numbers)
* Words that can be matched anywhere in the ID
This pre-filtering allows blocklist checks to eliminate almost all looping in Python.
Here is the output of the performance testing for the `main` branch before this PR, and for this PR branch:
```
Before changes: After changes:
$ python assets/performance.py $ python assets/performance.py
Iterations: 100,000 Iterations: 100,000
Instantiate: 23.493 Instantiate: 1.447
Encode [0]: 2.245 Encode [0]: 0.211
Encode [0, 1, 2]: 18.278 Encode [0, 1, 2]: 2.798
Decode 'bM': 0.350 Decode 'bM': 0.366
Decode 'rSCtlB': 2.490 Decode 'rSCtlB': 2.621
```
As you can see, IDs can be encoded ~85% faster. Although it's not reflected in these performance tests, if a non-default alphabet or blocklist is used, instantiation will require more up-front computation to filter the blocklist, but the encoding will still be faster.
Decode times are not affected by these changes.
合并状态:未合并 关闭于 2024-08-06 8 条评论