Commit e9435cd9 authored by Stefan Behnel's avatar Stefan Behnel

Avoid some useless overhead in "inject_utility_code_stage".

parent f52c0a6a
...@@ -82,15 +82,22 @@ def use_utility_code_definitions(scope, target, seen=None): ...@@ -82,15 +82,22 @@ def use_utility_code_definitions(scope, target, seen=None):
def sort_utility_codes(utilcodes): def sort_utility_codes(utilcodes):
ranks = {} ranks = {}
def get_rank(utilcode): get_rank = ranks.get
if utilcode not in ranks:
def calculate_rank(utilcode):
rank = get_rank(utilcode)
if rank is None:
ranks[utilcode] = 0 # prevent infinite recursion on circular dependencies ranks[utilcode] = 0 # prevent infinite recursion on circular dependencies
original_order = len(ranks) original_order = len(ranks)
ranks[utilcode] = 1 + min([get_rank(dep) for dep in utilcode.requires or ()] or [-1]) + original_order * 1e-8 rank = ranks[utilcode] = 1 + (
return ranks[utilcode] min([calculate_rank(dep) for dep in utilcode.requires or ()]) if utilcode.requires else -1
) + original_order * 1e-8
return rank
for utilcode in utilcodes: for utilcode in utilcodes:
get_rank(utilcode) calculate_rank(utilcode)
return [utilcode for utilcode, _ in sorted(ranks.items(), key=lambda kv: kv[1])]
utilcodes.sort(key=get_rank)
def normalize_deps(utilcodes): def normalize_deps(utilcodes):
...@@ -98,39 +105,36 @@ def normalize_deps(utilcodes): ...@@ -98,39 +105,36 @@ def normalize_deps(utilcodes):
for utilcode in utilcodes: for utilcode in utilcodes:
deps[utilcode] = utilcode deps[utilcode] = utilcode
def unify_dep(dep):
if dep in deps:
return deps[dep]
else:
deps[dep] = dep
return dep
for utilcode in utilcodes: for utilcode in utilcodes:
utilcode.requires = [unify_dep(dep) for dep in utilcode.requires or ()] utilcode.requires = [deps.setdefault(dep, dep) for dep in utilcode.requires or ()]
def inject_utility_code_stage_factory(context): def inject_utility_code_stage_factory(context):
def inject_utility_code_stage(module_node): def inject_utility_code_stage(module_node):
module_node.prepare_utility_code() module_node.prepare_utility_code()
use_utility_code_definitions(context.cython_scope, module_node.scope) use_utility_code_definitions(context.cython_scope, module_node.scope)
module_node.scope.utility_code_list = sort_utility_codes(module_node.scope.utility_code_list)
normalize_deps(module_node.scope.utility_code_list) utility_code_list = module_node.scope.utility_code_list
added = [] sort_utility_codes(utility_code_list)
normalize_deps(utility_code_list)
added = set()
# Note: the list might be extended inside the loop (if some utility code # Note: the list might be extended inside the loop (if some utility code
# pulls in other utility code, explicitly or implicitly) # pulls in other utility code, explicitly or implicitly)
for utilcode in module_node.scope.utility_code_list: for utilcode in utility_code_list:
if utilcode in added: if utilcode in added:
continue continue
added.append(utilcode) added.add(utilcode)
if utilcode.requires: if utilcode.requires:
for dep in utilcode.requires: for dep in utilcode.requires:
if dep not in added and dep not in module_node.scope.utility_code_list: if dep not in added:
module_node.scope.utility_code_list.append(dep) utility_code_list.append(dep)
tree = utilcode.get_tree(cython_scope=context.cython_scope) tree = utilcode.get_tree(cython_scope=context.cython_scope)
if tree: if tree:
module_node.merge_in(tree.with_compiler_directives(), module_node.merge_in(tree.with_compiler_directives(),
tree.scope, merge_scope=True) tree.scope, merge_scope=True)
return module_node return module_node
return inject_utility_code_stage return inject_utility_code_stage
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment