From 4f07205c6376aeac4fa8d2e24db205441174b78f Mon Sep 17 00:00:00 2001 From: Harmen Stoppels Date: Wed, 16 Aug 2023 14:33:03 +0200 Subject: Avoid sort on singleton list during edge insertion (#39458) The median length of this list of 1. For reasons I don't know, `.sort()` still like to call the key function. This saves ~9% of total database read time, and the number of calls goes from 5305 -> 1715. --- lib/spack/spack/spec.py | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) (limited to 'lib') diff --git a/lib/spack/spack/spec.py b/lib/spack/spack/spec.py index a3b0920bf0..3d8887a773 100644 --- a/lib/spack/spack/spec.py +++ b/lib/spack/spack/spec.py @@ -985,16 +985,14 @@ class _EdgeMap(collections.abc.Mapping): def __len__(self): return len(self.edges) - def add(self, edge): - """Adds a new edge to this object. - - Args: - edge (DependencySpec): edge to be added - """ + def add(self, edge: DependencySpec): key = edge.spec.name if self.store_by_child else edge.parent.name - current_list = self.edges.setdefault(key, []) - current_list.append(edge) - current_list.sort(key=_sort_by_dep_types) + if key in self.edges: + lst = self.edges[key] + lst.append(edge) + lst.sort(key=_sort_by_dep_types) + else: + self.edges[key] = [edge] def __str__(self): return "{deps: %s}" % ", ".join(str(d) for d in sorted(self.values())) -- cgit v1.2.3-70-g09d2