[pacman-dev] [PATCH] Optimize string comparison on 1 character compare
No need to do strcmp when we can check for direct equality, but is it worth the optimization? Signed-off-by: Dan McGee <dan@archlinux.org> --- lib/libalpm/be_files.c | 4 ++-- lib/libalpm/util.c | 2 +- src/pacman/sync.c | 4 ++-- src/pacman/util.c | 2 +- src/util/testdb.c | 2 +- 5 files changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/libalpm/be_files.c b/lib/libalpm/be_files.c index 078136e..74932d2 100644 --- a/lib/libalpm/be_files.c +++ b/lib/libalpm/be_files.c @@ -170,7 +170,7 @@ pmpkg_t *_alpm_db_scan(pmdb_t *db, const char *target) /* search for a specific package (by name only) */ rewinddir(db->handle); while(!found && (ent = readdir(db->handle)) != NULL) { - if(!strcmp(ent->d_name, ".") || !strcmp(ent->d_name, "..")) { + if(*(ent->d_name) == '.' || !strcmp(ent->d_name, "..")) { continue; } /* stat the entry, make sure it's a directory */ @@ -201,7 +201,7 @@ pmpkg_t *_alpm_db_scan(pmdb_t *db, const char *target) if(ent == NULL) { return(NULL); } - if(!strcmp(ent->d_name, ".") || !strcmp(ent->d_name, "..")) { + if(*(ent->d_name) == '.' || !strcmp(ent->d_name, "..")) { isdir = 0; continue; } diff --git a/lib/libalpm/util.c b/lib/libalpm/util.c index 2a42948..3ce1f04 100644 --- a/lib/libalpm/util.c +++ b/lib/libalpm/util.c @@ -463,7 +463,7 @@ int _alpm_rmrf(const char *path) for(dp = readdir(dirp); dp != NULL; dp = readdir(dirp)) { if(dp->d_ino) { sprintf(name, "%s/%s", path, dp->d_name); - if(strcmp(dp->d_name, "..") && strcmp(dp->d_name, ".")) { + if(strcmp(dp->d_name, "..") && *(dp->d_name) != '.') { errflag += _alpm_rmrf(name); } } diff --git a/src/pacman/sync.c b/src/pacman/sync.c index 58e616e..e9c16f5 100644 --- a/src/pacman/sync.c +++ b/src/pacman/sync.c @@ -61,7 +61,7 @@ static int sync_cleandb(const char *dbpath) { int found = 0; char *dname = ent->d_name; - if(!strcmp(dname, ".") || !strcmp(dname, "..")) { + if(*dname == '.' || !strcmp(dname, "..")) { continue; } /* skip the local and sync directories */ @@ -145,7 +145,7 @@ static int sync_cleancache(int level) char path[PATH_MAX]; pmpkg_t *localpkg = NULL, *dbpkg = NULL; - if(!strcmp(ent->d_name, ".") || !strcmp(ent->d_name, "..")) { + if(*(ent->d_name) == '.' || !strcmp(ent->d_name, "..")) { continue; } /* build the full filepath */ diff --git a/src/pacman/util.c b/src/pacman/util.c index a925be3..5025b3f 100644 --- a/src/pacman/util.c +++ b/src/pacman/util.c @@ -154,7 +154,7 @@ int rmrf(const char *path) if(dp->d_ino) { char name[PATH_MAX]; sprintf(name, "%s/%s", path, dp->d_name); - if(strcmp(dp->d_name, "..") && strcmp(dp->d_name, ".")) { + if(strcmp(dp->d_name, "..") && *(dp->d_name) != '.') { errflag += rmrf(name); } } diff --git a/src/util/testdb.c b/src/util/testdb.c index 31b4ff0..c3f86d8 100644 --- a/src/util/testdb.c +++ b/src/util/testdb.c @@ -75,7 +75,7 @@ static int db_test(char *dbpath) } while ((ent = readdir(dir)) != NULL) { - if(!strcmp(ent->d_name, ".") || !strcmp(ent->d_name, "..")) { + if(*(ent->d_name) == '.' || !strcmp(ent->d_name, "..")) { continue; } /* check for desc, depends, and files */ -- 1.5.3.6
On Tue, Nov 27, 2007 at 12:07:37AM -0600, Dan McGee wrote:
No need to do strcmp when we can check for direct equality, but is it worth the optimization?
I'm not sure. What does strcmp do anyway when one of its argument is only 1 char? Does it still read the other arg totally? Well, there is still the function call that is saved, but I couldn't measure any difference.
On Nov 27, 2007 2:03 AM, Xavier <shiningxc@gmail.com> wrote:
On Tue, Nov 27, 2007 at 12:07:37AM -0600, Dan McGee wrote:
No need to do strcmp when we can check for direct equality, but is it worth the optimization?
I'm not sure. What does strcmp do anyway when one of its argument is only 1 char? Does it still read the other arg totally? Well, there is still the function call that is saved, but I couldn't measure any difference.
Heh- this will blow up on any file that starts with ".", so ignore my useless spam here. -Dan
On Tue, Nov 27, 2007 at 12:06:18PM -0600, Dan McGee wrote:
On Nov 27, 2007 2:03 AM, Xavier <shiningxc@gmail.com> wrote:
On Tue, Nov 27, 2007 at 12:07:37AM -0600, Dan McGee wrote:
No need to do strcmp when we can check for direct equality, but is it worth the optimization?
I'm not sure. What does strcmp do anyway when one of its argument is only 1 char? Does it still read the other arg totally? Well, there is still the function call that is saved, but I couldn't measure any difference.
Heh- this will blow up on any file that starts with ".", so ignore my useless spam here.
Indeed, I noticed that, and I didn't think it was problematic. But well, I didn't like that patch much in any cases, so if you agree, then it's alright :)
participants (3)
-
Dan McGee
-
Dan McGee
-
Xavier