This repository has been archived on 2025-03-20. You can view files and clone it, but cannot push or open issues or pull requests.
sonic-buildimage/src/sonic-config-engine/lazy_re.py
Stepan Blyshchak 064689d442 [sonic-cfggen] optimize sonic-cfggen startup (#3658)
* [sonic-cfggen] optimize execution time

a lot of template rendering causes switch to start longer because jinja2
needs to parse them. Introducing RedisBytecodeCache to store parsed buckets of
internal template bytecode to speedup same template rendering during start

* [sonic-cfggen] do lazy regexp compilation to speedup sonic-cfggen

* [sonic-cfggen] address pep8 related comments

Signed-off-by: Stepan Blyschak <stepanb@mellanox.com>
2019-10-31 09:17:29 -07:00

23 lines
604 B
Python

# monkey patch re.compile to improve import time of some packages
import re
_orig_re_compile = re.compile
def __re_compile(*args, **kwargs):
class __LazyReCompile(object):
def __init__(self, *args, **kwargs):
self.args = args
self.kwargs = kwargs
self.pattern_obj = None
def __getattr__(self, name):
if self.pattern_obj is None:
self.pattern_obj = _orig_re_compile(*self.args, **self.kwargs)
return getattr(self.pattern_obj, name)
return __LazyReCompile(*args, **kwargs)
re.compile = __re_compile