[pacman-dev] [PATCH 1/2] _alpm_lstat: only duplicate string if necessary
The vast majority of the time we will just be passing the same string value on to the lstat() call. The only time we need to duplicate it is if the path ends in '/'. In one run using a profiler, only 400 of the 200,000 calls (0.2%) required the string to be copied first. Signed-off-by: Dan McGee <dan@archlinux.org> --- lib/libalpm/util.c | 15 ++++++++------- 1 files changed, 8 insertions(+), 7 deletions(-) diff --git a/lib/libalpm/util.c b/lib/libalpm/util.c index 9a8f06d..357ce50 100644 --- a/lib/libalpm/util.c +++ b/lib/libalpm/util.c @@ -636,17 +636,18 @@ const char *_alpm_filecache_setup(pmhandle_t *handle) int _alpm_lstat(const char *path, struct stat *buf) { int ret; - char *newpath = strdup(path); - size_t len = strlen(newpath); + size_t len = strlen(path); /* strip the trailing slash if one exists */ - if(len != 0 && newpath[len - 1] == '/') { - newpath[len - 1] = '\0'; + if(len != 0 && path[len - 1] == '/') { + char *newpath = strdup(path); + newpath[len - 1] = '\0'; + ret = lstat(newpath, buf); + free(newpath); + } else { + ret = lstat(path, buf); } - ret = lstat(newpath, buf); - - FREE(newpath); return ret; } -- 1.7.5.2
The few remaining instances were utilized for buffers in calls to snprintf() and realpath(). Both of these functions will always ensure the returned value is padded with '\0', so there is no need for the extra byte. Signed-off-by: Dan McGee <dan@archlinux.org> --- lib/libalpm/add.c | 2 +- lib/libalpm/conflict.c | 6 +++--- lib/libalpm/handle.c | 2 +- lib/libalpm/remove.c | 4 ++-- src/pacman/query.c | 2 +- 5 files changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/libalpm/add.c b/lib/libalpm/add.c index 4ea6c34..4c9a98c 100644 --- a/lib/libalpm/add.c +++ b/lib/libalpm/add.c @@ -469,7 +469,7 @@ static int commit_single_pkg(pmhandle_t *handle, pmpkg_t *newpkg, size_t pkg_current, size_t pkg_count) { int i, ret = 0, errors = 0; - char scriptlet[PATH_MAX+1]; + char scriptlet[PATH_MAX]; int is_upgrade = 0; pmpkg_t *oldpkg = NULL; pmdb_t *db = handle->db_local; diff --git a/lib/libalpm/conflict.c b/lib/libalpm/conflict.c index b64604f..66441a7 100644 --- a/lib/libalpm/conflict.c +++ b/lib/libalpm/conflict.c @@ -377,7 +377,7 @@ alpm_list_t *_alpm_db_find_fileconflicts(pmhandle_t *handle, for(current = 0, i = upgrade; i; i = i->next, current++) { alpm_list_t *k, *tmpfiles = NULL; pmpkg_t *p1, *p2, *dbpkg; - char path[PATH_MAX+1]; + char path[PATH_MAX]; p1 = i->data; if(!p1) { @@ -500,9 +500,9 @@ alpm_list_t *_alpm_db_find_fileconflicts(pmhandle_t *handle, } if(!resolved_conflict && dbpkg) { - char *rpath = calloc(PATH_MAX+1, sizeof(char)); + char *rpath = calloc(PATH_MAX, sizeof(char)); if(!realpath(path, rpath)) { - FREE(rpath); + free(rpath); continue; } char *filestr = rpath + strlen(handle->root); diff --git a/lib/libalpm/handle.c b/lib/libalpm/handle.c index 898ce41..3c17e9d 100644 --- a/lib/libalpm/handle.c +++ b/lib/libalpm/handle.c @@ -264,7 +264,7 @@ enum _pmerrno_t _alpm_set_directory_option(const char *value, if(stat(path, &st) == -1 || !S_ISDIR(st.st_mode)) { return PM_ERR_NOT_A_DIR; } - real = calloc(PATH_MAX + 1, sizeof(char)); + real = calloc(PATH_MAX, sizeof(char)); if(!realpath(path, real)) { free(real); return PM_ERR_NOT_A_DIR; diff --git a/lib/libalpm/remove.c b/lib/libalpm/remove.c index a0b6c41..f7ed68e 100644 --- a/lib/libalpm/remove.c +++ b/lib/libalpm/remove.c @@ -194,7 +194,7 @@ int _alpm_remove_prepare(pmhandle_t *handle, alpm_list_t **data) static int can_remove_file(pmhandle_t *handle, const char *path, alpm_list_t *skip) { - char file[PATH_MAX+1]; + char file[PATH_MAX]; snprintf(file, PATH_MAX, "%s%s", handle->root, path); @@ -223,7 +223,7 @@ static void unlink_file(pmhandle_t *handle, pmpkg_t *info, char *filename, alpm_list_t *skip_remove, int nosave) { struct stat buf; - char file[PATH_MAX+1]; + char file[PATH_MAX]; snprintf(file, PATH_MAX, "%s%s", handle->root, filename); diff --git a/src/pacman/query.c b/src/pacman/query.c index 938fff6..eb34071 100644 --- a/src/pacman/query.c +++ b/src/pacman/query.c @@ -40,7 +40,7 @@ static char *resolve_path(const char *file) { char *str = NULL; - str = calloc(PATH_MAX + 1, sizeof(char)); + str = calloc(PATH_MAX, sizeof(char)); if(!str) { return NULL; } -- 1.7.5.2
participants (1)
-
Dan McGee