Skip to content
This repository was archived by the owner on Mar 2, 2022. It is now read-only.

Commit 5b22166

Browse files
committed
There can be only 1: don't use a list for prev/next
Summary: This is just a minor cleanup of something that has bugged me about the compiler for a while - for some reason they use a list of blocks for the next/prev block. But they expect the list to only have at maximum 1 item. So now we just store the blocks directly and init them to None. Test Plan: ./python -m test.test_compiler
1 parent efd21f8 commit 5b22166

File tree

2 files changed

+16
-15
lines changed

2 files changed

+16
-15
lines changed

compiler/pyassem.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,8 @@ def __init__(self, label=''):
194194
self.outEdges = set()
195195
self.label = label
196196
self.bid = None
197-
self.next = []
198-
self.prev = []
197+
self.next = None
198+
self.prev = None
199199
self.returns = False
200200
self.offset = 0
201201
self.seen = False # visited during stack depth calculation
@@ -205,7 +205,7 @@ def __repr__(self):
205205
data = []
206206
data.append(f'id={self.bid}')
207207
if self.next:
208-
data.append(f'next={self.next[0].bid}')
208+
data.append(f'next={self.next.bid}')
209209
extras = ", ".join(data)
210210
if self.label:
211211
return f"<block {self.label} {extras}>"
@@ -230,10 +230,10 @@ def addOutEdge(self, block):
230230
self.outEdges.add(block)
231231

232232
def addNext(self, block):
233-
self.next.append(block)
234-
assert len(self.next) == 1, map(str, self.next)
235-
block.prev.append(self)
236-
assert len(block.prev) == 1, map(str, block.prev)
233+
assert self.next is None, next
234+
self.next = block
235+
assert block.prev is None, block.prev
236+
block.prev = self
237237

238238
_uncond_transfer = ('RETURN_VALUE', 'RAISE_VARARGS',
239239
'JUMP_ABSOLUTE', 'JUMP_FORWARD', 'CONTINUE_LOOP',
@@ -250,11 +250,11 @@ def has_return(self):
250250
return self.insts and self.insts[-1].opname == "RETURN_VALUE"
251251

252252
def get_children(self):
253-
return list(self.outEdges) + self.next
253+
return list(self.outEdges) + [self.next]
254254

255255
def get_followers(self):
256256
"""Get the whole list of followers, including the next block."""
257-
followers = set(self.next)
257+
followers = {self.next}
258258
# Blocks that must be emitted *after* this one, because of
259259
# bytecode offsets (e.g. relative jumps) pointing to them.
260260
for inst in self.insts:
@@ -372,6 +372,8 @@ def dump(self, io=None):
372372
sys.stdout = save
373373

374374
def stackdepth_walk(self, block, depth, maxdepth):
375+
assert block is not None
376+
375377
if block.seen or block.startdepth >= depth:
376378
return maxdepth
377379
block.seen = True
@@ -407,8 +409,7 @@ def stackdepth_walk(self, block, depth, maxdepth):
407409
return maxdepth
408410

409411
if block.next:
410-
assert len(block.next) == 1
411-
maxdepth = self.stackdepth_walk(block.next[0], depth, maxdepth)
412+
maxdepth = self.stackdepth_walk(block.next, depth, maxdepth)
412413

413414
block.seen = False
414415
return maxdepth

test_compiler/test_graph.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class GraphTests(CompilerTest):
1919

2020
def format_graph(self, graph):
2121
if graph.next:
22-
return f"Block({repr(graph.label)}, {self.format_graph(graph.next[0])})"
22+
return f"Block({repr(graph.label)}, {self.format_graph(graph.next)})"
2323
return f"Block({repr(graph.label)})"
2424

2525
def assert_graph_equal(self, graph, expected):
@@ -34,10 +34,10 @@ def assert_graph_equal(self, graph, expected):
3434
def assert_graph_equal_worker(self, compiled, expected):
3535
self.assertEqual(compiled.label, expected.label)
3636
if expected.next:
37-
self.assertEqual(len(compiled.next), 1)
38-
self.assert_graph_equal_worker(compiled.next[0], expected.next)
37+
self.assertIsNotNone(compiled.next)
38+
self.assert_graph_equal_worker(compiled.next, expected.next)
3939
else:
40-
self.assertEqual(compiled.next, [])
40+
self.assertEqual(compiled.next, None)
4141

4242
def get_child_graph(self, graph, name):
4343
for block in graph.ordered_blocks:

0 commit comments

Comments
 (0)