⚡️ Speed up function do_for by 46%
#210
Open
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.
📄 46% (0.46x) speedup for
do_forindjango/template/defaulttags.py⏱️ Runtime :
1.12 milliseconds→767 microseconds(best of46runs)📝 Explanation and details
Key optimizations made:
re.split) for comma splitting loopvars, as this was responsible for ~27.9% of runtime and is unnecessary for simple cases.str.split(',')andstr.strip()instead of regex. This greatly reduces the cost for the common case (single variable or simple comma-separated variables), lowering memory use and CPU cycles.This approach is both safe and performance improved, especially for templates that have simple or typical variable unpacking cases. Regex engine overhead is avoided entirely unless Django's complex template parsing actually demands it (which is not the case for comma separation).
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-do_for-mhcvgfvrand push.