-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[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 <[email protected]>
- Loading branch information
1 parent
841949f
commit 064689d
Showing
4 changed files
with
63 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# 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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import jinja2 | ||
|
||
class RedisBytecodeCache(jinja2.BytecodeCache): | ||
""" A bytecode cache for jinja2 template that stores bytecode in Redis """ | ||
|
||
REDIS_HASH = 'JINJA2_CACHE' | ||
|
||
def __init__(self, client): | ||
self._client = client | ||
try: | ||
self._client.connect(self._client.STATE_DB, retry_on=False) | ||
except Exception: | ||
self._client = None | ||
|
||
def load_bytecode(self, bucket): | ||
if self._client is None: | ||
return | ||
code = self._client.get(self._client.STATE_DB, self.REDIS_HASH, bucket.key) | ||
if code is not None: | ||
bucket.bytecode_from_string(code) | ||
|
||
def dump_bytecode(self, bucket): | ||
if self._client is None: | ||
return | ||
self._client.set(self._client.STATE_DB, self.REDIS_HASH, bucket.key, bucket.bytecode_to_string()) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,7 +16,7 @@ def get_test_suite(): | |
author='Taoyu Li', | ||
author_email='[email protected]', | ||
url='https://github.com/Azure/sonic-buildimage', | ||
py_modules=['portconfig', 'minigraph', 'openconfig_acl', 'sonic_device_util', 'config_samples'], | ||
py_modules=['portconfig', 'minigraph', 'openconfig_acl', 'sonic_device_util', 'config_samples', 'redis_bcc', 'lazy_re'], | ||
scripts=['sonic-cfggen'], | ||
install_requires=['lxml', 'jinja2>=2.10', 'netaddr', 'ipaddr', 'pyyaml', 'pyangbind==0.6.0'], | ||
test_suite='setup.get_test_suite', | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters