Improve performance of jinja2_convenience_function by not importing NAPALM when called #466
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Discovered this while investigating startup performance of the
nautobot-servercommand as a part of nautobot/nautobot#4292. I found that thenetutils.utils.jinja2_convenience_function()function, which is called as a part of Nautobot's startup sequence, turned out to be surprisingly expensive to call, the reason being thatjinja2_convenience_function()imports (but does not itself call)netutils.lib_helpers.get_napalm_getters(), which in turn automatically imports the entire NAPALM library.Importing
napalmtakes anywhere from 0.5 to 1 second all by itself, and we don't actually need that library imported until we actually callget_napalm_getters(). My solution here is to simply move the import inside theget_napalm_getters()function so that it runs when the function is called, rather then simply when the function is initially imported. This makesjinja2_convenience_function()notably faster to call.(edit on noticing CI failure - Why are there tests that CI includes that aren't run by
invoke tests? I raninvoke testssuccessfully locally before pushing this branch up. I'll investigate, but that's a bit annoying. 😄 )