Skip to content

Commit 6326781

Browse files
committed
String hashing improvements
- appStrdupPool() is now using 1Mb (256k items) hash table (just to mention: Fortnite is hashing 1.5M strings) - increased GAME_FILE_HASH_SIZE - slightly cleaned up hash functions
1 parent 2366c50 commit 6326781

File tree

3 files changed

+12
-16
lines changed

3 files changed

+12
-16
lines changed

Unreal/GameFileSystem.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,7 @@ static TArray<CGameFileInfo*> GameFiles;
140140
int GNumPackageFiles = 0;
141141
int GNumForeignFiles = 0;
142142

143-
#define GAME_FILE_HASH_SIZE 16384
144-
#define GAME_FILE_HASH_MASK (GAME_FILE_HASH_SIZE-1)
143+
#define GAME_FILE_HASH_SIZE 32768
145144

146145
//#define PRINT_HASH_DISTRIBUTION 1
147146
//#define DEBUG_HASH 1
@@ -164,15 +163,15 @@ static int GetHashForFileName(const char* FileName, bool cutExtension)
164163
const char* s2 = cutExtension ? strrchr(s1, '.') : NULL;
165164
int len = (s2 != NULL) ? s2 - s1 : strlen(s1);
166165

167-
uint16 hash = 0;
166+
unsigned int hash = 0;
168167
for (int i = 0; i < len; i++)
169168
{
170169
char c = s1[i];
171170
if (c >= 'A' && c <= 'Z') c += 'a' - 'A'; // lowercase a character
172-
hash = ROL16(hash, 5) - hash + ((c << 4) + c ^ 0x13F); // some crazy hash function
171+
// hash = ROL16(hash, 5) - hash + ((c << 4) + c ^ 0x13F); // some crazy hash function
172+
hash = ROL16(hash, 1) + c; // note: if we'll use more than 16-bit GAME_FILE_HASH_SIZE value, should use ROL32 here
173173
}
174-
// hash += (len << 6) - len;
175-
hash &= GAME_FILE_HASH_MASK;
174+
hash &= (GAME_FILE_HASH_SIZE - 1);
176175
#ifdef DEBUG_HASH_NAME
177176
if (strstr(FileName, DEBUG_HASH_NAME))
178177
printf("-> hash[%s] (%s,%d) -> %X\n", FileName, s1, len, hash);

Unreal/PackageUtils.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -227,8 +227,6 @@ static void ScanPackageExports(UnPackage* package, CGameFileInfo* file)
227227
} */
228228
}
229229

230-
//void PrintStringHashDistribution();
231-
232230
bool ScanContent(const TArray<const CGameFileInfo*>& Packages, IProgressCallback* Progress)
233231
{
234232
#if PROFILE
@@ -273,7 +271,10 @@ bool ScanContent(const TArray<const CGameFileInfo*>& Packages, IProgressCallback
273271
}
274272
scanned = true;
275273
}
276-
// PrintStringHashDistribution();
274+
#if 0
275+
void PrintStringHashDistribution();
276+
PrintStringHashDistribution();
277+
#endif
277278
#if PROFILE
278279
if (scanned)
279280
appPrintProfiler("Scanned packages");

Unreal/UnCore.cpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,7 @@ void FString::TrimStartAndEndInline()
455455
FName (string) pool
456456
-----------------------------------------------------------------------------*/
457457

458-
#define STRING_HASH_SIZE 32768
458+
#define STRING_HASH_SIZE (65536*4) // 1Mb of 32-bit pointers
459459

460460
struct CStringPoolEntry
461461
{
@@ -470,15 +470,11 @@ static CMemoryChain* StringPool;
470470
const char* appStrdupPool(const char* str)
471471
{
472472
int len = strlen(str);
473-
int hash = 0;
473+
unsigned int hash = 0;
474474
for (int i = 0; i < len; i++)
475475
{
476476
char c = str[i];
477-
#if 0
478-
hash = (hash + c) ^ 0xABCDEF;
479-
#else
480-
hash = ROL16(hash, 5) - hash + ((c << 4) + c ^ 0x13F); // some crazy hash function
481-
#endif
477+
hash = ROL32(hash, 1) + c;
482478
}
483479
hash &= (STRING_HASH_SIZE - 1);
484480

0 commit comments

Comments
 (0)