From 3519a176245d9cc6b5f6802aad3b1bb1c2d6ec47 Mon Sep 17 00:00:00 2001 From: Todd Gamblin Date: Tue, 28 Jan 2020 21:41:35 -0800 Subject: specs: avoid traversing specs when parsing The Spec parser currently calls `spec.traverse()` after every parse, in order to set the platform if it's not set. We don't need to do a full traverse -- we can just check the platforrm as new specs are parsed. This takes about a second off the time required to import all packages in Spack (from 8s to 7s). - [x] simplify platform-setting logic in `SpecParser`. --- lib/spack/spack/spec.py | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) (limited to 'lib') diff --git a/lib/spack/spack/spec.py b/lib/spack/spack/spec.py index 3bee81c0d4..4b6bd67b6d 100644 --- a/lib/spack/spack/spec.py +++ b/lib/spack/spack/spec.py @@ -1117,6 +1117,18 @@ class Spec(object): self._dependencies[spec.name] = dspec spec._dependents[self.name] = dspec + def _add_default_platform(self): + """If a spec has an os or a target and no platform, give it + the default platform. + + This is private because it is used by the parser -- it's not + expected to be used outside of ``spec.py``. + + """ + arch = self.architecture + if arch and not arch.platform and (arch.os or arch.target): + self._set_architecture(platform=spack.architecture.platform().name) + # # Public interface # @@ -4053,14 +4065,6 @@ class SpecParser(spack.parse.Parser): except spack.parse.ParseError as e: raise SpecParseError(e) - # If the spec has an os or a target and no platform, give it - # the default platform - platform_default = spack.architecture.platform().name - for spec in specs: - for s in spec.traverse(): - if s.architecture and not s.architecture.platform and \ - (s.architecture.os or s.architecture.target): - s._set_architecture(platform=platform_default) return specs def spec_from_file(self): @@ -4192,6 +4196,7 @@ class SpecParser(spack.parse.Parser): else: break + spec._add_default_platform() return spec def variant(self, name=None): -- cgit v1.2.3-70-g09d2