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 <kerolasa@iki.fi>
This commit is contained in:
Sami Kerola 2011-07-11 00:08:45 +02:00
parent 8c6a3d9a4a
commit b1cd84e804
3 changed files with 32 additions and 5 deletions

View file

@ -162,6 +162,7 @@ void *safe_malloc(const size_t size)
; ;
void *safe_realloc(void *ptr, const size_t size); void *safe_realloc(void *ptr, const size_t size);
char *safe_strdup(const char *str) __attribute__ ((nonnull(1))); char *safe_strdup(const char *str) __attribute__ ((nonnull(1)));
int xstrstr(char *a, char *b, int len);
void print_version(void) __attribute__ ((noreturn)); void print_version(void) __attribute__ ((noreturn));
void usage(int status) __attribute__ ((noreturn)); void usage(int status) __attribute__ ((noreturn));
/* qsort required functions... */ /* qsort required functions... */

View file

@ -134,14 +134,14 @@ int parse_leases(void)
config.dhcpdlease_file); config.dhcpdlease_file);
} }
/* It's a lease, save IP */ /* It's a lease, save IP */
if (strstr(line, "lease") == line) { if (xstrstr(line, "lease", 5)) {
strncpy(ipstring, line, MAXLEN); strncpy(ipstring, line, MAXLEN);
nth_field(2, ipstring, ipstring); nth_field(2, ipstring, ipstring);
inet_aton(ipstring, &inp); inet_aton(ipstring, &inp);
sw_active_lease = 0; sw_active_lease = 0;
} }
/* Copy IP to correct array */ /* 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); leases[num_leases] = htonl(inp.s_addr);
num_leases++; num_leases++;
if (leasesmallocsize < num_leases) { if (leasesmallocsize < num_leases) {
@ -151,7 +151,7 @@ int parse_leases(void)
leasesmallocsize /= sizeof(uint32_t); leasesmallocsize /= sizeof(uint32_t);
} }
sw_active_lease = 1; 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); touches[num_touches] = htonl(inp.s_addr);
num_touches++; num_touches++;
if (touchesmallocsize < num_touches) { if (touchesmallocsize < num_touches) {
@ -161,7 +161,7 @@ int parse_leases(void)
safe_realloc(touches, touchesmallocsize); safe_realloc(touches, touchesmallocsize);
touchesmallocsize /= sizeof(uint32_t); touchesmallocsize /= sizeof(uint32_t);
} }
} else if (strstr(line, " binding state backup")) { } else if (xstrstr(line, " binding state backup", 22)) {
if (num_backups == 0) { if (num_backups == 0) {
backups = backups =
safe_malloc(sizeof(uint32_t) * safe_malloc(sizeof(uint32_t) *
@ -180,7 +180,7 @@ int parse_leases(void)
if ((macaddr != NULL) if ((macaddr != NULL)
&& (sw_active_lease == 1) && (sw_active_lease == 1)
&& (strstr(line, "hardware ethernet"))) { && (xstrstr(line, " hardware ethernet", 19))) {
nth_field(3, macstring, line); nth_field(3, macstring, line);
macstring[17] = '\0'; macstring[17] = '\0';
macaddr_p->ethernet = safe_strdup(macstring); macaddr_p->ethernet = safe_strdup(macstring);

View file

@ -78,6 +78,32 @@ void *safe_realloc(void *ptr, const size_t size)
return ret; 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 */ /* Simple strdup wrapper */
char *safe_strdup(const char *str) char *safe_strdup(const char *str)
{ {