Optimise searching for placeholders - #1370
Merged
Merged
Conversation
quis
force-pushed
the
optimise-placeholder-extraction
branch
2 times, most recently
from
June 11, 2026 11:32
7b86026 to
9acc20c
Compare
Mocking the init method is messy because then the code in the real `__init__` method doesn’t get run, so properties like `content` don’t get set. We can still test the same thing (which arguments the `Field` is instantiated with) by mocking the whole class. Then any downstream code which is looking at the field will be dealing with a mock, which returns something for properties like `content` which don’t exist.
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.
quis
force-pushed
the
optimise-placeholder-extraction
branch
from
June 11, 2026 12:03
9acc20c to
48e06c3
Compare
BlessedDev
approved these changes
Jun 12, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
Before
After
14× faster
Benchmark for same string with one placeholder right at the end
Before
After
No change.