[Python-ideas] re.compile_lazy - on first use compiled regexes

Bruce Leban bruce at leapyear.org
Sun Mar 24 00:03:19 CET 2013

To summarize:

- compiling regexes is slow so applications frequently compute it once and
save it
- compiling all the regexes at startup slows down startup for regexes that
may never be used
- a common pattern is to compute once at time of use and it would be nice
to optimize this pattern
- the regex library has a cache feature which means that frequently it will
be optimized automatically
- however, there's no guarantee that the regex you care about won't fall
out of the cache.

I think this addresses all the issues better than compute_lazy:

re.compile(r'...', keep=True)

When keep=True is specified, the regex library keeps the cached value for
the lifetime of the process. The regex is computed only once on first use
and you don't need to create a place to store it. Furthermore, if you use
the same regex in more than one place, once with keep=True, the other uses
will automatically be optimized.

--- Bruce
Latest blog post: Alice's Puzzle Page http://www.vroospeak.com
Learn how hackers think: http://j.mp/gruyere-security
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20130323/1416c63b/attachment.html>

More information about the Python-ideas mailing list