Skip to content

Commit 49685aa

Browse files
Remove regular expression re-compiles
- The only thing that changes about the regular expression is the prefix - We can also detect the prefix with startswith, and then use the same regular expression across the loop - This means that Python can cache the compiled regex internally, and we save some time
1 parent 3fa52a9 commit 49685aa

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

nipype/pipeline/engine/utils.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1100,11 +1100,12 @@ def make_field_func(*pair):
11001100
old_edge_dict = jedge_dict[jnode]
11011101
# the edge source node replicates
11021102
expansions = defaultdict(list)
1103-
for node in graph_in.nodes():
1103+
for node in graph_in:
11041104
for src_id in list(old_edge_dict.keys()):
11051105
# Drop the original JoinNodes; only concerned with
11061106
# generated Nodes
1107-
if hasattr(node, "joinfield") and node.itername == src_id:
1107+
itername = node.itername
1108+
if hasattr(node, "joinfield") and itername == src_id:
11081109
continue
11091110
# Patterns:
11101111
# - src_id : Non-iterable node
@@ -1113,10 +1114,12 @@ def make_field_func(*pair):
11131114
# - src_id.[a-z]I.[a-z]\d+ :
11141115
# Non-IdentityInterface w/ iterables
11151116
# - src_idJ\d+ : JoinNode(IdentityInterface)
1116-
if re.match(
1117-
src_id + r"((\.[a-z](I\.[a-z])?|J)\d+)?$", node.itername
1118-
):
1119-
expansions[src_id].append(node)
1117+
if itername.startswith(src_id):
1118+
itername = itername[len(src_id):]
1119+
if re.fullmatch(
1120+
r"((\.[a-z](I\.[a-z])?|J)\d+)?", itername
1121+
):
1122+
expansions[src_id].append(node)
11201123
for in_id, in_nodes in list(expansions.items()):
11211124
logger.debug(
11221125
"The join node %s input %s was expanded" " to %d nodes.",

0 commit comments

Comments
 (0)