Skip to content

Commit f2bf051

Browse files
ArmavicaricardoV94
authored andcommitted
Fix PERF401: list comprehensions when appropriate
1 parent 3647c98 commit f2bf051

File tree

10 files changed

+41
-37
lines changed

10 files changed

+41
-37
lines changed

pytensor/compile/debugmode.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -756,10 +756,7 @@ def _get_preallocated_maps(
756756
# TODO: Sparse? Scalar does not really make sense.
757757

758758
# Do not preallocate memory for outputs that actually work inplace
759-
considered_outputs = []
760-
for r in node.outputs:
761-
if r not in inplace_outs:
762-
considered_outputs.append(r)
759+
considered_outputs = [r for r in node.outputs if r not in inplace_outs]
763760

764761
# Output storage that was initially present in the storage_map
765762
if "initial" in prealloc_modes or "ALL" in prealloc_modes:

pytensor/link/c/basic.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1450,12 +1450,16 @@ def in_sig(i, topological_pos, i_idx):
14501450
if props:
14511451
version.append(props)
14521452

1453-
for i in node.inputs:
1454-
if isinstance(i.type, CLinkerObject):
1455-
version.append(i.type.c_code_cache_version())
1456-
for o in node.outputs:
1457-
if isinstance(o.type, CLinkerObject):
1458-
version.append(o.type.c_code_cache_version())
1453+
version.extend(
1454+
i.type.c_code_cache_version()
1455+
for i in node.inputs
1456+
if isinstance(i.type, CLinkerObject)
1457+
)
1458+
version.extend(
1459+
o.type.c_code_cache_version()
1460+
for o in node.outputs
1461+
if isinstance(o.type, CLinkerObject)
1462+
)
14591463

14601464
# add the signature for this node
14611465
sig.append(

pytensor/link/c/cmodule.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2131,9 +2131,11 @@ def get_lines(cmd, parse=True):
21312131
or "-march=native" in line
21322132
):
21332133
continue
2134-
for reg in ("-march=", "-mtune=", "-target-cpu", "-mabi="):
2135-
if reg in line:
2136-
selected_lines.append(line.strip())
2134+
selected_lines.extend(
2135+
line.strip()
2136+
for reg in ("-march=", "-mtune=", "-target-cpu", "-mabi=")
2137+
if reg in line
2138+
)
21372139
lines = list(set(selected_lines)) # to remove duplicate
21382140

21392141
return lines

pytensor/link/vm.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1270,15 +1270,16 @@ def make_all(
12701270
if self.allow_gc:
12711271
post_thunk_clear = []
12721272
for node in order:
1273-
clear_after_this_thunk = []
1274-
for input in node.inputs:
1273+
clear_after_this_thunk = [
1274+
storage_map[input]
1275+
for input in node.inputs
12751276
if (
12761277
input in computed
12771278
and input not in fgraph.outputs
12781279
and node == last_user[input]
12791280
and input not in reallocated_vars
1280-
):
1281-
clear_after_this_thunk.append(storage_map[input])
1281+
)
1282+
]
12821283
post_thunk_clear.append(clear_after_this_thunk)
12831284
else:
12841285
post_thunk_clear = None

pytensor/tensor/conv/abstract_conv.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -585,10 +585,11 @@ def assert_shape(x, expected_shape, msg="Unexpected shape."):
585585
if expected_shape is None or not config.conv__assert_shape:
586586
return x
587587
shape = x.shape
588-
tests = []
589-
for i in range(x.ndim):
590-
if expected_shape[i] is not None:
591-
tests.append(pt.eq(shape[i], expected_shape[i]))
588+
tests = [
589+
pt.eq(shape[i], expected_shape[i])
590+
for i in range(x.ndim)
591+
if expected_shape[i] is not None
592+
]
592593
if tests:
593594
return Assert(msg)(x, *tests)
594595
else:

pytensor/tensor/subtensor.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2107,10 +2107,7 @@ def perform(self, node, inp, out_):
21072107
out[0] = x.take(i, axis=0, out=o)
21082108

21092109
def connection_pattern(self, node):
2110-
rval = [[True]]
2111-
2112-
for ipt in node.inputs[1:]:
2113-
rval.append([False])
2110+
rval = [[True], *([False] for _ in node.inputs[1:])]
21142111

21152112
return rval
21162113

tests/sparse/test_basic.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1784,13 +1784,14 @@ def f_b(z, a, x, y):
17841784
)
17851785
== len(topo) - 5
17861786
)
1787-
new_topo = []
1788-
for node in topo:
1787+
new_topo = [
1788+
node
1789+
for node in topo
17891790
if not (
17901791
isinstance(node.op, Elemwise)
17911792
and isinstance(node.op.scalar_op, pytensor.scalar.basic.Cast)
1792-
):
1793-
new_topo.append(node)
1793+
)
1794+
]
17941795
topo = new_topo
17951796
assert len(topo) == 5, topo
17961797
# Usmm is tested at the same time in debugmode

tests/tensor/test_math.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3477,8 +3477,9 @@ def test_grad_useless_sum():
34773477
old_values_eq_approx = staticmethod(TensorType.values_eq_approx)
34783478
TensorType.values_eq_approx = staticmethod(values_eq_approx_remove_nan)
34793479
try:
3480-
for test_value in test_values:
3481-
outputs.append(f(np.array([test_value]).astype("float32")))
3480+
outputs.extend(
3481+
f(np.array([test_value]).astype("float32")) for test_value in test_values
3482+
)
34823483
finally:
34833484
TensorType.values_eq_approx = old_values_eq_approx
34843485

tests/tensor/test_subtensor.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1143,9 +1143,11 @@ def test_grad_list(self):
11431143
data = random(4)
11441144
data = np.asarray(data, dtype=self.dtype)
11451145
idxs = [[i] for i in range(data.shape[0])]
1146-
for i in range(data.shape[0]):
1147-
for j in range(0, data.shape[0], 2):
1148-
idxs.append([i, j, (i + 1) % data.shape[0]])
1146+
idxs.extend(
1147+
[i, j, (i + 1) % data.shape[0]]
1148+
for i in range(data.shape[0])
1149+
for j in range(0, data.shape[0], 2)
1150+
)
11491151
self.grad_list_(idxs, data)
11501152

11511153
data = random(4, 3)

tests/typed_list/test_type.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,7 @@ def test_load_alot(self):
7878
myType = TypedListType(TensorType(pytensor.config.floatX, shape=(None, None)))
7979

8080
x = random_ranged(-1000, 1000, [10, 10])
81-
testList = []
82-
for i in range(10000):
83-
testList.append(x)
81+
testList = [x for _ in range(10000)]
8482

8583
assert np.array_equal(myType.filter(testList), testList)
8684

0 commit comments

Comments
 (0)