mirror of
https://github.com/json-c/json-c.git
synced 2026-03-13 18:19:06 +08:00
Merge pull request #196 from rgerhards/improve-performance
Performance improvements
This commit is contained in:
109
linkhash.h
109
linkhash.h
@@ -41,6 +41,23 @@ extern "C" {
|
||||
*/
|
||||
#define LH_FREED (void*)-2
|
||||
|
||||
/**
|
||||
* default string hash function
|
||||
*/
|
||||
#define JSON_C_STR_HASH_DFLT 0
|
||||
|
||||
/**
|
||||
* perl-like string hash function
|
||||
*/
|
||||
#define JSON_C_STR_HASH_PERLLIKE 1
|
||||
|
||||
/**
|
||||
* This function sets the hash function to be used for strings.
|
||||
* Must be one of the JSON_C_STR_HASH_* values.
|
||||
* @returns 0 - ok, -1 if parameter was invalid
|
||||
*/
|
||||
int json_global_set_string_hash(const int h);
|
||||
|
||||
struct lh_entry;
|
||||
|
||||
/**
|
||||
@@ -64,6 +81,7 @@ struct lh_entry {
|
||||
* The key.
|
||||
*/
|
||||
void *k;
|
||||
int k_is_constant;
|
||||
/**
|
||||
* The value.
|
||||
*/
|
||||
@@ -92,36 +110,6 @@ struct lh_table {
|
||||
*/
|
||||
int count;
|
||||
|
||||
/**
|
||||
* Number of collisions.
|
||||
*/
|
||||
int collisions;
|
||||
|
||||
/**
|
||||
* Number of resizes.
|
||||
*/
|
||||
int resizes;
|
||||
|
||||
/**
|
||||
* Number of lookups.
|
||||
*/
|
||||
int lookups;
|
||||
|
||||
/**
|
||||
* Number of inserts.
|
||||
*/
|
||||
int inserts;
|
||||
|
||||
/**
|
||||
* Number of deletes.
|
||||
*/
|
||||
int deletes;
|
||||
|
||||
/**
|
||||
* Name of the hash table.
|
||||
*/
|
||||
const char *name;
|
||||
|
||||
/**
|
||||
* The first entry.
|
||||
*/
|
||||
@@ -143,16 +131,6 @@ struct lh_table {
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Pre-defined hash and equality functions
|
||||
*/
|
||||
extern unsigned long lh_ptr_hash(const void *k);
|
||||
extern int lh_ptr_equal(const void *k1, const void *k2);
|
||||
|
||||
extern unsigned long lh_char_hash(const void *k);
|
||||
extern int lh_char_equal(const void *k1, const void *k2);
|
||||
|
||||
|
||||
/**
|
||||
* Convenience list iterator.
|
||||
*/
|
||||
@@ -171,7 +149,6 @@ for(entry = table->head; entry && ((tmp = entry->next) || 1); entry = tmp)
|
||||
* Create a new linkhash table.
|
||||
* @param size initial table size. The table is automatically resized
|
||||
* although this incurs a performance penalty.
|
||||
* @param name the table name.
|
||||
* @param free_fn callback function used to free memory for entries
|
||||
* when lh_table_free or lh_table_delete is called.
|
||||
* If NULL is provided, then memory for keys and values
|
||||
@@ -184,7 +161,7 @@ for(entry = table->head; entry && ((tmp = entry->next) || 1); entry = tmp)
|
||||
* and C strings respectively.
|
||||
* @return a pointer onto the linkhash table.
|
||||
*/
|
||||
extern struct lh_table* lh_table_new(int size, const char *name,
|
||||
extern struct lh_table* lh_table_new(int size,
|
||||
lh_entry_free_fn *free_fn,
|
||||
lh_hash_fn *hash_fn,
|
||||
lh_equal_fn *equal_fn);
|
||||
@@ -197,7 +174,7 @@ extern struct lh_table* lh_table_new(int size, const char *name,
|
||||
* @param free_fn callback function used to free memory for entries.
|
||||
* @return a pointer onto the linkhash table.
|
||||
*/
|
||||
extern struct lh_table* lh_kchar_table_new(int size, const char *name,
|
||||
extern struct lh_table* lh_kchar_table_new(int size,
|
||||
lh_entry_free_fn *free_fn);
|
||||
|
||||
|
||||
@@ -209,7 +186,7 @@ extern struct lh_table* lh_kchar_table_new(int size, const char *name,
|
||||
* @param free_fn callback function used to free memory for entries.
|
||||
* @return a pointer onto the linkhash table.
|
||||
*/
|
||||
extern struct lh_table* lh_kptr_table_new(int size, const char *name,
|
||||
extern struct lh_table* lh_kptr_table_new(int size,
|
||||
lh_entry_free_fn *free_fn);
|
||||
|
||||
|
||||
@@ -231,6 +208,21 @@ extern void lh_table_free(struct lh_table *t);
|
||||
extern int lh_table_insert(struct lh_table *t, void *k, const void *v);
|
||||
|
||||
|
||||
/**
|
||||
* Insert a record into the table. This one accepts the key's hash in additon
|
||||
* to the key. This is an exension to support functions that need to calculate
|
||||
* the hash several times and allows them to do it just once and then pass
|
||||
* in the hash to all utility functions. Depending on use case, this can be a
|
||||
* very considerate performance improvement.
|
||||
* @param t the table to insert into.
|
||||
* @param k a pointer to the key to insert.
|
||||
* @param v a pointer to the value to insert.
|
||||
* @param h hash value of the key to insert
|
||||
* @param opts opts, a subset of JSON_OBJECT_ADD_* flags is supported
|
||||
*/
|
||||
extern int lh_table_insert_w_hash(struct lh_table *t, void *k, const void *v, const unsigned long h, const unsigned opts);
|
||||
|
||||
|
||||
/**
|
||||
* Lookup a record into the table.
|
||||
* @param t the table to lookup
|
||||
@@ -239,6 +231,19 @@ extern int lh_table_insert(struct lh_table *t, void *k, const void *v);
|
||||
*/
|
||||
extern struct lh_entry* lh_table_lookup_entry(struct lh_table *t, const void *k);
|
||||
|
||||
/**
|
||||
* Lookup a record into the table. This one accepts the key's hash in additon
|
||||
* to the key. This is an exension to support functions that need to calculate
|
||||
* the hash several times and allows them to do it just once and then pass
|
||||
* in the hash to all utility functions. Depending on use case, this can be a
|
||||
* very considerate performance improvement.
|
||||
* @param t the table to lookup
|
||||
* @param k a pointer to the key to lookup
|
||||
* @param h hash value of the key to lookup
|
||||
* @return a pointer to the record structure of the value or NULL if it does not exist.
|
||||
*/
|
||||
extern struct lh_entry* lh_table_lookup_entry_w_hash(struct lh_table *t, const void *k, const unsigned long h);
|
||||
|
||||
/**
|
||||
* Lookup a record into the table
|
||||
* @param t the table to lookup
|
||||
@@ -285,6 +290,22 @@ extern int lh_table_length(struct lh_table *t);
|
||||
void lh_abort(const char *msg, ...);
|
||||
void lh_table_resize(struct lh_table *t, int new_size);
|
||||
|
||||
|
||||
/**
|
||||
* Calculate the hash of a key for a given table.
|
||||
* This is an exension to support functions that need to calculate
|
||||
* the hash several times and allows them to do it just once and then pass
|
||||
* in the hash to all utility functions. Depending on use case, this can be a
|
||||
* very considerate performance improvement.
|
||||
* @param t the table (used to obtain hash function)
|
||||
* @param k a pointer to the key to lookup
|
||||
* @return the key's hash
|
||||
*/
|
||||
static inline unsigned long lh_get_hash(const struct lh_table *t, const void *k)
|
||||
{
|
||||
return t->hash_fn(k);
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user