Merged
Conversation
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]>
alalazo
approved these changes
Jan 14, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Extracted from #51836
In Python 3.11+ it's better to do
tuple([...])instead oftuple(...). Forolder Python it doesn't matter much.
The reason is that
tuple(x for x in xs)creates a generator functionwhere 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
appendis not a function call but an intrinsic.Further, remove the indirection of the
HashableMap.__iter__functioncall, and do not use splatting in
StandardVersion.from_string.Before:
After:
On my macbook it gives small (4.8%) reduction in package load time.