From: Rainer Gerhards Date: Wed, 23 Sep 2015 10:40:57 +0000 (+0200) Subject: remove unneeded data items from hashtable code X-Git-Tag: json-c-0.13-20171207~184^2~1 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=1ae4b50bdedfde89f179c7538ecf886d175a59b0;p=json-c remove unneeded data items from hashtable code These items were used for statistics tracking, but no code at all exists to consume them. By removing them we save a) space because they counters required space, and did so in each and every json object b) performance because calloc() needs to write less data and the counters are no longer maintained; cache performance can be better, load on OS main memory is lighter We could conditionally enable/disable these counters, but I have not done this they were really nowhere used and it looked more like a left-over from the import of hashtable code. --- diff --git a/json_object.c b/json_object.c index 55f7469..f950257 100644 --- a/json_object.c +++ b/json_object.c @@ -69,7 +69,7 @@ static struct lh_table *json_object_table; static void json_object_init(void) __attribute__ ((constructor)); static void json_object_init(void) { MC_DEBUG("json_object_init: creating object table\n"); - json_object_table = lh_kptr_table_new(128, "json_object_table", NULL); + json_object_table = lh_kptr_table_new(128, NULL); } static void json_object_fini(void) __attribute__ ((destructor)); @@ -374,7 +374,7 @@ struct json_object* json_object_new_object(void) jso->_delete = &json_object_object_delete; jso->_to_json_string = &json_object_object_to_json_string; jso->o.c_object = lh_kchar_table_new(JSON_OBJECT_DEF_HASH_ENTRIES, - NULL, &json_object_lh_entry_free); + &json_object_lh_entry_free); if (!jso->o.c_object) { json_object_generic_delete(jso); diff --git a/linkhash.c b/linkhash.c index 5a9a239..e3dc442 100644 --- a/linkhash.c +++ b/linkhash.c @@ -458,7 +458,7 @@ int lh_char_equal(const void *k1, const void *k2) return (strcmp((const char*)k1, (const char*)k2) == 0); } -struct lh_table* lh_table_new(int size, const char *name, +struct lh_table* lh_table_new(int size, lh_entry_free_fn *free_fn, lh_hash_fn *hash_fn, lh_equal_fn *equal_fn) @@ -470,7 +470,6 @@ struct lh_table* lh_table_new(int size, const char *name, if(!t) lh_abort("lh_table_new: calloc failed\n"); t->count = 0; t->size = size; - t->name = name; t->table = (struct lh_entry*)calloc(size, sizeof(struct lh_entry)); if(!t->table) lh_abort("lh_table_new: calloc failed\n"); t->free_fn = free_fn; @@ -480,16 +479,16 @@ struct lh_table* lh_table_new(int size, const char *name, return t; } -struct lh_table* lh_kchar_table_new(int size, const char *name, +struct lh_table* lh_kchar_table_new(int size, lh_entry_free_fn *free_fn) { - return lh_table_new(size, name, free_fn, char_hash_fn, lh_char_equal); + return lh_table_new(size, free_fn, char_hash_fn, lh_char_equal); } -struct lh_table* lh_kptr_table_new(int size, const char *name, +struct lh_table* lh_kptr_table_new(int size, lh_entry_free_fn *free_fn) { - return lh_table_new(size, name, free_fn, lh_ptr_hash, lh_ptr_equal); + return lh_table_new(size, free_fn, lh_ptr_hash, lh_ptr_equal); } void lh_table_resize(struct lh_table *t, int new_size) @@ -497,7 +496,7 @@ void lh_table_resize(struct lh_table *t, int new_size) struct lh_table *new_t; struct lh_entry *ent; - new_t = lh_table_new(new_size, t->name, NULL, t->hash_fn, t->equal_fn); + new_t = lh_table_new(new_size, NULL, t->hash_fn, t->equal_fn); ent = t->head; while(ent) { lh_table_insert(new_t, ent->k, ent->v); @@ -508,7 +507,6 @@ void lh_table_resize(struct lh_table *t, int new_size) t->size = new_size; t->head = new_t->head; t->tail = new_t->tail; - t->resizes++; free(new_t); } @@ -529,14 +527,12 @@ int lh_table_insert_w_hash(struct lh_table *t, void *k, const void *v, const uns { unsigned long n; - t->inserts++; if(t->count >= t->size * LH_LOAD_FACTOR) lh_table_resize(t, t->size * 2); n = h % t->size; while( 1 ) { if(t->table[n].k == LH_EMPTY || t->table[n].k == LH_FREED) break; - t->collisions++; if ((int)++n == t->size) n = 0; } @@ -568,7 +564,6 @@ struct lh_entry* lh_table_lookup_entry_w_hash(struct lh_table *t, const void *k, unsigned long n = h % t->size; int count = 0; - t->lookups++; while( count < t->size ) { if(t->table[n].k == LH_EMPTY) return NULL; if(t->table[n].k != LH_FREED && diff --git a/linkhash.h b/linkhash.h index ba7e627..f0d7ce2 100644 --- a/linkhash.h +++ b/linkhash.h @@ -110,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. */ @@ -179,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 @@ -192,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); @@ -205,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); @@ -217,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);