diff options
author | Tavian Barnes <tavianator@tavianator.com> | 2020-02-29 18:35:55 -0500 |
---|---|---|
committer | Tavian Barnes <tavianator@tavianator.com> | 2020-02-29 18:52:04 -0500 |
commit | 1c184e718f5745cf3a53a3c389814a792e73aa51 (patch) | |
tree | 0959d07a8e4ac507aefa4867156d144ce2eff1dd /eval.c | |
parent | 7fbf673ae2db33d6e47386bf3169d4b48f0fc8b4 (diff) | |
download | bfs-1c184e718f5745cf3a53a3c389814a792e73aa51.tar.xz |
passwd: Cache the user/group tables
This is a significant optimization for conditions that need these
tables:
Benchmark #1: ./bfs ~/code/linux -nouser >/dev/null
Time (mean ± σ): 232.0 ms ± 2.5 ms [User: 44.3 ms, System: 185.0 ms]
Range (min … max): 228.7 ms … 238.7 ms 12 runs
Benchmark #2: ./bfs-1.6 ~/code/linux -nouser >/dev/null
Time (mean ± σ): 1.050 s ± 0.012 s [User: 544.2 ms, System: 500.0 ms]
Range (min … max): 1.025 s … 1.063 s 10 runs
Benchmark #3: find ~/code/linux -nouser >/dev/null
Time (mean ± σ): 1.040 s ± 0.012 s [User: 533.6 ms, System: 500.6 ms]
Range (min … max): 1.017 s … 1.054 s 10 runs
Summary
'./bfs ~/code/linux -nouser >/dev/null' ran
4.48 ± 0.07 times faster than 'find ~/code/linux -nouser >/dev/null'
4.52 ± 0.07 times faster than './bfs-1.6 ~/code/linux -nouser >/dev/null'
Diffstat (limited to 'eval.c')
-rw-r--r-- | eval.c | 29 |
1 files changed, 7 insertions, 22 deletions
@@ -28,6 +28,7 @@ #include "exec.h" #include "fsade.h" #include "mtab.h" +#include "passwd.h" #include "printf.h" #include "stat.h" #include "time.h" @@ -294,16 +295,7 @@ bool eval_nogroup(const struct expr *expr, struct eval_state *state) { return false; } - errno = 0; - if (getgrgid(statbuf->gid) == NULL) { - if (errno == 0) { - return true; - } else { - eval_report_error(state); - } - } - - return false; + return bfs_getgrgid(state->cmdline->groups, statbuf->gid) == NULL; } /** @@ -315,16 +307,7 @@ bool eval_nouser(const struct expr *expr, struct eval_state *state) { return false; } - errno = 0; - if (getpwuid(statbuf->uid) == NULL) { - if (errno == 0) { - return true; - } else { - eval_report_error(state); - } - } - - return false; + return bfs_getpwuid(state->cmdline->users, statbuf->uid) == NULL; } /** @@ -603,6 +586,8 @@ bool eval_perm(const struct expr *expr, struct eval_state *state) { bool eval_fls(const struct expr *expr, struct eval_state *state) { CFILE *cfile = expr->cfile; FILE *file = cfile->file; + const struct bfs_users *users = state->cmdline->users; + const struct bfs_groups *groups = state->cmdline->groups; const struct BFTW *ftwbuf = state->ftwbuf; const struct bfs_stat *statbuf = eval_stat(state); if (!statbuf) { @@ -620,7 +605,7 @@ bool eval_fls(const struct expr *expr, struct eval_state *state) { } uintmax_t uid = statbuf->uid; - struct passwd *pwd = getpwuid(uid); + const struct passwd *pwd = users ? bfs_getpwuid(users, uid) : NULL; if (pwd) { if (fprintf(file, " %-8s", pwd->pw_name) < 0) { goto error; @@ -632,7 +617,7 @@ bool eval_fls(const struct expr *expr, struct eval_state *state) { } uintmax_t gid = statbuf->gid; - struct group *grp = getgrgid(gid); + const struct group *grp = groups ? bfs_getgrgid(groups, gid) : NULL; if (grp) { if (fprintf(file, " %-8s", grp->gr_name) < 0) { goto error; |