performance: speed up spack find in environments

`Environment.added_specs()` has a loop around calls to
`Package.installed()`, which can result in repeated DB queries.  Optimize
this with a read transaction in `Environment`.
This commit is contained in:
Todd Gamblin 2019-12-18 14:25:21 -08:00
parent 5bdba98837
commit 91ea90c253
No known key found for this signature in database
GPG key ID: 66B24B9050FDD0B8

View file

@ -25,6 +25,7 @@
import spack.repo import spack.repo
import spack.schema.env import spack.schema.env
import spack.spec import spack.spec
import spack.store
import spack.util.spack_json as sjson import spack.util.spack_json as sjson
import spack.util.spack_yaml as syaml import spack.util.spack_yaml as syaml
import spack.config import spack.config
@ -1232,13 +1233,16 @@ def added_specs(self):
Yields the user spec for non-concretized specs, and the concrete Yields the user spec for non-concretized specs, and the concrete
spec for already concretized but not yet installed specs. spec for already concretized but not yet installed specs.
""" """
concretized = dict(self.concretized_specs()) # use a transaction to avoid overhead of repeated calls
for spec in self.user_specs: # to `package.installed`
concrete = concretized.get(spec) with spack.store.db.read_transaction():
if not concrete: concretized = dict(self.concretized_specs())
yield spec for spec in self.user_specs:
elif not concrete.package.installed: concrete = concretized.get(spec)
yield concrete if not concrete:
yield spec
elif not concrete.package.installed:
yield concrete
def concretized_specs(self): def concretized_specs(self):
"""Tuples of (user spec, concrete spec) for all concrete specs.""" """Tuples of (user spec, concrete spec) for all concrete specs."""