Change metadata_vars if not
to if ... is None
#37
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.
(Opened this PR instead of dbt-labs/dbt-core#9376)
Both
None
and{}
evaluate toTrue
when usingif not metadata_vars
. This caused us to loop over the environment variables unnecessarily. This provides a significant performance increase for large projects.resolves #
Relates to dbt-labs/dbt-core#6073
Problem
Poor performance with large models.
I was trying to profile a large model and noticed
get_metadata_vars
is called many times and we were spending quite a bit of time in in adictcomp
. I then realised that whenever we do not have any environment variables with the "DBT_ENV_CUSTOM_ENV_" prefix the metadata_vars dictionary will be set to{}
the first timeget_metadata_vars
is called. On subsequent calls, we continue to loop over the environment variables due to the if condition,if not metadata_vars
, which evaluates toTrue
for bothNone
and{}
.Solution
Solution was to change
if not metadata_vars
toif metadata_vars is None
. This ensures we only loop through the environment variables looking for the prefix once.Here is a test that could be run before dbt-common was split off to its own repo and the pytest-benchmark results on my machine. Note that this test may take up to 30 min to complete. Please let me know if you think a shorter test (perhaps one that just ensures we do not unnecessarily loop through
os.environ
multiple times) should be included in this PR.Checklist