inline optimizations for string functions

a little more speed for locale_string (less strcmp)
This commit is contained in:
Enno Rehling 2006-02-04 23:41:23 +00:00
parent 9a343df8fc
commit e119a29142
6 changed files with 65 additions and 60 deletions

View file

@ -2,11 +2,6 @@
#include <string.h>
#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 */

View file

@ -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"

View file

@ -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)
{

View file

@ -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);

View file

@ -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;

View file

@ -251,15 +251,58 @@
RelativePath=".\xml.h">
</File>
</Filter>
<Filter
Name="Inline"
Filter="">
<File
RelativePath=".\bsdstring.c">
<FileConfiguration
Name="Release|Win32"
ExcludedFromBuild="TRUE">
<Tool
Name="VCCLCompilerTool"/>
</FileConfiguration>
<FileConfiguration
Name="Profile|Win32"
ExcludedFromBuild="TRUE">
<Tool
Name="VCCLCompilerTool"/>
</FileConfiguration>
<FileConfiguration
Name="Debug|Win32"
ExcludedFromBuild="TRUE">
<Tool
Name="VCCLCompilerTool"/>
</FileConfiguration>
</File>
<File
RelativePath=".\strings.c">
<FileConfiguration
Name="Release|Win32"
ExcludedFromBuild="TRUE">
<Tool
Name="VCCLCompilerTool"/>
</FileConfiguration>
<FileConfiguration
Name="Profile|Win32"
ExcludedFromBuild="TRUE">
<Tool
Name="VCCLCompilerTool"/>
</FileConfiguration>
<FileConfiguration
Name="Debug|Win32"
ExcludedFromBuild="TRUE">
<Tool
Name="VCCLCompilerTool"/>
</FileConfiguration>
</File>
</Filter>
<File
RelativePath=".\attrib.c">
</File>
<File
RelativePath=".\base36.c">
</File>
<File
RelativePath=".\bsdstring.c">
</File>
<File
RelativePath=".\command.c">
</File>