From 3bbd6173cd4ad404f8ddf7f393e9a99cff7fddc0 Mon Sep 17 00:00:00 2001 From: Andy Fiddaman Date: Sat, 8 Aug 2026 12:06:34 +0100 Subject: [PATCH] Memoise version object construction when sorting catalog parts CatalogPart.sort() constructs a pkg.version.Version object for every entry each time a part is sorted. Version construction is expensive, and sorting happens on every catalog finalise - i.e. package publication, pkgrepo refresh/rebuild, and the client's image catalog rebuild. The same version string commonly recurs across many stems, because packages built and published together share a branch and, where the publisher stamps a build's packages with a shared timestamp, the whole version string. Construct each distinct version string only once per sort, via a memo dictionary local to the sort call. The win scales with the amount of duplication in the catalog. For a repository whose builds are published with a single shared timestamp, a full three-part sort drops from 2.00s to 0.35s. The OmniOS core repository currently has only minimal duplication (1.86x, from packages that happen to publish within the same second) and benefits proportionately less) In the pkg5 test suite, TestPkgRepo runs 39s faster, TestPkgSign 27s faster and TestLintEngineDepot 29% faster with this change. The publication path already honours a timestamp provided on the FMRI, so a repository can opt into the full benefit simply by publishing each build's packages with a shared timestamp (we should do this for core illumos-omnios packages which already have a shared 0.5.11 version stem). --- src/modules/catalog.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/modules/catalog.py b/src/modules/catalog.py index 4dcb3cecf..250af59b5 100644 --- a/src/modules/catalog.py +++ b/src/modules/catalog.py @@ -829,8 +829,20 @@ def sort(self, pfmris=None, pubs=None): If neither 'pfmris' or 'pubs' is provided, all entries will be sorted.""" + # Version objects are expensive to construct and the same + # version string commonly recurs across many stems (packages + # built and published together share branch and timestamp), so + # construct each distinct version only once per sort. + vcache = {} + def key_func(item): - return pkg.version.Version(item["version"]) + ver = item["version"] + try: + return vcache[ver] + except KeyError: + v = pkg.version.Version(ver) + vcache[ver] = v + return v self.load() if pfmris is not None: