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.
This commit is contained in:
Harmen Stoppels 2023-08-16 14:33:03 +02:00 committed by GitHub
parent 08f9c7670e
commit 4f07205c63
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -985,16 +985,14 @@ def __iter__(self):
def __len__(self): def __len__(self):
return len(self.edges) return len(self.edges)
def add(self, edge): def add(self, edge: DependencySpec):
"""Adds a new edge to this object.
Args:
edge (DependencySpec): edge to be added
"""
key = edge.spec.name if self.store_by_child else edge.parent.name key = edge.spec.name if self.store_by_child else edge.parent.name
current_list = self.edges.setdefault(key, []) if key in self.edges:
current_list.append(edge) lst = self.edges[key]
current_list.sort(key=_sort_by_dep_types) lst.append(edge)
lst.sort(key=_sort_by_dep_types)
else:
self.edges[key] = [edge]
def __str__(self): def __str__(self):
return "{deps: %s}" % ", ".join(str(d) for d in sorted(self.values())) return "{deps: %s}" % ", ".join(str(d) for d in sorted(self.values()))