Skip to content

Commit dfebe3b

Browse files
committed
Move FunctionDependencyGraph to its own class.
1 parent 380a40c commit dfebe3b

File tree

2 files changed

+77
-76
lines changed

2 files changed

+77
-76
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
from typed_python.compiler.directed_graph import DirectedGraph
2+
from sortedcontainers import SortedSet
3+
4+
5+
class FunctionDependencyGraph:
6+
def __init__(self):
7+
self._dependencies = DirectedGraph()
8+
9+
# the search depth in the dependency to find 'identity'
10+
# the _first_ time we ever saw it. We prefer to update
11+
# nodes with higher search depth, so we don't recompute
12+
# earlier nodes until their children are complete.
13+
self._identity_levels = {}
14+
15+
# nodes that need to recompute
16+
self._dirty_inflight_functions = set()
17+
18+
# (priority, node) pairs that need to recompute
19+
self._dirty_inflight_functions_with_order = SortedSet(key=lambda pair: pair[0])
20+
21+
def dropNode(self, node):
22+
self._dependencies.dropNode(node, False)
23+
if node in self._identity_levels:
24+
del self._identity_levels[node]
25+
self._dirty_inflight_functions.discard(node)
26+
27+
def getNextDirtyNode(self):
28+
while self._dirty_inflight_functions_with_order:
29+
priority, identity = self._dirty_inflight_functions_with_order.pop()
30+
31+
if identity in self._dirty_inflight_functions:
32+
self._dirty_inflight_functions.discard(identity)
33+
34+
return identity
35+
36+
def addRoot(self, identity):
37+
if identity not in self._identity_levels:
38+
self._identity_levels[identity] = 0
39+
self.markDirty(identity)
40+
41+
def addEdge(self, caller, callee):
42+
if caller not in self._identity_levels:
43+
raise Exception(f"unknown identity {caller} found in the graph")
44+
45+
if callee not in self._identity_levels:
46+
self._identity_levels[callee] = self._identity_levels[caller] + 1
47+
48+
self.markDirty(callee, isNew=True)
49+
50+
self._dependencies.addEdge(caller, callee)
51+
52+
def getNamesDependedOn(self, caller):
53+
return self._dependencies.outgoing(caller)
54+
55+
def markDirtyWithLowPriority(self, callee):
56+
# mark this dirty, but call it back after new functions.
57+
self._dirty_inflight_functions.add(callee)
58+
59+
level = self._identity_levels[callee]
60+
self._dirty_inflight_functions_with_order.add((-1000000 + level, callee))
61+
62+
def markDirty(self, callee, isNew=False):
63+
self._dirty_inflight_functions.add(callee)
64+
65+
if isNew:
66+
# if its a new node, compute it with higher priority the _higher_ it is in the stack
67+
# so that we do a depth-first search on the way down
68+
level = 1000000 - self._identity_levels[callee]
69+
else:
70+
level = self._identity_levels[callee]
71+
72+
self._dirty_inflight_functions_with_order.add((level, callee))
73+
74+
def functionReturnSignatureChanged(self, identity):
75+
for caller in self._dependencies.incoming(identity):
76+
self.markDirty(caller)

typed_python/compiler/python_to_native_converter.py

Lines changed: 1 addition & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@
2323
import typed_python.compiler
2424
import typed_python.compiler.native_ast as native_ast
2525
from typed_python.compiler.native_function_pointer import NativeFunctionPointer
26-
from sortedcontainers import SortedSet
27-
from typed_python.compiler.directed_graph import DirectedGraph
2826
from typed_python.compiler.type_wrappers.wrapper import Wrapper
2927
from typed_python.compiler.type_wrappers.class_wrapper import ClassWrapper
3028
from typed_python.compiler.python_object_representation import typedPythonTypeToTypeWrapper
@@ -33,6 +31,7 @@
3331
from typed_python.compiler.type_wrappers.python_typed_function_wrapper import (
3432
PythonTypedFunctionWrapper, CannotBeDetermined, NoReturnTypeSpecified
3533
)
34+
from typed_python.compiler.function_dependency_graph import FunctionDependencyGraph
3635
from typed_python.compiler.typed_call_target import TypedCallTarget
3736

3837
typeWrapper = lambda t: typed_python.compiler.python_object_representation.typedPythonTypeToTypeWrapper(t)
@@ -41,80 +40,6 @@
4140
VALIDATE_FUNCTION_DEFINITIONS_STABLE = False
4241

4342

44-
class FunctionDependencyGraph:
45-
def __init__(self):
46-
self._dependencies = DirectedGraph()
47-
48-
# the search depth in the dependency to find 'identity'
49-
# the _first_ time we ever saw it. We prefer to update
50-
# nodes with higher search depth, so we don't recompute
51-
# earlier nodes until their children are complete.
52-
self._identity_levels = {}
53-
54-
# nodes that need to recompute
55-
self._dirty_inflight_functions = set()
56-
57-
# (priority, node) pairs that need to recompute
58-
self._dirty_inflight_functions_with_order = SortedSet(key=lambda pair: pair[0])
59-
60-
def dropNode(self, node):
61-
self._dependencies.dropNode(node, False)
62-
if node in self._identity_levels:
63-
del self._identity_levels[node]
64-
self._dirty_inflight_functions.discard(node)
65-
66-
def getNextDirtyNode(self):
67-
while self._dirty_inflight_functions_with_order:
68-
priority, identity = self._dirty_inflight_functions_with_order.pop()
69-
70-
if identity in self._dirty_inflight_functions:
71-
self._dirty_inflight_functions.discard(identity)
72-
73-
return identity
74-
75-
def addRoot(self, identity):
76-
if identity not in self._identity_levels:
77-
self._identity_levels[identity] = 0
78-
self.markDirty(identity)
79-
80-
def addEdge(self, caller, callee):
81-
if caller not in self._identity_levels:
82-
raise Exception(f"unknown identity {caller} found in the graph")
83-
84-
if callee not in self._identity_levels:
85-
self._identity_levels[callee] = self._identity_levels[caller] + 1
86-
87-
self.markDirty(callee, isNew=True)
88-
89-
self._dependencies.addEdge(caller, callee)
90-
91-
def getNamesDependedOn(self, caller):
92-
return self._dependencies.outgoing(caller)
93-
94-
def markDirtyWithLowPriority(self, callee):
95-
# mark this dirty, but call it back after new functions.
96-
self._dirty_inflight_functions.add(callee)
97-
98-
level = self._identity_levels[callee]
99-
self._dirty_inflight_functions_with_order.add((-1000000 + level, callee))
100-
101-
def markDirty(self, callee, isNew=False):
102-
self._dirty_inflight_functions.add(callee)
103-
104-
if isNew:
105-
# if its a new node, compute it with higher priority the _higher_ it is in the stack
106-
# so that we do a depth-first search on the way down
107-
level = 1000000 - self._identity_levels[callee]
108-
else:
109-
level = self._identity_levels[callee]
110-
111-
self._dirty_inflight_functions_with_order.add((level, callee))
112-
113-
def functionReturnSignatureChanged(self, identity):
114-
for caller in self._dependencies.incoming(identity):
115-
self.markDirty(caller)
116-
117-
11843
class PythonToNativeConverter:
11944
def __init__(self, llvmCompiler, compilerCache):
12045
object.__init__(self)

0 commit comments

Comments
 (0)