Skip to content

_make_name_re causes very slow jinja2 imports due to abuse of regexes #729

@lf-

Description

@lf-

Expected Behavior

Jinja2 should import near-instantly

Actual Behavior

It imports in almost 250 milliseconds

Repro Code

import time
start = time.time()
import jinja2
end = time.time()
print('Jinja2 took ', end-start, 's to import')

Output: Jinja2 took 0.2398972511291504 s to import

The profiler shows that (in the profiled run), 289 ms was spent in _make_name_re in lexer.py.

Upon further inspection, I found that this line:

name_re = re.compile(r'[%s][%s]*' % (_stringdefs.xid_start,
                                     _stringdefs.xid_continue))

is the culprit, taking around 159 ms to run. It's incredibly slow because of the sheer length of the regex.

Your Environment

  • Python version: 3.6.1
  • Jinja version: 2.9.6 (found similarly slow results on a git checkout seconds before submitting this)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions