Commit b4daab2
committed
Optimise searching for placeholders
If a string doesn’t have an opening parentheses then we can guarantee
it doesn’t have any placeholders in it.
We can take advantage of this to skip the expensive regex search.
Benchmark for string with no placeholders:
```shell
python -m timeit -s "import random; import string; from notifications_utils.field import Field; s = ''.join(random.choices(string.ascii_uppercase + string.digits + string.whitespace, k=1000))" "Field(s).placeholders"
```
Before
> 50000 loops, best of 5: 7.68 usec per loop
After
> 500000 loops, best of 5: 536 nsec per loop
14× faster
Benchmark for same string with one placeholder right at the end:
```shell
python -m timeit -s "import random; import string; from notifications_utils.field import Field; s = ''.join(random.choices(string.ascii_uppercase + string.digits + string.whitespace, k=1000)) + '((name))'" "Field(s).placeholders"
```
Before
> 50000 loops, best of 5: 9.06 usec per loop
After
> 50000 loops, best of 5: 9.06 usec per loop
No change.1 parent 5811563 commit b4daab2
1 file changed
Lines changed: 1 addition & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
161 | 161 | | |
162 | 162 | | |
163 | 163 | | |
164 | | - | |
| 164 | + | |
165 | 165 | | |
166 | 166 | | |
167 | 167 | | |
| |||
0 commit comments