Skip to content

spec: Introduce cache for Spec objects.#51537

Merged
haampie merged 4 commits intospack:developfrom
vbrunini:spec_object_cache
Jan 20, 2026
Merged

spec: Introduce cache for Spec objects.#51537
haampie merged 4 commits intospack:developfrom
vbrunini:spec_object_cache

Conversation

@vbrunini
Copy link
Copy Markdown
Contributor

@vbrunini vbrunini commented Nov 8, 2025

Split off of #51518 @haampie

Combined with #51535, #51536, #51367 and the reduced install task sleep time from #51491 this reduces the time for a no-op install of a development environment with 94 packages (including dependencies) from 8s to 2.8s for me. It also provides a similar reduction in time for spack build-env --dump env.txt mypkg which we frequently use to start a shell with the build environment.

@vbrunini
Copy link
Copy Markdown
Contributor Author

vbrunini commented Nov 9, 2025

@spackbot fix style

@spackbot-app
Copy link
Copy Markdown

spackbot-app bot commented Nov 9, 2025

Let me see if I can fix that for you!

@spackbot-app
Copy link
Copy Markdown

spackbot-app bot commented Nov 9, 2025

I was able to run spack style --fix for you!

spack style --fix
==> Running style checks on spack
  selected: import, isort, black, flake8, mypy
==> Modified files
  lib/spack/spack/dependency.py
  lib/spack/spack/directives.py
  lib/spack/spack/spec.py
==> Running import checks
  import checks were clean
==> Running isort checks
  isort checks were clean
==> Running black checks
reformatted lib/spack/spack/directives.py
reformatted lib/spack/spack/spec.py
All done! ✨ 🍰 ✨
2 files reformatted, 1 file left unchanged.
  black checks were clean
==> Running flake8 checks
  flake8 checks were clean
==> Running mypy checks
Success: no issues found in 621 source files
  mypy checks were clean
==> spack style checks were clean
Keep in mind that I cannot fix your flake8 or mypy errors, so if you have any you'll need to fix them and update the pull request. If I was able to push to your branch, if you make further changes you will need to pull from your updated branch before pushing again.

I've updated the branch with style fixes.

And convert _EmptySpec to _ImmutableSpec to be used for the spec's
in the cache to ensure they are not mutated.

Timing comparison for:
 "spack-python -c 'import spack.repo; x = list(spack.repo.PATH.all_package_classes())'"

develop ( 9c7f846 ): 23.4s
This patch: 15.5s

Thanks to @haampie for discussions and the _ImmutableSpec portion
(plus a bunch of pre-work in other PRs to actually get rid of spec
mutation elsewhere in spack)!

Signed-off-by: Victor Brunini <[email protected]>
Signed-off-by: Victor Brunini <[email protected]>
Signed-off-by: Victor Brunini <[email protected]>
haampie
haampie previously approved these changes Jan 16, 2026
Copy link
Copy Markdown
Member

@haampie haampie left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me 👍

@haampie
Copy link
Copy Markdown
Member

haampie commented Jan 16, 2026

If I take this PR + #51836 + #51858, and use os._exit(0) instead of sys.exit(0), then importing all packages using

time python -c 'import spack.repo, os; x = list(spack.repo.PATH.all_package_classes()); os._exit(0)'

runs in 3.7s on my macbook. That's 5.6x faster than releases/v1.0 (admittedly, without os._exit(0)).

haampie
haampie previously approved these changes Jan 19, 2026
alalazo
alalazo previously approved these changes Jan 19, 2026
it will raise AttributeError, but that's fine.

Signed-off-by: Harmen Stoppels <[email protected]>
@haampie haampie dismissed stale reviews from alalazo and themself via a1af0da January 19, 2026 19:39
@haampie
Copy link
Copy Markdown
Member

haampie commented Jan 19, 2026

@alalazo, I've changed it to assert self._mutable to show "intent" even though it will raise AttributeError.

Going with a persistent property _mutate is a pain cause it gets pickled, leading to mutation of the _mutate argument itself, and I don't wanna write even more code to prevent that.

@haampie haampie merged commit 2af287a into spack:develop Jan 20, 2026
31 of 32 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants