From bb19ac0926424c1403acdfbeebd11a7adef2389c Mon Sep 17 00:00:00 2001 From: Jim Warner Date: Sun, 30 Jul 2017 00:00:00 -0500 Subject: [PATCH] top: at program startup, avoid a little extra overhead There was a time when the PROCPS_PIDS_noop represented the highest valued enumerator. Therefore if one wished to prime an array consisting of pids_result structures with this specific item a loop would have been needed. Now that this enum is the first one, with the value of zero, we can avoid avoid such a loop with just calloc. But just in case, we'll use an 'if' to guarantee zero. [ and the nice thing is, since the value is known at ] [ compile time, that 'if' test plus subordinate loop ] [ can be discarded by the compiler as long as it's 0 ] Reference(s): . introduced (PIDS_noop > 0) commit 7e6a371d8a36b250a2edddff9f5d059640b8132e . top employs PIDS_noop at 'new' time commit f1bd82ff07d67dbcfa455e82678f44376cfe1a90 . relocated PIDS_noop (PIDS_noop == 0) commit e7585992d9c0743246247b3d6ee0041942fe07d5 Signed-off-by: Jim Warner --- top/top.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/top/top.c b/top/top.c index d0fe2784..b4b355ca 100644 --- a/top/top.c +++ b/top/top.c @@ -2821,9 +2821,10 @@ static void before (char *me) { // establish max depth for newlib pids stack (# of result structs) Pids_itms = alloc_c(sizeof(enum pids_item) * MAXTBL(Fieldstab)); - for (i = 0; i < MAXTBL(Fieldstab); i++) - Pids_itms[i] = PIDS_noop; - Pids_itms_cur = i; + if (PIDS_noop != 0) + for (i = 0; i < MAXTBL(Fieldstab); i++) + Pids_itms[i] = PIDS_noop; + Pids_itms_cur = MAXTBL(Fieldstab); // we will identify specific items in the build_headers() function if (procps_pids_new(&Pids_ctx, Pids_itms, Pids_itms_cur)) error_exit(fmtmk(N_fmt(LIB_errorpid_fmt),__LINE__));