Skip to content

Commit

Permalink
Optimize: Use seperate hash tables for each materialbind_t namespace.
Browse files Browse the repository at this point in the history
  • Loading branch information
danij committed Feb 13, 2009
1 parent 7d76644 commit e10b592
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions doomsday/engine/portable/src/p_materialmanager.c
Expand Up @@ -42,7 +42,7 @@
// MACROS ------------------------------------------------------------------

#define MATERIALS_BLOCK_ALLOC (32) // Num materials to allocate per block.
#define MATERIALS_NAME_HASH_SIZE (512)
#define MATERIAL_NAME_HASH_SIZE (512)

// TYPES -------------------------------------------------------------------

Expand Down Expand Up @@ -97,7 +97,7 @@ static material_t* materialsHead; // Head of the linked list of materials.

static materialbind_t* materialBinds;
static materialnum_t maxMaterialBinds;
static uint hashTable[MATERIALS_NAME_HASH_SIZE];
static uint hashTable[NUM_MATERIAL_NAMESPACES][MATERIAL_NAME_HASH_SIZE];

// CODE --------------------------------------------------------------------

Expand All @@ -124,7 +124,7 @@ static boolean isKnownMNamespace(material_namespace_t mnamespace)

/**
* This is a hash function. Given a material name it generates a
* somewhat-random number between 0 and MATERIALS_NAME_HASH_SIZE.
* somewhat-random number between 0 and MATERIAL_NAME_HASH_SIZE.
*
* @return The generated hash index.
*/
Expand All @@ -147,7 +147,7 @@ static uint hashForName(const char* name)
}
}

return key % MATERIALS_NAME_HASH_SIZE;
return key % MATERIAL_NAME_HASH_SIZE;
}

/**
Expand All @@ -163,17 +163,18 @@ static materialnum_t getMaterialNumForName(const char* name, uint hash,
material_namespace_t mnamespace)
{
// Go through the candidates.
if(hashTable[hash])
if(hashTable[mnamespace][hash])
{
materialbind_t* mb = &materialBinds[hashTable[hash] - 1];
materialbind_t* mb = &materialBinds[
hashTable[mnamespace][hash] - 1];

for(;;)
{
material_t* mat;

mat = mb->mat;

if(mat->mnamespace == mnamespace && !strncmp(mb->name, name, 8))
if(!strncmp(mb->name, name, 8))
return ((mb) - materialBinds) + 1;

if(!mb->hashNext)
Expand Down Expand Up @@ -214,6 +215,7 @@ static materialnum_t getMaterialNumForIndex(uint idx,
}

static void newMaterialNameBinding(material_t* mat, const char* name,
material_namespace_t mnamespace,
uint hash)
{
materialbind_t* mb;
Expand All @@ -233,8 +235,8 @@ static void newMaterialNameBinding(material_t* mat, const char* name,
mb->mat = mat;

// We also hash the name for faster searching.
mb->hashNext = hashTable[hash];
hashTable[hash] = (mb - materialBinds) + 1;
mb->hashNext = hashTable[mnamespace][hash];
hashTable[mnamespace][hash] = (mb - materialBinds) + 1;
}

/**
Expand Down Expand Up @@ -526,7 +528,7 @@ Con_Message("P_MaterialCreate: Warning, attempted to create material "
mat = createMaterial(width, height, flags, mnamespace, def, tex);

// Now create a name binding for it.
newMaterialNameBinding(mat, name, hash);
newMaterialNameBinding(mat, name, mnamespace, hash);

return mat;
}
Expand Down

0 comments on commit e10b592

Please sign in to comment.