Opened 2 hours 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: | Unreviewed | |
| Has patch: | no | 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.