Util: Slightly optimize table lookups

This commit is contained in:
Jeffrey Pfau 2016-11-03 01:04:38 -07:00
parent 8eb38e0c3e
commit 4332653af2
1 changed files with 14 additions and 20 deletions

View File

@ -11,21 +11,21 @@
#define LIST_INITIAL_SIZE 8
#define TABLE_INITIAL_SIZE 8
#define TABLE_COMPARATOR(LIST, INDEX) LIST->list[(INDEX)].key == key
#define HASH_TABLE_COMPARATOR(LIST, INDEX) LIST->list[(INDEX)].key == hash && strncmp(LIST->list[(INDEX)].stringKey, key, LIST->list[(INDEX)].keylen) == 0
#define TABLE_COMPARATOR(TUPLE) TUPLE->key == key
#define HASH_TABLE_COMPARATOR(TUPLE) TUPLE->key == hash && strncmp(TUPLE->stringKey, key, TUPLE->keylen) == 0
#define TABLE_LOOKUP_START(COMPARATOR, LIST, KEY) \
#define TABLE_LOOKUP_START(COMPARATOR, KEY) \
uint32_t entry = (KEY) & (table->tableSize - 1); \
LIST = &table->table[entry]; \
struct TableList* list = &table->table[entry]; \
struct TableTuple* lookupResult = list->list; \
size_t i; \
for (i = 0; i < LIST->nEntries; ++i) { \
if (COMPARATOR(LIST, i)) { \
struct TableTuple* lookupResult = &LIST->list[i]; \
UNUSED(lookupResult);
for (i = list->nEntries; i--;) { \
if (COMPARATOR(lookupResult)) { \
#define TABLE_LOOKUP_END \
break; \
} \
++lookupResult; \
}
struct TableTuple {
@ -98,16 +98,14 @@ void TableDeinit(struct Table* table) {
}
void* TableLookup(const struct Table* table, uint32_t key) {
const struct TableList* list;
TABLE_LOOKUP_START(TABLE_COMPARATOR, list, key) {
TABLE_LOOKUP_START(TABLE_COMPARATOR, key) {
return lookupResult->value;
} TABLE_LOOKUP_END;
return 0;
}
void TableInsert(struct Table* table, uint32_t key, void* value) {
struct TableList* list;
TABLE_LOOKUP_START(TABLE_COMPARATOR, list, key) {
TABLE_LOOKUP_START(TABLE_COMPARATOR, key) {
if (value != lookupResult->value) {
table->deinitializer(lookupResult->value);
lookupResult->value = value;
@ -131,8 +129,7 @@ size_t TableSize(const struct Table* table) {
}
void TableRemove(struct Table* table, uint32_t key) {
struct TableList* list;
TABLE_LOOKUP_START(TABLE_COMPARATOR, list, key) {
TABLE_LOOKUP_START(TABLE_COMPARATOR, key) {
_removeItemFromList(table, list, i); // TODO: Move i out of the macro
} TABLE_LOOKUP_END;
}
@ -167,8 +164,7 @@ void TableEnumerate(const struct Table* table, void (handler(uint32_t key, void*
void* HashTableLookup(const struct Table* table, const char* key) {
uint32_t hash = hash32(key, strlen(key), 0);
const struct TableList* list;
TABLE_LOOKUP_START(HASH_TABLE_COMPARATOR, list, hash) {
TABLE_LOOKUP_START(HASH_TABLE_COMPARATOR, hash) {
return lookupResult->value;
} TABLE_LOOKUP_END;
return 0;
@ -176,8 +172,7 @@ void* HashTableLookup(const struct Table* table, const char* key) {
void HashTableInsert(struct Table* table, const char* key, void* value) {
uint32_t hash = hash32(key, strlen(key), 0);
struct TableList* list;
TABLE_LOOKUP_START(HASH_TABLE_COMPARATOR, list, hash) {
TABLE_LOOKUP_START(HASH_TABLE_COMPARATOR, hash) {
if (value != lookupResult->value) {
table->deinitializer(lookupResult->value);
lookupResult->value = value;
@ -193,8 +188,7 @@ void HashTableInsert(struct Table* table, const char* key, void* value) {
void HashTableRemove(struct Table* table, const char* key) {
uint32_t hash = hash32(key, strlen(key), 0);
struct TableList* list;
TABLE_LOOKUP_START(HASH_TABLE_COMPARATOR, list, hash) {
TABLE_LOOKUP_START(HASH_TABLE_COMPARATOR, hash) {
_removeItemFromList(table, list, i); // TODO: Move i out of the macro
} TABLE_LOOKUP_END;
}