Skip to content

Commit 52f544d

Browse files
Optimize substitute method (pyccel#1938)
### Description This PR addresses an optimization for the substitute method in the PyccelAstNode class. The current implementation does not check if original and replacement are empty, leading to unnecessary recursion over the code. This update adds a condition to return immediately if original is empty (and consequenty replacement), thereby improving the performance of the method. --------- Co-authored-by: Emily Bourne <[email protected]>
1 parent 01d7407 commit 52f544d

File tree

2 files changed

+6
-0
lines changed

2 files changed

+6
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ All notable changes to this project will be documented in this file.
2727
- #1895 : Add Python support for dict initialisation with `dict()`.
2828
- #1886 : Add Python support for dict method `pop()`.
2929
- #1936 : Add missing C output for inline decorator example in documentation
30+
- #1937 : Optimise `pyccel.ast.basic.PyccelAstNode.substitute` method.
3031
- \[INTERNALS\] Added `container_rank` property to `ast.datatypes.PyccelType` objects.
3132
- \[DEVELOPER\] Added an improved traceback to the developer-mode errors for errors in function calls.
3233

pyccel/ast/basic.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,11 @@ def substitute(self, original, replacement, excluded_nodes = (), invalidate = Tr
279279
return
280280
self._recursion_in_progress = True
281281

282+
if not original:
283+
assert not replacement
284+
self._recursion_in_progress = False
285+
return
286+
282287
if iterable(original):
283288
assert iterable(replacement)
284289
assert len(original) == len(replacement)

0 commit comments

Comments
 (0)