From 34a78d935ac44fc73becc8bba213cdf0268bb5e0 Mon Sep 17 00:00:00 2001 From: Dan McGee Date: Mon, 20 Dec 2010 19:58:15 -0600 Subject: Remove need for memory allocation in _alpm_depcmp Noticed when tweaking testdb, when we run _alpm_depcmp in loops and call it seven million times, the strdup()/free() combo can add up. Remove the need for any string duplication by some pointer manipulation and use of strncmp instead of strcmp. Also kill the function logger and add an escape so we don't needlessly retrieve the list of provides. Signed-off-by: Dan McGee --- lib/libalpm/deps.c | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) (limited to 'lib/libalpm/deps.c') diff --git a/lib/libalpm/deps.c b/lib/libalpm/deps.c index 21578293..e37eb02e 100644 --- a/lib/libalpm/deps.c +++ b/lib/libalpm/deps.c @@ -322,9 +322,6 @@ static int dep_vercmp(const char *version1, pmdepmod_t mod, int _alpm_depcmp(pmpkg_t *pkg, pmdepend_t *dep) { alpm_list_t *i; - - ALPM_LOG_FUNC; - const char *pkgname = alpm_pkg_get_name(pkg); const char *pkgversion = alpm_pkg_get_version(pkg); int satisfy = 0; @@ -332,22 +329,29 @@ int _alpm_depcmp(pmpkg_t *pkg, pmdepend_t *dep) /* check (pkg->name, pkg->version) */ satisfy = (strcmp(pkgname, dep->name) == 0 && dep_vercmp(pkgversion, dep->mod, dep->version)); + if(satisfy) { + return(satisfy); + } /* check provisions, format : "name=version" */ for(i = alpm_pkg_get_provides(pkg); i && !satisfy; i = i->next) { - char *provname = strdup(i->data); - char *provver = strchr(provname, '='); + const char *provision = i->data; + const char *provver = strchr(provision, '='); if(provver == NULL) { /* no provision version */ satisfy = (dep->mod == PM_DEP_MOD_ANY - && strcmp(provname, dep->name) == 0); + && strcmp(provision, dep->name) == 0); } else { - *provver = '\0'; + /* This is a bit tricker than the old code for performance reasons. To + * prevent the need to copy and duplicate strings, strncmp only the name + * portion if they are the same length, since there is a version and + * operator in play here. */ + size_t namelen = provver - provision; provver += 1; - satisfy = (strcmp(provname, dep->name) == 0 + satisfy = (strlen(dep->name) == namelen + && strncmp(provision, dep->name, namelen) == 0 && dep_vercmp(provver, dep->mod, dep->version)); } - free(provname); } return(satisfy); -- cgit v1.2.3-24-g4f1b