From b1cd84e804ac286d8ab1d6c67e4ae8177531066b Mon Sep 17 00:00:00 2001 From: Sami Kerola Date: Mon, 11 Jul 2011 00:08:45 +0200 Subject: [PATCH] getdata: new xstrstr function for performance The strstr is changed to xstrstr, which short cuts to points in string where first differences are expected to found. This made the tool tiny bit quicker. Signed-off-by: Sami Kerola --- src/dhcpd-pools.h | 1 + src/getdata.c | 10 +++++----- src/other.c | 26 ++++++++++++++++++++++++++ 3 files changed, 32 insertions(+), 5 deletions(-) diff --git a/src/dhcpd-pools.h b/src/dhcpd-pools.h index 7c32ecb..4d785fe 100644 --- a/src/dhcpd-pools.h +++ b/src/dhcpd-pools.h @@ -162,6 +162,7 @@ void *safe_malloc(const size_t size) ; void *safe_realloc(void *ptr, const size_t size); char *safe_strdup(const char *str) __attribute__ ((nonnull(1))); +int xstrstr(char *a, char *b, int len); void print_version(void) __attribute__ ((noreturn)); void usage(int status) __attribute__ ((noreturn)); /* qsort required functions... */ diff --git a/src/getdata.c b/src/getdata.c index bce5ce4..8a0786d 100644 --- a/src/getdata.c +++ b/src/getdata.c @@ -134,14 +134,14 @@ int parse_leases(void) config.dhcpdlease_file); } /* It's a lease, save IP */ - if (strstr(line, "lease") == line) { + if (xstrstr(line, "lease", 5)) { strncpy(ipstring, line, MAXLEN); nth_field(2, ipstring, ipstring); inet_aton(ipstring, &inp); sw_active_lease = 0; } /* Copy IP to correct array */ - else if (strstr(line, "binding state active")) { + else if (xstrstr(line, " binding state active", 22)) { leases[num_leases] = htonl(inp.s_addr); num_leases++; if (leasesmallocsize < num_leases) { @@ -151,7 +151,7 @@ int parse_leases(void) leasesmallocsize /= sizeof(uint32_t); } sw_active_lease = 1; - } else if (strstr(line, " binding state free")) { + } else if (xstrstr(line, " binding state free", 20)) { touches[num_touches] = htonl(inp.s_addr); num_touches++; if (touchesmallocsize < num_touches) { @@ -161,7 +161,7 @@ int parse_leases(void) safe_realloc(touches, touchesmallocsize); touchesmallocsize /= sizeof(uint32_t); } - } else if (strstr(line, " binding state backup")) { + } else if (xstrstr(line, " binding state backup", 22)) { if (num_backups == 0) { backups = safe_malloc(sizeof(uint32_t) * @@ -180,7 +180,7 @@ int parse_leases(void) if ((macaddr != NULL) && (sw_active_lease == 1) - && (strstr(line, "hardware ethernet"))) { + && (xstrstr(line, " hardware ethernet", 19))) { nth_field(3, macstring, line); macstring[17] = '\0'; macaddr_p->ethernet = safe_strdup(macstring); diff --git a/src/other.c b/src/other.c index c18f3ac..f489775 100644 --- a/src/other.c +++ b/src/other.c @@ -78,6 +78,32 @@ void *safe_realloc(void *ptr, const size_t size) return ret; } +int +#if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3) + __attribute__ ((hot)) +#endif + xstrstr(char *a, char *b, int len) +{ + int i; + /* two spaces are very common in lease file, after them + * nearly everything differs */ + if (a[2] != b[2]) { + return false; + } + /* " binding state " == 16 chars, this will skip right + * to first difering line. */ + if (17 < len && a[17] != b[17]) { + return false; + } + /* looking good, double check the whole thing... */ + for (i = 0; a[i] != '\0' && b[i] != '\0'; i++) { + if (a[i] != b[i]) { + return false; + } + } + return true; +} + /* Simple strdup wrapper */ char *safe_strdup(const char *str) {