forked from github/server
more intelligent hashing = less divisions
This commit is contained in:
parent
3cd617fe39
commit
9a343df8fc
2 changed files with 12 additions and 6 deletions
|
@ -75,7 +75,7 @@ const char *
|
||||||
locale_getstring(const locale * lang, const char * key)
|
locale_getstring(const locale * lang, const char * key)
|
||||||
{
|
{
|
||||||
unsigned int hkey = hashstring(key);
|
unsigned int hkey = hashstring(key);
|
||||||
unsigned int id = hkey % SMAXHASH;
|
unsigned int id = hkey & (SMAXHASH-1);
|
||||||
const struct locale_str * find;
|
const struct locale_str * find;
|
||||||
|
|
||||||
assert(lang);
|
assert(lang);
|
||||||
|
@ -94,7 +94,7 @@ locale_string(const locale * lang, const char * key)
|
||||||
if (key==NULL) return NULL;
|
if (key==NULL) return NULL;
|
||||||
else {
|
else {
|
||||||
unsigned int hkey = hashstring(key);
|
unsigned int hkey = hashstring(key);
|
||||||
unsigned int id = hkey % SMAXHASH;
|
unsigned int id = hkey & (SMAXHASH-1);
|
||||||
struct locale_str * find;
|
struct locale_str * find;
|
||||||
|
|
||||||
if (key == NULL || *key==0) return NULL;
|
if (key == NULL || *key==0) return NULL;
|
||||||
|
@ -129,16 +129,23 @@ locale_setstring(locale * lang, const char * key, const char * value)
|
||||||
{
|
{
|
||||||
int nval = atoi(key);
|
int nval = atoi(key);
|
||||||
unsigned int hkey = nval?nval:hashstring(key);
|
unsigned int hkey = nval?nval:hashstring(key);
|
||||||
unsigned int id = hkey % SMAXHASH;
|
unsigned int id = hkey & (SMAXHASH-1);
|
||||||
struct locale_str * find;
|
struct locale_str * find;
|
||||||
|
static int maxs = 0, totals = 0;
|
||||||
|
int si=0;
|
||||||
if (lang==NULL) lang=default_locale;
|
if (lang==NULL) lang=default_locale;
|
||||||
find = lang->strings[id];
|
find = lang->strings[id];
|
||||||
while (find) {
|
while (find) {
|
||||||
|
++si;
|
||||||
if (find->hashkey==hkey && !strcmp(key, find->key)) break;
|
if (find->hashkey==hkey && !strcmp(key, find->key)) break;
|
||||||
find=find->nexthash;
|
find=find->nexthash;
|
||||||
}
|
}
|
||||||
if (!find) {
|
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 = calloc(1, sizeof(struct locale_str));
|
||||||
find->nexthash = lang->strings[id];
|
find->nexthash = lang->strings[id];
|
||||||
lang->strings[id] = find;
|
lang->strings[id] = find;
|
||||||
|
|
|
@ -5,8 +5,7 @@
|
||||||
* feel that you need to include it, it's a sure sign that you're trying to
|
* feel that you need to include it, it's a sure sign that you're trying to
|
||||||
* do something BAD. */
|
* do something BAD. */
|
||||||
|
|
||||||
#define SMAXHASH 512
|
#define SMAXHASH 2048
|
||||||
|
|
||||||
typedef struct locale_str {
|
typedef struct locale_str {
|
||||||
unsigned int hashkey;
|
unsigned int hashkey;
|
||||||
struct locale_str * nexthash;
|
struct locale_str * nexthash;
|
||||||
|
|
Loading…
Reference in a new issue