diff --git a/conda_build/metadata.py b/conda_build/metadata.py index 01f3367d03..60922b438c 100644 --- a/conda_build/metadata.py +++ b/conda_build/metadata.py @@ -281,8 +281,8 @@ def select_lines(data, namespace, variants_in_place): if line.lstrip().startswith("#"): # Don't bother with comment only lines continue - m = sel_pat.match(line) - if m: + # Checking for "[" and "]" before regex matching every line is a bit faster. + if "[" in line and "]" in line and (m := sel_pat.match(line)): cond = m.group(3) try: if eval_selector(cond, namespace, variants_in_place): @@ -889,8 +889,8 @@ def get_output_dicts_from_metadata(metadata, outputs=None): outputs.append(OrderedDict(name=metadata.name())) for out in outputs: if ( - "package:" in metadata.get_recipe_text() - and out.get("name") == metadata.name() + out.get("name") == metadata.name() + and "package:" in metadata.get_recipe_text() ): combine_top_level_metadata_with_output(metadata, out) return outputs @@ -1015,6 +1015,7 @@ def get_updated_output_dict_from_reparsed_metadata(original_dict, new_outputs): return output_d +@lru_cache(maxsize=200) def _filter_recipe_text(text, extract_pattern=None): if extract_pattern: match = re.search(extract_pattern, text, flags=re.MULTILINE | re.DOTALL) @@ -1665,7 +1666,6 @@ def build_id(self): raise RuntimeError( f"Couldn't extract raw recipe text for {self.name()} output" ) - raw_recipe_text = self.extract_package_and_build_text() raw_manual_build_string = re.search(r"\s*string:", raw_recipe_text) # user setting their own build string. Don't modify it. if manual_build_string and not ( @@ -2641,13 +2641,11 @@ def get_loop_vars(self): return variants.get_vars(_variants, loop_only=True) def get_used_loop_vars(self, force_top_level=False, force_global=False): - return { - var - for var in self.get_used_vars( - force_top_level=force_top_level, force_global=force_global - ) - if var in self.get_loop_vars() - } + loop_vars = self.get_loop_vars() + used_vars = self.get_used_vars( + force_top_level=force_top_level, force_global=force_global + ) + return set(loop_vars).intersection(used_vars) def get_rendered_recipe_text( self, permit_undefined_jinja=False, extract_pattern=None diff --git a/conda_build/variants.py b/conda_build/variants.py index c5bbe9a41e..076309fa4c 100644 --- a/conda_build/variants.py +++ b/conda_build/variants.py @@ -697,16 +697,18 @@ def get_package_variants(recipedir_or_metadata, config=None, variants=None): def get_vars(variants, loop_only=False): """For purposes of naming/identifying, provide a way of identifying which variables contribute to the matrix dimensionality""" - special_keys = {"pin_run_as_build", "zip_keys", "ignore_version"} - special_keys.update(set(ensure_list(variants[0].get("extend_keys")))) + first_variant, *other_variants = variants + special_keys = { + "pin_run_as_build", + "zip_keys", + "ignore_version", + *ensure_list(first_variant.get("extend_keys")), + } loop_vars = [ k - for k in variants[0] + for k, v in first_variant.items() if k not in special_keys - and ( - not loop_only - or any(variant[k] != variants[0][k] for variant in variants[1:]) - ) + and (not loop_only or any(variant[k] != v for variant in other_variants)) ] return loop_vars @@ -763,7 +765,9 @@ def find_used_variables_in_shell_script(variant, file_path): text = f.read() used_variables = set() for v in variant: - variant_regex = r"(^[^$]*?\$\{?\s*%s\s*[\s|\}])" % v + if v not in text: + continue + variant_regex = rf"(^[^$]*?\$\{{?\s*{re.escape(v)}\s*[\s|\}}])" if re.search(variant_regex, text, flags=re.MULTILINE | re.DOTALL): used_variables.add(v) return used_variables @@ -774,7 +778,9 @@ def find_used_variables_in_batch_script(variant, file_path): text = f.read() used_variables = set() for v in variant: - variant_regex = r"\%" + v + r"\%" + if v not in text: + continue + variant_regex = rf"\%{re.escape(v)}\%" if re.search(variant_regex, text, flags=re.MULTILINE | re.DOTALL): used_variables.add(v) return used_variables diff --git a/news/5248-render-speedups-func-calls b/news/5248-render-speedups-func-calls new file mode 100644 index 0000000000..77863ddf02 --- /dev/null +++ b/news/5248-render-speedups-func-calls @@ -0,0 +1,19 @@ +### Enhancements + +* Speed up rendering by avoiding costly/redundant function calls. (#5248) + +### Bug fixes + +* + +### Deprecations + +* + +### Docs + +* + +### Other + +*