From 1c184e718f5745cf3a53a3c389814a792e73aa51 Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Sat, 29 Feb 2020 18:35:55 -0500 Subject: passwd: Cache the user/group tables MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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' --- eval.c | 29 +++++++---------------------- 1 file changed, 7 insertions(+), 22 deletions(-) (limited to 'eval.c') diff --git a/eval.c b/eval.c index 010fef9..bdf4d99 100644 --- a/eval.c +++ b/eval.c @@ -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; -- cgit v1.2.3