Opened 3 weeks ago
Last modified 3 weeks ago
#36729 assigned Cleanup/optimization
Pre-compile regular expressions as standard
| Reported by: | Jake Howard | Owned by: | Jake Howard |
|---|---|---|---|
| Component: | Utilities | Version: | dev |
| Severity: | Normal | Keywords: | |
| Cc: | Triage Stage: | Accepted | |
| Has patch: | yes | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description
See also forum thread.
It's fairly well known that there's a performance boost pre-compiling regexes (either using re.compile or _lazy_re_compile, depending on popularity). However, some of Django's internals compile at time of use, which adds unnecessary extra work. Python's re module caches the most recent 512 patterns, however a reasonably sized Django project can easily go beyond that.
I suggest replacing patterns defined at runtime with pre-compiled patterns done at module import time (or _lazy_re_compile if the pattern isn't used much). This probably wants to be done as a single patch, rather than trying to separate per module or the ticket dragging on.
I also suggest reasonably rejecting any non pre-compiled regexes in the coding style.
Change History (3)
comment:1 by , 3 weeks ago
| Triage Stage: | Unreviewed → Accepted |
|---|
follow-up: 3 comment:2 by , 3 weeks ago
| Has patch: | set |
|---|
(If someone with permission could add the "benchmark" label, that'd be great - it's definitely needed here).
comment:3 by , 3 weeks ago
Replying to Jake Howard:
(If someone with permission could add the "benchmark" label, that'd be great - it's definitely needed here).
Added!
Thank you Jake, I think this makes sense. Looking forward to your work!
Slightly related to #30899.