Commit e1e2b3bb authored by Stefan Behnel's avatar Stefan Behnel

Revert "Avoid some useless overhead in "inject_utility_code_stage"."

This reverts commit e9435cd9.
parent 43b24513
...@@ -82,22 +82,15 @@ def use_utility_code_definitions(scope, target, seen=None): ...@@ -82,22 +82,15 @@ def use_utility_code_definitions(scope, target, seen=None):
def sort_utility_codes(utilcodes): def sort_utility_codes(utilcodes):
ranks = {} ranks = {}
get_rank = ranks.get def get_rank(utilcode):
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)
rank = ranks[utilcode] = 1 + ( ranks[utilcode] = 1 + min([get_rank(dep) for dep in utilcode.requires or ()] or [-1]) + original_order * 1e-8
min([calculate_rank(dep) for dep in utilcode.requires or ()]) if utilcode.requires else -1 return ranks[utilcode]
) + original_order * 1e-8
return rank
for utilcode in utilcodes: for utilcode in utilcodes:
calculate_rank(utilcode) get_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):
...@@ -105,36 +98,39 @@ def normalize_deps(utilcodes): ...@@ -105,36 +98,39 @@ 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 = [deps.setdefault(dep, dep) for dep in utilcode.requires or ()] utilcode.requires = [unify_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)
utility_code_list = module_node.scope.utility_code_list normalize_deps(module_node.scope.utility_code_list)
sort_utility_codes(utility_code_list) added = []
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 utility_code_list: for utilcode in module_node.scope.utility_code_list:
if utilcode in added: if utilcode in added:
continue continue
added.add(utilcode) added.append(utilcode)
if utilcode.requires: if utilcode.requires:
for dep in utilcode.requires: for dep in utilcode.requires:
if dep not in added: if dep not in added and dep not in module_node.scope.utility_code_list:
utility_code_list.append(dep) module_node.scope.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