]> arthur.barton.de Git - netdata.git/blobdiff - src/dictionary.c
40% faster AVL searching and DICTIONARY operations
[netdata.git] / src / dictionary.c
index 1f9a26aeacf18bec52f42dee2c9c4bb18d484e5c..1543f4d0e7334bce5ad63e2b5469e5b47b0dffb8 100644 (file)
 // ----------------------------------------------------------------------------
 // dictionary locks
 
-static inline void dictionary_atomic_updates_lock(DICTIONARY *dict) {
-       if(likely(!(dict->flags & DICTIONARY_FLAG_SINGLE_THREADED))) {
-               // debug(D_DICTIONARY, "Dictionary ATOMIC lock");
-               pthread_mutex_lock(&dict->atomic_mutex);
-       }
-}
-
-static inline void dictionary_atomic_updates_unlock(DICTIONARY *dict) {
-       if(likely(!(dict->flags & DICTIONARY_FLAG_SINGLE_THREADED))) {
-               // debug(D_DICTIONARY, "Dictionary ATOMIC unlock");
-               pthread_mutex_unlock(&dict->atomic_mutex);
-       }
-}
-
 static inline void dictionary_read_lock(DICTIONARY *dict) {
        if(likely(!(dict->flags & DICTIONARY_FLAG_SINGLE_THREADED))) {
                // debug(D_DICTIONARY, "Dictionary READ lock");
@@ -54,8 +40,6 @@ static inline void dictionary_unlock(DICTIONARY *dict) {
 // ----------------------------------------------------------------------------
 // avl index
 
-static int name_value_iterator(avl *a) { if(a) {}; return 0; }
-
 static int name_value_compare(void* a, void* b) {
        if(((NAME_VALUE *)a)->hash < ((NAME_VALUE *)b)->hash) return -1;
        else if(((NAME_VALUE *)a)->hash > ((NAME_VALUE *)b)->hash) return 1;
@@ -66,14 +50,12 @@ static int name_value_compare(void* a, void* b) {
 #define dictionary_name_value_index_del_nolock(dict, nv) do { (dict)->deletes++; avl_remove(&(dict->values_index), (avl *)(nv)); } while(0)
 
 static inline NAME_VALUE *dictionary_name_value_index_find_nolock(DICTIONARY *dict, const char *name, uint32_t hash) {
-       NAME_VALUE *result = NULL, tmp;
+       NAME_VALUE tmp;
        tmp.hash = (hash)?hash:simple_hash(name);
        tmp.name = (char *)name;
 
        dict->searches++;
-       avl_search(&(dict->values_index), (avl *)&tmp, name_value_iterator, (avl **)&result);
-
-       return result;
+       return (NAME_VALUE *)avl_search(&(dict->values_index), (avl *) &tmp);
 }
 
 // ----------------------------------------------------------------------------
@@ -143,7 +125,6 @@ DICTIONARY *dictionary_create(uint32_t flags) {
 
        avl_init(&dict->values_index, name_value_compare);
        pthread_rwlock_init(&dict->rwlock, NULL);
-       pthread_mutex_init(&dict->atomic_mutex, NULL);
 
        dict->flags = flags;
 
@@ -153,8 +134,6 @@ DICTIONARY *dictionary_create(uint32_t flags) {
 void dictionary_destroy(DICTIONARY *dict) {
        debug(D_DICTIONARY, "Destroying dictionary.");
 
-       dictionary_atomic_updates_lock(dict);
-
        dictionary_write_lock(dict);
 
        while(dict->values_index.root)
@@ -162,8 +141,6 @@ void dictionary_destroy(DICTIONARY *dict) {
 
        dictionary_unlock(dict);
 
-       dictionary_atomic_updates_unlock(dict);
-
        free(dict);
 }
 
@@ -174,19 +151,13 @@ void *dictionary_set(DICTIONARY *dict, const char *name, void *value, size_t val
 
        uint32_t hash = simple_hash(name);
 
-       dictionary_atomic_updates_lock(dict);
+       dictionary_write_lock(dict);
 
-       dictionary_read_lock(dict);
        NAME_VALUE *nv = dictionary_name_value_index_find_nolock(dict, name, hash);
-       dictionary_unlock(dict);
-
        if(unlikely(!nv)) {
                debug(D_DICTIONARY, "Dictionary entry with name '%s' not found. Creating a new one.", name);
 
-               dictionary_write_lock(dict);
                nv = dictionary_name_value_create_nolock(dict, name, value, value_len, hash);
-               dictionary_unlock(dict);
-
                if(unlikely(!nv))
                        fatal("Cannot create name_value.");
        }
@@ -214,7 +185,7 @@ void *dictionary_set(DICTIONARY *dict, const char *name, void *value, size_t val
                }
        }
 
-       dictionary_atomic_updates_unlock(dict);
+       dictionary_unlock(dict);
 
        return nv->value;
 }
@@ -240,25 +211,20 @@ int dictionary_del(DICTIONARY *dict, const char *name) {
 
        debug(D_DICTIONARY, "DEL dictionary entry with name '%s'.", name);
 
-       dictionary_atomic_updates_lock(dict);
+       dictionary_write_lock(dict);
 
-       dictionary_read_lock(dict);
        NAME_VALUE *nv = dictionary_name_value_index_find_nolock(dict, name, 0);
-       dictionary_unlock(dict);
-
        if(unlikely(!nv)) {
                debug(D_DICTIONARY, "Not found dictionary entry with name '%s'.", name);
                ret = -1;
        }
        else {
                debug(D_DICTIONARY, "Found dictionary entry with name '%s'.", name);
-               dictionary_write_lock(dict);
                dictionary_name_value_destroy_nolock(dict, nv);
-               dictionary_unlock(dict);
                ret = 0;
        }
 
-       dictionary_atomic_updates_unlock(dict);
+       dictionary_unlock(dict);
 
        return ret;
 }