From 2915e711f77d68dff756babd19af8da1677c4549 Mon Sep 17 00:00:00 2001 From: Maria Matejka Date: Mon, 25 Feb 2019 23:28:36 +0100 Subject: Custom number parser to speed up config parsing The glibc's generic parser is slow due to its versatility. Specialized parsers for base-10 and base-16 are much faster and we don't use other bases. --- lib/Makefile | 2 +- lib/ip.c | 2 +- lib/string.h | 4 ++++ lib/strtoul.c | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 67 insertions(+), 2 deletions(-) create mode 100644 lib/strtoul.c (limited to 'lib') diff --git a/lib/Makefile b/lib/Makefile index 01f3114d..18816bb3 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -1,4 +1,4 @@ -src := bitops.c checksum.c event.c flowspec.c idm.c ip.c lists.c mac.c md5.c mempool.c net.c patmatch.c printf.c resource.c sha1.c sha256.c sha512.c slab.c slists.c tbf.c timer.c xmalloc.c +src := bitops.c checksum.c event.c flowspec.c idm.c ip.c lists.c mac.c md5.c mempool.c net.c patmatch.c printf.c resource.c sha1.c sha256.c sha512.c slab.c slists.c strtoul.c tbf.c timer.c xmalloc.c obj := $(src-o-files) $(all-daemon) diff --git a/lib/ip.c b/lib/ip.c index 9497248c..0f5a5348 100644 --- a/lib/ip.c +++ b/lib/ip.c @@ -245,7 +245,7 @@ ip4_pton(const char *a, ip4_addr *o) char *d, *c = strchr(a, '.'); if (!c != !i) return 0; - l = strtoul(a, &d, 10); + l = bstrtoul(a, &d, 10); if (((d != c) && *d) || (l > 255)) return 0; ia = (ia << 8) | l; diff --git a/lib/string.h b/lib/string.h index 0d34f9c5..5f7c4666 100644 --- a/lib/string.h +++ b/lib/string.h @@ -24,6 +24,10 @@ int buffer_vprint(buffer *buf, const char *fmt, va_list args); int buffer_print(buffer *buf, const char *fmt, ...); void buffer_puts(buffer *buf, const char *str); +#define bstrtoul(str, end, base) bstrtoul##base(str, end) +u64 bstrtoul10(const char *str, char **end); +u64 bstrtoul16(const char *str, char **end); + int patmatch(const byte *pat, const byte *str); static inline char *xbasename(const char *str) diff --git a/lib/strtoul.c b/lib/strtoul.c new file mode 100644 index 00000000..44a1bb1d --- /dev/null +++ b/lib/strtoul.c @@ -0,0 +1,61 @@ +/* + * BIRD Library -- Parse numbers + * + * (c) 2019 Maria Matejka + * + * Can be freely distributed and used under the terms of the GNU GPL. + */ + +#include "nest/bird.h" +#include "lib/string.h" + +#include + +#define ULI_MAX_DIV10 (UINT64_MAX / 10) +#define ULI_MAX_MOD10 (UINT64_MAX % 10) + +u64 +bstrtoul10(const char *str, char **end) +{ + u64 out = 0; + for (*end = (char *) str; (**end >= '0') && (**end <= '9'); (*end)++) { + u64 digit = **end - '0'; + if ((out > ULI_MAX_DIV10) || + (out == ULI_MAX_DIV10) && (digit > ULI_MAX_MOD10)) { + errno = ERANGE; + return UINT64_MAX; + } + + out *= 10; + out += (**end) - '0'; + } + return out; +} + +u64 +bstrtoul16(const char *str, char **end) +{ + u64 out = 0; + for (int i=0; i<=(64/4); i++) { + switch (str[i]) { + case '0' ... '9': + out *= 16; + out += str[i] - '0'; + break; + case 'a' ... 'f': + out *= 16; + out += str[i] + 10 - 'a'; + break; + case 'A' ... 'F': + out *= 16; + out += str[i] + 10 - 'A'; + break; + default: + *end = (char *) &(str[i]); + return out; + } + } + + errno = ERANGE; + return UINT64_MAX; +} -- cgit v1.2.3