Skip to content

Commit

Permalink
Reduce complexity of forward substitution algorithm
Browse files Browse the repository at this point in the history
Instead of creating all possible paths from src to dst and checking
them, check while flooding the graph, which is much less costly.

As a side effect, remove graph.all_simple_paths, which was previously
used and responsible fro the performance issue.

Fix #2216
  • Loading branch information
serge-sans-paille committed Jul 16, 2024
1 parent d4fcaeb commit 7e24de7
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 82 deletions.
68 changes: 0 additions & 68 deletions pythran/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,71 +63,3 @@ def has_path(graph, src, dest):
return True
worklist.extend(graph.successors(current))
return False

# Copied verbatim from NetworkX 2.6.1
#
# NetworkX is distributed with the 3-clause BSD license.
#
# ::
#
# Copyright (C) 2004-2021, NetworkX Developers
# Aric Hagberg <hagberg@lanl.gov>
# Dan Schult <dschult@colgate.edu>
# Pieter Swart <swart@lanl.gov>
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following
# disclaimer in the documentation and/or other materials provided
# with the distribution.
#
# * Neither the name of the NetworkX Developers nor the names of its
# contributors may be used to endorse or promote products derived
# from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

def _all_simple_paths_graph(G, source, targets, cutoff):
visited = dict.fromkeys([source])
stack = [iter(G[source])]
while stack:
children = stack[-1]
child = next(children, None)
if child is None:
stack.pop()
visited.popitem()
elif len(visited) < cutoff:
if child in visited:
continue
if child in targets:
yield list(visited) + [child]
visited[child] = None
if targets - set(visited.keys()): # expand stack until find all targets
stack.append(iter(G[child]))
else:
visited.popitem() # maybe other ways to child
else: # len(visited) == cutoff:
for target in (targets & (set(children) | {child})) - set(visited.keys()):
yield list(visited) + [target]
stack.pop()
visited.popitem()

def all_simple_paths(graph, src, target):
return _all_simple_paths_graph(graph, src, {target}, len(graph) - 1)
37 changes: 23 additions & 14 deletions pythran/optimizations/forward_substitution.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,22 +135,31 @@ def visit_Name(self, node):
return value
elif len(parent.targets) == 1:
ids = self.gather(Identifiers, value)
node_stmt = next(reversed([s for s in self.ancestors[node]
if isinstance(s, ast.stmt)]))
all_paths = graph.all_simple_paths(self.cfg, parent, node_stmt)
for path in all_paths:
for stmt in path[1:-1]:
node_stmt = next(s for s in self.ancestors[node][::-1]
if isinstance(s, ast.stmt))
# Check if there is a path from `parent' to `node_stmt' that
# modifies any of the identifier from `value'. If so, cancel the
# forward substitution.
worklist = [node_stmt]
visited = {parent}
while worklist:
workitem = worklist.pop()
if workitem in visited:
continue
visited.add(workitem)
for pred in self.cfg.predecessors(workitem):
if not graph.has_path(self.cfg, parent, pred):
continue

assigned_ids = {n.id
for n in self.gather(IsAssigned, stmt)}
for n in self.gather(IsAssigned,
pred)}
if not ids.isdisjoint(assigned_ids):
break
else:
continue
break
else:
self.update = True
self.to_remove[parent].append(dnode)
return value
return node # cancel
worklist.append(pred)
self.update = True
self.to_remove[parent].append(dnode)
return value

return node

Expand Down

0 comments on commit 7e24de7

Please sign in to comment.