Skip to content

Commit 6e720ab

Browse files
authored
Merge pull request #3664 from techee/kw_table
main: add quick path for looking up too long strings in the keyword table
2 parents 5a55af2 + 9a02e92 commit 6e720ab

File tree

1 file changed

+25
-4
lines changed

1 file changed

+25
-4
lines changed

main/keyword.c

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ typedef struct sHashEntry {
3636
*/
3737
static const unsigned int TableSize = 2039; /* prime */
3838
static hashEntry **HashTable = NULL;
39+
static unsigned int MaxEntryLen = 0;
3940

4041
/*
4142
* FUNCTION DEFINITIONS
@@ -70,7 +71,8 @@ static hashEntry *getHashTableEntry (unsigned long hashedValue)
7071
return entry;
7172
}
7273

73-
static unsigned int hashValue (const char *const string, langType language)
74+
static unsigned int hashValue (const char *const string, langType language,
75+
unsigned int maxLen, bool *maxLenReached)
7476
{
7577
const signed char *p;
7678
unsigned int h = 5381;
@@ -79,11 +81,19 @@ static unsigned int hashValue (const char *const string, langType language)
7981

8082
/* "djb" hash as used in g_str_hash() in glib */
8183
for (p = (const signed char *)string; *p != '\0'; p++)
84+
{
8285
h = (h << 5) + h + tolower (*p);
86+
if (p - (const signed char *)string > maxLen)
87+
{
88+
*maxLenReached = true;
89+
return 0;
90+
}
91+
}
8392

8493
/* consider language as an extra "character" and add it to the hash */
8594
h = (h << 5) + h + language;
8695

96+
*maxLenReached = false;
8797
return h;
8898
}
8999

@@ -107,8 +117,13 @@ static hashEntry *newEntry (
107117
*/
108118
extern void addKeyword (const char *const string, langType language, int value)
109119
{
110-
const unsigned int index = hashValue (string, language) % TableSize;
120+
bool dummy;
121+
const unsigned int index = hashValue (string, language, 1000, &dummy) % TableSize;
111122
hashEntry *entry = getHashTableEntry (index);
123+
size_t len = strlen (string);
124+
125+
if (len > MaxEntryLen)
126+
MaxEntryLen = len;
112127

113128
if (entry == NULL)
114129
{
@@ -139,10 +154,16 @@ extern void addKeyword (const char *const string, langType language, int value)
139154

140155
static int lookupKeywordFull (const char *const string, bool caseSensitive, langType language)
141156
{
142-
const unsigned int index = hashValue (string, language) % TableSize;
143-
hashEntry *entry = getHashTableEntry (index);
157+
bool maxLenReached;
158+
const unsigned int index = hashValue (string, language, MaxEntryLen, &maxLenReached) % TableSize;
159+
hashEntry *entry;
144160
int result = KEYWORD_NONE;
145161

162+
if (maxLenReached)
163+
return KEYWORD_NONE;
164+
165+
entry = getHashTableEntry (index);
166+
146167
while (entry != NULL)
147168
{
148169
if (language == entry->language &&

0 commit comments

Comments
 (0)