From acba2e265e115e9d80a570ce083a9ff9ac46a93f Mon Sep 17 00:00:00 2001 From: Sami Kerola Date: Sun, 11 Nov 2012 23:29:07 +0000 Subject: [PATCH] (un)likely: use __builtin_expect to inform expected path Remove also unnecessary comparison in getdata nth_field(). Signed-off-by: Sami Kerola --- configure.ac | 14 ++++++++++++++ src/dhcpd-pools.h | 8 ++++++++ src/getdata.c | 4 ++-- src/other.c | 2 +- 4 files changed, 25 insertions(+), 3 deletions(-) diff --git a/configure.ac b/configure.ac index c16e995..3f7a8df 100644 --- a/configure.ac +++ b/configure.ac @@ -69,6 +69,20 @@ AC_CHECK_FUNCS([\ strrchr \ ]) +AC_MSG_CHECKING([if the compiler supports __builtin_expect]) +AC_TRY_COMPILE(, [ + return __builtin_expect(1, 1) ? 1 : 0 +], [ + have_builtin_expect=yes + AC_MSG_RESULT([yes]) +], [ + have_builtin_expect=no + AC_MSG_RESULT([no]) +]) +if test x$have_builtin_expect = xyes ; then + AC_DEFINE([HAVE_BUILTIN_EXPECT], [1], [Define to 1 if the compiler supports __builtin_expect.]) +fi + AC_CONFIG_FILES([Makefile lib/Makefile man/Makefile diff --git a/src/dhcpd-pools.h b/src/dhcpd-pools.h index 5795d7d..8d07eb6 100644 --- a/src/dhcpd-pools.h +++ b/src/dhcpd-pools.h @@ -93,6 +93,14 @@ inline char *prog_inv_sh_nm_from_file(char *f, char stripext) # endif #endif +#ifdef HAVE_BUILTIN_EXPECT +# define likely(x) __builtin_expect(!!(x), 1) +# define unlikely(x) __builtin_expect(!!(x), 0) +#else +# define likely(x) (x) +# define unlikely(x) (x) +#endif + /* Structures and unions */ struct configuration_t { char *dhcpdconf_file; diff --git a/src/getdata.c b/src/getdata.c index 57bb2b5..5c6016c 100644 --- a/src/getdata.c +++ b/src/getdata.c @@ -175,7 +175,7 @@ void nth_field(char *restrict dest, const char *restrict src) len = strlen(src); for (i = 0; i < len; i++) { dest[i] = src[i]; - if (src[i] == ' ' || dest[i] == '\0') { + if (unlikely(src[i] == ' ')) { dest[i] = '\0'; break; } @@ -233,7 +233,7 @@ void parse_config(int is_include, const char *restrict config_file, #endif /* POSIX_FADV_SEQUENTIAL */ /* Very hairy stuff begins. */ - while (!feof(dhcpd_config)) { + while (unlikely(!feof(dhcpd_config))) { c = fgetc(dhcpd_config); /* Certain characters are magical */ switch (c) { diff --git a/src/other.c b/src/other.c index dd5e00d..6b619fb 100644 --- a/src/other.c +++ b/src/other.c @@ -55,7 +55,7 @@ int int i; /* two spaces are very common in lease file, after them * nearly everything differs */ - if (a[2] != b[2]) { + if (likely(a[2] != b[2])) { return false; } /* " binding state " == 16 chars, this will skip right