Skip to content

use tuple([...]) in a few places#51841

Merged
alalazo merged 1 commit intodevelopfrom
hs/fix/micro-optimizations
Jan 14, 2026
Merged

use tuple([...]) in a few places#51841
alalazo merged 1 commit intodevelopfrom
hs/fix/micro-optimizations

Conversation

@haampie
Copy link
Copy Markdown
Member

@haampie haampie commented Jan 13, 2026

Extracted from #51836

In Python 3.11+ it's better to do tuple([...]) instead of tuple(...). For
older Python it doesn't matter much.

The reason is that tuple(x for x in xs) creates a generator function
where cpythonn needs another stack frame, whereas tuple([x for x in xs])
is native iteration equivalent to lst = []; for x in xs: lst.append(x); tuple(lst),
except that append is not a function call but an intrinsic.

Further, remove the indirection of the HashableMap.__iter__ function
call, and do not use splatting in StandardVersion.from_string.

Before:

$ python -m timeit -s 'from spack.spec import Spec; s = Spec("[email protected] foo=bar")' 'hash(s)'
100000 loops, best of 5: 2.66 usec per loop
$ python -m timeit -s 'from spack.version import ver' 'ver("1.2.3")'
200000 loops, best of 5: 1.82 usec per loop

After:

$ python -m timeit -s 'from spack.spec import Spec; s = Spec("[email protected] foo=bar")' 'hash(s)'
200000 loops, best of 5: 1.58 usec per loop
$ python -m timeit -s 'from spack.version import ver' 'ver("1.2.3")'
200000 loops, best of 5: 1.54 usec per loop

On my macbook it gives small (4.8%) reduction in package load time.

In Python 3.11+ it's better to do `tuple([])`, for older Python it
doesn't matter much.

The reason is that `tuple(x for x in xs)` creates a generator function
where cpython goes between to frames, whereas `tuple([x for x in xs])`
is native iteration.

Further, remove the indirection of the `HashableMap.__iter__` function
call, and do not use splatting in `StandardVersion.from_string`.

Before:

```
$ python -m timeit -s 'from spack.spec import Spec; s = Spec("[email protected] foo=bar")' 'hash(s)'
100000 loops, best of 5: 2.66 usec per loop
$ python -m timeit -s 'from spack.version import ver' 'ver("1.2.3")'
200000 loops, best of 5: 1.82 usec per loop
```

After:

```
$ python -m timeit -s 'from spack.spec import Spec; s = Spec("[email protected] foo=bar")' 'hash(s)'
200000 loops, best of 5: 1.58 usec per loop
$ python -m timeit -s 'from spack.version import ver' 'ver("1.2.3")'
200000 loops, best of 5: 1.54 usec per loop
```

Signed-off-by: Harmen Stoppels <[email protected]>
@haampie haampie changed the title use tuple([]) in a few places use tuple([...]) in a few places Jan 13, 2026
@alalazo alalazo merged commit 7306e48 into develop Jan 14, 2026
31 of 32 checks passed
@alalazo alalazo deleted the hs/fix/micro-optimizations branch January 14, 2026 09:36
@haampie haampie mentioned this pull request Jan 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants