chiark / gitweb /
util: Avoid memory allocations for formatting paths
authorHolger Hans Peter Freyther <holger@moiji-mobile.com>
Sat, 6 Apr 2013 08:05:59 +0000 (10:05 +0200)
committerLennart Poettering <lennart@poettering.net>
Mon, 8 Apr 2013 13:26:32 +0000 (15:26 +0200)
commitab7d9b674e8eb1388dff4d44def1b661892832e6
tree240db615184028e12ff1743389625c6151dfb752
parentb6b7d4337976eeac610b9ed2c3e1fd596a247b14
util: Avoid memory allocations for formatting paths

Avoid memory allocations to construct the path for files in the
procfs. The procfs paths are way shorter than the PATH_MAX so we
can use snprintf on a string located on the stack. This shows up
as a win on x86 using the benchmark program below.

$ make libsystemd-shared.la; gcc -O2 -Isrc/systemd/ -Isrc/ \
-o simple-perf-test simple-perf-test.c \
.libs/libsystemd-shared.a  -lrt

 #include "shared/util.h"
void test_once(void) {
pid_t pid = getpid();
char *tmp = NULL;

get_process_comm(pid, &tmp);
free(tmp);
tmp = NULL;
get_process_cmdline(pid, 0, 1, &tmp);
free(tmp);
is_kernel_thread(pid);
tmp = NULL;
get_process_exe(pid, &tmp);
free(tmp);
}

int main(int argc, char **argv)
{
int i;
for (i = 0; i < 50000; ++i)
test_once();
}
src/shared/util.c