From cad8e0fd0b4b00cbb65314eeaa1d6d5bfad0b82b Mon Sep 17 00:00:00 2001 From: HiGarfield Date: Sat, 11 Jan 2025 14:40:46 +0800 Subject: [PATCH] Enhance get_ncpu function with static variable caching Implement static variable caching to prevent unnecessary repeated system calls. --- src/util.c | 37 ++++++++++++++++++++++++++----------- 1 file changed, 26 insertions(+), 11 deletions(-) diff --git a/src/util.c b/src/util.c index 3d9cdad9..8c37d3d4 100644 --- a/src/util.c +++ b/src/util.c @@ -55,28 +55,43 @@ void increase_priority(void) /* Get the number of CPUs */ int get_ncpu(void) { + static int cached_ncpu = -1; + + if (cached_ncpu > 0) + { + return cached_ncpu; + } + #if defined(_SC_NPROCESSORS_ONLN) - long ncpu = sysconf(_SC_NPROCESSORS_ONLN); - return ncpu > 0 ? (int)ncpu : 1; + { + long ncpu = sysconf(_SC_NPROCESSORS_ONLN); + cached_ncpu = ncpu > 0 ? (int)ncpu : 1; + } #elif defined(__APPLE__) || defined(__FreeBSD__) - int ncpu = 0; - int mib[2] = {CTL_HW, HW_AVAILCPU}; - size_t len = sizeof(ncpu); - if (sysctl(mib, 2, &ncpu, &len, NULL, 0) != 0 || ncpu < 1) { - mib[1] = HW_NCPU; + int ncpu = 0, mib[2] = {CTL_HW, HW_AVAILCPU}; + size_t len = sizeof(ncpu); if (sysctl(mib, 2, &ncpu, &len, NULL, 0) != 0 || ncpu < 1) { - return 1; + mib[1] = HW_NCPU; + if (sysctl(mib, 2, &ncpu, &len, NULL, 0) != 0 || ncpu < 1) + { + cached_ncpu = 1; + return 1; + } } + cached_ncpu = ncpu; } - return ncpu; #elif defined(__linux__) - int ncpu = get_nprocs(); - return ncpu > 0 ? ncpu : 1; + { + int ncpu = get_nprocs(); + cached_ncpu = ncpu > 0 ? ncpu : 1; + } #else #error "Unsupported platform" #endif + + return cached_ncpu; } pid_t get_pid_max(void)