Skip to content

Instantly share code, notes, and snippets.

@jperkin
Created August 7, 2023 07:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jperkin/a60680d0a60a3db89774cce884f5404a to your computer and use it in GitHub Desktop.
Save jperkin/a60680d0a60a3db89774cce884f5404a to your computer and use it in GitHub Desktop.
Optimisation for "pkg_admin rebuild-tree"
Index: files/add/perform.c
===================================================================
RCS file: /cvsroot/pkgsrc/pkgtools/pkg_install/files/add/perform.c,v
retrieving revision 1.121
diff -u -r1.121 perform.c
--- files/add/perform.c 21 Oct 2021 13:05:25 -0000 1.121
+++ files/add/perform.c 7 Aug 2023 07:25:57 -0000
@@ -451,7 +451,7 @@
return -1;
}
*iter = '\0';
- pkg->other_version = find_best_matching_installed_pkg(pkgbase, 0);
+ pkg->other_version = find_matching_installed_pkg(pkgbase, 0, 1);
free(pkgbase);
if (pkg->other_version == NULL)
return 0;
@@ -1129,7 +1129,7 @@
warnx("Can't install dependency %s, continuing", dep);
}
- if (find_best_matching_installed_pkg(dep, 0) == NULL) {
+ if (find_matching_installed_pkg(dep, 0, 1) == NULL) {
if (!ForceDepends) {
warnx("Just installed dependency %s disappeared", dep);
return 1;
@@ -1160,7 +1160,7 @@
} else if (p->type != PLIST_PKGDEP)
continue;
- if (find_best_matching_installed_pkg(p->name, 0) == NULL) {
+ if (find_matching_installed_pkg(p->name, 0, 1) == NULL) {
if (install_depend_pkg(p->name) != 0) {
status = -1;
break;
@@ -1179,7 +1179,7 @@
} else if (p->type != PLIST_PKGDEP)
continue;
- best_installed = find_best_matching_installed_pkg(p->name, 0);
+ best_installed = find_matching_installed_pkg(p->name, 0, 1);
if (best_installed == NULL) {
warnx("Expected dependency %s still missing", p->name);
return -1;
Index: files/admin/main.c
===================================================================
RCS file: /cvsroot/pkgsrc/pkgtools/pkg_install/files/admin/main.c,v
retrieving revision 1.69
diff -u -r1.69 main.c
--- files/admin/main.c 2 Dec 2020 10:45:47 -0000 1.69
+++ files/admin/main.c 7 Aug 2023 07:25:57 -0000
@@ -312,7 +312,7 @@
char *best_installed;
int i;
- best_installed = find_best_matching_installed_pkg(pattern, 1);
+ best_installed = find_matching_installed_pkg(pattern, 1, 0);
if (best_installed == NULL) {
warnx("Dependency %s of %s unresolved", pattern, pkgname);
return;
Index: files/create/perform.c
===================================================================
RCS file: /cvsroot/pkgsrc/pkgtools/pkg_install/files/create/perform.c,v
retrieving revision 1.28
diff -u -r1.28 perform.c
--- files/create/perform.c 1 Jul 2020 10:03:20 -0000 1.28
+++ files/create/perform.c 7 Aug 2023 07:25:57 -0000
@@ -68,7 +68,7 @@
cp = strsep(&deps, " \t\n");
if (*cp) {
char *best_installed;
- best_installed = find_best_matching_installed_pkg(cp, 1);
+ best_installed = find_matching_installed_pkg(cp, 1, 1);
if (best_installed != NULL) {
add_plist(plist, PLIST_BLDDEP, best_installed);
if (Verbose && !PlistOnly && build_only)
Index: files/info/perform.c
===================================================================
RCS file: /cvsroot/pkgsrc/pkgtools/pkg_install/files/info/perform.c,v
retrieving revision 1.64
diff -u -r1.64 perform.c
--- files/info/perform.c 1 Jul 2020 10:03:20 -0000 1.64
+++ files/info/perform.c 7 Aug 2023 07:25:57 -0000
@@ -566,13 +566,13 @@
{
char *pattern, *best_match;
- best_match = find_best_matching_installed_pkg(pkgname, 1);
+ best_match = find_matching_installed_pkg(pkgname, 1, 1);
if (best_match == NULL) {
if (ispkgpattern(pkgname))
return 1;
pattern = xasprintf("%s-[0-9]*", pkgname);
- best_match = find_best_matching_installed_pkg(pattern, 1);
+ best_match = find_matching_installed_pkg(pattern, 1, 1);
free(pattern);
}
Index: files/lib/iterate.c
===================================================================
RCS file: /cvsroot/pkgsrc/pkgtools/pkg_install/files/lib/iterate.c,v
retrieving revision 1.10
diff -u -r1.10 iterate.c
--- files/lib/iterate.c 2 Dec 2020 12:10:50 -0000 1.10
+++ files/lib/iterate.c 7 Aug 2023 07:25:57 -0000
@@ -379,19 +379,43 @@
}
/*
- * Returns a copy of the name of best matching package.
+ * Only save the first matching package, ignoring any further matches.
+ */
+static int
+match_first_installed(const char *pkg, void *cookie)
+{
+ struct best_installed_match_arg *arg = cookie;
+
+ if (arg->best_current_match)
+ return 0;
+
+ if (pkg_match(arg->pattern, pkg))
+ arg->best_current_match = xstrdup(pkg);
+
+ return 0;
+}
+
+/*
+ * Returns a copy of the name of a matching installed package for pattern.
* If no package matched the pattern or an error occured, return NULL.
*
* If use_cached is set, return a cached match entry if it exists, and also use
* the iterate_pkg_db cache, otherwise clear any matching cache entry and use
* regular iterate_pkg_db().
+ *
+ * If best is set, return what pkg_match() considers to be the best match,
+ * otherwise just return the first match. Given the limitations on installed
+ * packages, and that it should be impossible for two packages with the same
+ * name but different versions to be installed, it is unlikely that searching
+ * for the best match is useful, and it can be a lot more expensive.
*/
char *
-find_best_matching_installed_pkg(const char *pattern, int use_cached)
+find_matching_installed_pkg(const char *pattern, int use_cached, int best)
{
struct best_installed_match_arg arg;
struct pkg_match_list *pkg;
int idx = PKG_HASH_ENTRY(pattern), rv;
+ int (*matchiter)(const char *, void *);
if (pattern == NULL)
return NULL;
@@ -412,10 +436,12 @@
arg.pattern = pattern;
arg.best_current_match = NULL;
+ matchiter = (best) ? match_best_installed : match_first_installed;
+
if (use_cached)
- rv = iterate_pkg_db_cached(match_best_installed, &arg);
+ rv = iterate_pkg_db_cached(matchiter, &arg);
else
- rv = iterate_pkg_db(match_best_installed, &arg);
+ rv = iterate_pkg_db(matchiter, &arg);
if (rv == -1) {
warnx("could not process pkgdb");
Index: files/lib/lib.h
===================================================================
RCS file: /cvsroot/pkgsrc/pkgtools/pkg_install/files/lib/lib.h,v
retrieving revision 1.72
diff -u -r1.72 lib.h
--- files/lib/lib.h 11 Dec 2020 10:06:53 -0000 1.72
+++ files/lib/lib.h 7 Aug 2023 07:25:57 -0000
@@ -306,7 +306,7 @@
int add_installed_pkgs_by_basename(const char *, lpkg_head_t *);
int add_installed_pkgs_by_pattern(const char *, lpkg_head_t *);
-char *find_best_matching_installed_pkg(const char *, int);
+char *find_matching_installed_pkg(const char *, int, int);
char *find_best_matching_file(const char *, const char *, int, int);
int match_installed_pkgs(const char *, int (*)(const char *, void *), void *);
int match_local_files(const char *, int, int, const char *, int (*cb)(const char *, void *), void *);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment