Browse Source

Store the hashValue to avoid repeating the hash calculation during the hash resize.

pull/587/head
dota17 5 years ago
parent
commit
9d50d541be
2 changed files with 10 additions and 4 deletions
  1. +3
    -2
      linkhash.c
  2. +7
    -2
      linkhash.h

+ 3
- 2
linkhash.c View File

@@ -540,7 +540,7 @@ int lh_table_resize(struct lh_table *t, int new_size)


for (ent = t->head; ent != NULL; ent = ent->next) for (ent = t->head; ent != NULL; ent = ent->next)
{ {
unsigned long h = lh_get_hash(new_t, ent->k);
unsigned long h = ent->hash;
unsigned int opts = 0; unsigned int opts = 0;
if (ent->k_is_constant) if (ent->k_is_constant)
opts = JSON_C_OBJECT_KEY_IS_CONSTANT; opts = JSON_C_OBJECT_KEY_IS_CONSTANT;
@@ -581,7 +581,7 @@ int lh_table_insert_w_hash(struct lh_table *t, const void *k, const void *v, con
if (lh_table_resize(t, t->size * 2) != 0) if (lh_table_resize(t, t->size * 2) != 0)
return -1; return -1;


n = h % t->size;
n = h & (t->size - 1);


while (1) while (1)
{ {
@@ -591,6 +591,7 @@ int lh_table_insert_w_hash(struct lh_table *t, const void *k, const void *v, con
n = 0; n = 0;
} }


t->table[n].hash = h;
t->table[n].k = k; t->table[n].k = k;
t->table[n].k_is_constant = (opts & JSON_C_OBJECT_KEY_IS_CONSTANT); t->table[n].k_is_constant = (opts & JSON_C_OBJECT_KEY_IS_CONSTANT);
t->table[n].v = v; t->table[n].v = v;


+ 7
- 2
linkhash.h View File

@@ -84,15 +84,20 @@ typedef int(lh_equal_fn)(const void *k1, const void *k2);
*/ */
struct lh_entry struct lh_entry
{ {

/** /**
* The key. Use lh_entry_k() instead of accessing this directly.
*The hash. Use to determine the position of the entry in the array.
*/ */
const void *k;
uint32_t hash;
/** /**
* A flag for users of linkhash to know whether or not they * A flag for users of linkhash to know whether or not they
* need to free k. * need to free k.
*/ */
int k_is_constant; int k_is_constant;
/**
* The key. Use lh_entry_k() instead of accessing this directly.
*/
const void *k;
/** /**
* The value. Use lh_entry_v() instead of accessing this directly. * The value. Use lh_entry_v() instead of accessing this directly.
*/ */


Loading…
Cancel
Save