[pacman-dev] [PATCH] _alpm_db_search-cleanup: remove needless regex compilation, string duplication and code cleanup
Jürgen Hötzel
juergen at hoetzel.info
Tue Jan 16 18:15:52 EST 2007
Hi,
* avoid repeated regex compilations (regex for search string do not
change while scanning the package database)
* remove needless string duplication (regex function do not change target
string nor free them)
* code cleanup
This patch improves search performance:
bash-3.2$ time ./src/pacman/pacman.static.old -Ss "(database|web).*server" >/dev/null
real 0m1.026s
user 0m0.544s
sys 0m0.208s
bash-3.2$ time ./src/pacman/pacman.static -Ss "(database|web).*server" >/dev/null
real 0m0.777s
user 0m0.456s
sys 0m0.128s
bash-3.2$
Jürgen
-------------- next part --------------
Index: lib/libalpm/db.c
===================================================================
RCS file: /home/cvs-pacman/pacman-lib/lib/libalpm/db.c,v
retrieving revision 1.52
diff -u -r1.52 db.c
--- lib/libalpm/db.c 21 Dec 2006 01:53:41 -0000 1.52
+++ lib/libalpm/db.c 16 Jan 2007 22:39:42 -0000
@@ -37,6 +37,7 @@
#include <sys/stat.h>
#include <dirent.h>
#include <libintl.h>
+#include <regex.h>
#ifdef CYGWIN
#include <limits.h> /* PATH_MAX */
#endif
@@ -101,68 +102,47 @@
for(i = needles; i; i = i->next) {
char *targ;
- int retval;
if(i->data == NULL) {
continue;
}
- targ = strdup(i->data);
+ targ = i->data;
_alpm_log(PM_LOG_DEBUG, "searching for target '%s'", targ);
for(j = _alpm_db_get_pkgcache(db, INFRQ_DESC|INFRQ_DEPENDS); j; j = j->next) {
pmpkg_t *pkg = j->data;
- char *haystack;
- int match = 0;
-
- /* check name */
- haystack = strdup(pkg->name);
- retval = _alpm_reg_match(haystack, targ);
- if(retval < 0) {
+ char *matched = NULL;
+ regex_t reg;
+
+ if(regcomp(®, targ, REG_EXTENDED | REG_NOSUB | REG_ICASE) != 0) {
/* bad regexp */
- FREE(haystack);
- return(NULL);
- } else if(retval) {
- _alpm_log(PM_LOG_DEBUG, " search target '%s' matched '%s'", targ, haystack);
- match = 1;
- }
- FREE(haystack);
-
- /* check description */
- if(!match) {
- haystack = strdup(pkg->desc);
- retval = _alpm_reg_match(haystack, targ);
- if(retval < 0) {
- /* bad regexp */
- FREE(haystack);
- return(NULL);
- } else if(retval) {
- match = 1;
- }
- FREE(haystack);
+ RET_ERR(PM_ERR_INVALID_REGEX, NULL);
}
+ /* check name */
+ if (regexec(®, pkg->name, 0, 0, 0) == 0) {
+ matched = pkg->name;
+ }
+ /* check desc */
+ else if (regexec(®, pkg->desc, 0, 0, 0) == 0) {
+ matched = pkg->desc;
+ }
/* check provides */
- if(!match) {
+ else {
for(k = pkg->provides; k; k = k->next) {
- haystack = strdup(k->data);
- retval = _alpm_reg_match(haystack, targ);
- if(retval < 0) {
- /* bad regexp */
- FREE(haystack);
- return(NULL);
- } else if(retval) {
- match = 1;
+ if (regexec(®, k->data, 0, 0, 0) == 0) {
+ matched = k->data;
+ break;
}
- FREE(haystack);
}
}
+ regfree(®);
- if(match) {
+ if(matched != NULL) {
+ _alpm_log(PM_LOG_DEBUG, " search target '%s' matched '%s'", targ, matched);
ret = _alpm_list_add(ret, pkg);
}
}
-
- FREE(targ);
}
return(ret);
Index: lib/libalpm/util.c
===================================================================
RCS file: /home/cvs-pacman/pacman-lib/lib/libalpm/util.c,v
retrieving revision 1.35
diff -u -r1.35 util.c
--- lib/libalpm/util.c 22 Nov 2006 09:03:42 -0000 1.35
+++ lib/libalpm/util.c 16 Jan 2007 22:39:43 -0000
@@ -54,7 +54,6 @@
#ifndef __sun__
#include <mntent.h>
#endif
-#include <regex.h>
/* pacman */
#include "log.h"
@@ -619,20 +618,6 @@
}
#endif
-/* match a string against a regular expression */
-int _alpm_reg_match(char *string, char *pattern)
-{
- int result;
- regex_t reg;
-
- if(regcomp(®, pattern, REG_EXTENDED | REG_NOSUB | REG_ICASE) != 0) {
- RET_ERR(PM_ERR_INVALID_REGEX, -1);
- }
- result = regexec(®, string, 0, 0, 0);
- regfree(®);
- return(!(result));
-}
-
/* convert a time_t to a string - buffer MUST be large enough for
* YYYYMMDDHHMMSS - 15 chars */
void _alpm_time2string(time_t t, char *buffer)
Index: lib/libalpm/util.h
===================================================================
RCS file: /home/cvs-pacman/pacman-lib/lib/libalpm/util.h,v
retrieving revision 1.15
diff -u -r1.15 util.h
--- lib/libalpm/util.h 22 Nov 2006 09:03:42 -0000 1.15
+++ lib/libalpm/util.h 16 Jan 2007 22:39:43 -0000
@@ -70,7 +70,6 @@
int _alpm_check_freespace(pmtrans_t *trans, pmlist_t **data);
#endif
#endif
-int _alpm_reg_match(char *string, char *pattern);
void _alpm_time2string(time_t t, char *buffer);
#ifdef __sun__
char* strsep(char** str, const char* delims);
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
URL: <http://archlinux.org/pipermail/pacman-dev/attachments/20070117/f9bab49e/attachment.pgp>
More information about the pacman-dev
mailing list