From a47d03085190de4d58dfe379f72f71b5991f5007 Mon Sep 17 00:00:00 2001 From: Juan RP Date: Tue, 25 Jan 2011 14:56:37 +0100 Subject: [PATCH] xbps_sort_pkg_deps: optimize one more time, it is 5% faster approx. --- lib/transaction_sortdeps.c | 38 ++++++++++++++++++++------------------ 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/lib/transaction_sortdeps.c b/lib/transaction_sortdeps.c index a8962b7c..f392864e 100644 --- a/lib/transaction_sortdeps.c +++ b/lib/transaction_sortdeps.c @@ -56,6 +56,7 @@ xbps_sort_pkg_deps(void) const char *pkgname, *pkgver, *str; char *pkgnamedep; int rv = 0; + bool done; if ((transd = xbps_transaction_dictionary_get()) == NULL) return EINVAL; @@ -78,12 +79,20 @@ xbps_sort_pkg_deps(void) prop_dictionary_set(transd, "packages", sorted); return 0; } + /* + * The sorted array should have the same capacity than + * all objects in the unsorted array. + */ ndeps = prop_array_count(unsorted); - + if (!prop_array_ensure_capacity(sorted, ndeps)) { + xbps_error_printf("failed to set capacity to the sorted " + "pkgdeps array\n"); + return ENOMEM; + } iter = prop_array_iterator(unsorted); if (iter == NULL) { - prop_object_release(sorted); - return ENOMEM; + rv = ENOMEM; + goto out; } again: /* @@ -93,14 +102,16 @@ again: prop_dictionary_get_cstring_nocopy(obj, "pkgname", &pkgname); prop_dictionary_get_cstring_nocopy(obj, "pkgver", &pkgver); xbps_dbg_printf("Sorting package '%s': ", pkgver); - - if (xbps_find_pkg_in_dict_by_name(transd, - "packages", pkgname)) { + /* + * Check if package was sorted previously and skip. + */ + done = false; + prop_dictionary_get_bool(obj, "sorted", &done); + if (done) { xbps_dbg_printf_append("skipping, already queued.\n", pkgname); continue; } - /* * Packages that don't have deps go unsorted, because * it doesn't matter. @@ -110,11 +121,7 @@ again: xbps_dbg_printf_append("added (no rundeps) into " "the sorted queue.\n"); prop_array_add(sorted, obj); - if (!xbps_remove_pkg_from_dict(transd, - "unsorted_deps", pkgname)) { - xbps_dbg_printf("can't remove %s from " - "unsorted_deps array!\n", pkgname); - } + prop_dictionary_set_bool(obj, "sorted", true); cnt++; continue; } @@ -123,7 +130,6 @@ again: rv = ENOMEM; goto out; } - /* * Iterate over the run_depends array, and find out if they * were already added in the sorted list. @@ -164,11 +170,7 @@ again: /* Add dependency if all its required deps are already added */ if (prop_array_count(rundeps) == rundepscnt) { prop_array_add(sorted, obj); - if (!xbps_remove_pkg_from_dict(transd, - "unsorted_deps", pkgname)) { - xbps_dbg_printf("can't remove %s from " - "unsorted_deps array!\n", pkgname); - } + prop_dictionary_set_bool(obj, "sorted", true); xbps_dbg_printf("Added package '%s' to the sorted " "queue (all rundeps satisfied).\n\n", pkgver); rundepscnt = 0;