From e119a2914202f5d7bf0d62d56ab6b480870bd01a Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sat, 4 Feb 2006 23:41:23 +0000 Subject: [PATCH] inline optimizations for string functions a little more speed for locale_string (less strcmp) --- src/common/util/bsdstring.c | 5 ---- src/common/util/bsdstring.h | 4 --- src/common/util/goodies.c | 32 ------------------------ src/common/util/goodies.h | 7 +++++- src/common/util/language.c | 28 ++++++++++----------- src/common/util/util.vcproj | 49 ++++++++++++++++++++++++++++++++++--- 6 files changed, 65 insertions(+), 60 deletions(-) diff --git a/src/common/util/bsdstring.c b/src/common/util/bsdstring.c index ba6670a4e..e0ccffe0c 100644 --- a/src/common/util/bsdstring.c +++ b/src/common/util/bsdstring.c @@ -2,11 +2,6 @@ #include #include "bsdstring.h" -#ifndef HAVE_INLINE -# undef INLINE_FUNCTION -# define INLINE_FUNCTION -#endif - #if !defined(HAVE_STRLCPY) INLINE_FUNCTION size_t strlcpy(char *dst, const char *src, size_t siz) /* copied from OpenBSD source code */ diff --git a/src/common/util/bsdstring.h b/src/common/util/bsdstring.h index 1f0ce6792..c94643a06 100644 --- a/src/common/util/bsdstring.h +++ b/src/common/util/bsdstring.h @@ -1,10 +1,6 @@ #ifndef UTIL_BSDSTRING_H #define UTIL_BSDSTRING_H -#ifndef NDEBUG -# undef HAVE_INLINE -#endif - #if !defined(HAVE_STRLCPY) # ifdef HAVE_INLINE # include "bsdstring.c" diff --git a/src/common/util/goodies.c b/src/common/util/goodies.c index e28e3b2e5..057e1811f 100644 --- a/src/common/util/goodies.c +++ b/src/common/util/goodies.c @@ -57,38 +57,6 @@ intlist_find(int *i_p, int fi) return NULL; } -unsigned int -hashstring(const char* s) -{ - unsigned int key = 0; - while (*s) { - key = key*37 + *s++; - } - return key % 0x7FFFFFFF; -} - -const char * -escape_string(const char * str, char * buffer, unsigned int len) -{ - static char s_buffer[4096]; - const char * p = str; - char * o; - if (buffer==NULL) { - buffer = s_buffer; - len = sizeof(s_buffer); - } - o = buffer; - do { - switch (*p) { - case '\"': - case '\\': - (*o++) = '\\'; - } - (*o++) = (*p); - } while (*p++); - return buffer; -} - char * set_string (char **s, const char *neu) { diff --git a/src/common/util/goodies.h b/src/common/util/goodies.h index 4aa60ea50..76d575eb1 100644 --- a/src/common/util/goodies.h +++ b/src/common/util/goodies.h @@ -21,9 +21,14 @@ extern "C" { extern int *intlist_init(void); extern int *intlist_add(int *i_p, int i); extern int *intlist_find(int *i_p, int i); -extern unsigned int hashstring(const char* s); +#ifdef HAVE_INLINE +# include "strings.c" +#else +extern unsigned int hashstring(const char* s); extern const char *escape_string(const char * str, char * buffer, unsigned int len); +#endif + extern boolean locale_check(void); extern int set_email(char** pemail, const char *newmail); diff --git a/src/common/util/language.c b/src/common/util/language.c index 88998dc81..ec2d891c2 100644 --- a/src/common/util/language.c +++ b/src/common/util/language.c @@ -91,8 +91,7 @@ locale_getstring(const locale * lang, const char * key) const char * locale_string(const locale * lang, const char * key) { - if (key==NULL) return NULL; - else { + if (key!=NULL) { unsigned int hkey = hashstring(key); unsigned int id = hkey & (SMAXHASH-1); struct locale_str * find; @@ -101,7 +100,14 @@ locale_string(const locale * lang, const char * key) if (lang == NULL) return key; find = lang->strings[id]; while (find) { - if (find->hashkey == hkey && !strcmp(key, find->key)) break; + if (find->hashkey == hkey) { + if (find->nexthash==NULL) { + /* if this is the only entry with this hash, fine. */ + assert(strcmp(key, find->key)==0); + break; + } + if (strcmp(key, find->key)==0) break; + } find = find->nexthash; } if (!find) { @@ -122,30 +128,22 @@ locale_string(const locale * lang, const char * key) } return find->str; } + return NULL; } void locale_setstring(locale * lang, const char * key, const char * value) { - int nval = atoi(key); - unsigned int hkey = nval?nval:hashstring(key); + unsigned int hkey = hashstring(key); unsigned int id = hkey & (SMAXHASH-1); struct locale_str * find; - static int maxs = 0, totals = 0; - int si=0; if (lang==NULL) lang=default_locale; find = lang->strings[id]; while (find) { - ++si; - if (find->hashkey==hkey && !strcmp(key, find->key)) break; - find=find->nexthash; + if (find->hashkey==hkey && strcmp(key, find->key)==0) break; + find = find->nexthash; } if (!find) { - ++totals; - if (si>=maxs) { - maxs=si+1; - printf("max hash conflicts: %d/%d\n", maxs, totals); - } find = calloc(1, sizeof(struct locale_str)); find->nexthash = lang->strings[id]; lang->strings[id] = find; diff --git a/src/common/util/util.vcproj b/src/common/util/util.vcproj index 3b943754f..70d4dde02 100644 --- a/src/common/util/util.vcproj +++ b/src/common/util/util.vcproj @@ -251,15 +251,58 @@ RelativePath=".\xml.h"> + + + + + + + + + + + + + + + + + + + + + + + + - -