eet - dictionary - use rwlocks instead of spinlocks - better contention
authorCarsten Haitzler (Rasterman) <raster@rasterman.com>
Tue, 20 Nov 2018 16:55:45 +0000 (16:55 +0000)
committerHermet Park <hermetpark@gmail.com>
Wed, 5 Dec 2018 05:21:28 +0000 (14:21 +0900)
if the dict is contended on by lots of threads then a spinlock really
makes it slow. a rwlock gets about 1.5-2x speedup depending on
arch/thread count.

@optimize

src/lib/eet/Eet_private.h
src/lib/eet/eet_dictionary.c

index ca55227..43b4c14 100644 (file)
@@ -40,8 +40,8 @@ struct _Eet_Dictionary
    unsigned char *all_hash;
    unsigned char *all_allocated;
 
-   Eina_Hash  *converts;
-   Eina_Spinlock   mutex;
+   Eina_Hash     *converts;
+   Eina_RWLock    rwlock;
 
    int         size;
    int         offset;
index 779eb21..c8ea016 100644 (file)
@@ -21,7 +21,7 @@ eet_dictionary_add(void)
      return NULL;
 
    memset(new->hash, -1, sizeof (int) * 256);
-   eina_spinlock_new(&new->mutex);
+   eina_rwlock_new(&new->rwlock);
 
    return new;
 }
@@ -33,7 +33,7 @@ eet_dictionary_free(Eet_Dictionary *ed)
 
    if (!ed) return;
 
-   eina_spinlock_free(&ed->mutex);
+   eina_rwlock_free(&ed->rwlock);
 
    for (i = 0; i < ed->count; ++i)
      if (ed->all_allocated[i >> 3] & (1 << (i & 0x7)))
@@ -98,7 +98,7 @@ eet_dictionary_string_add(Eet_Dictionary *ed,
    hash = _eet_hash_gen(string, 8);
    len = strlen(string) + 1;
 
-   eina_spinlock_take(&ed->mutex);
+   eina_rwlock_take_read(&ed->rwlock);
 
    idx = _eet_dictionary_lookup(ed, string, len, hash, &pidx);
 
@@ -108,11 +108,16 @@ eet_dictionary_string_add(Eet_Dictionary *ed,
             ((ed->all[idx].str == string) ||
                 (!strcmp(ed->all[idx].str, string))))
          {
-           eina_spinlock_release(&ed->mutex);
+             eina_rwlock_release(&ed->rwlock);
            return idx;
          }
      }
 
+   str = eina_stringshare_add(string);
+   if (!str) goto on_error;
+
+   eina_rwlock_release(&ed->rwlock);
+   eina_rwlock_take_write(&ed->rwlock);
    if (ed->total == ed->count)
      {
         Eet_String *new;
@@ -120,7 +125,7 @@ eet_dictionary_string_add(Eet_Dictionary *ed,
        unsigned char *new_allocated;
         int total;
 
-        total = ed->total + 8;
+        total = ed->total + 64;
 
         new = realloc(ed->all, total * sizeof(Eet_String));
         if (!new) goto on_error;
@@ -137,9 +142,6 @@ eet_dictionary_string_add(Eet_Dictionary *ed,
         ed->total = total;
      }
 
-   str = eina_stringshare_add(string);
-   if (!str) goto on_error;
-
    current = ed->all + ed->count;
 
    ed->all_allocated[ed->count >> 3] |= (1 << (ed->count & 0x7));
@@ -164,11 +166,11 @@ eet_dictionary_string_add(Eet_Dictionary *ed,
      }
 
    cnt = ed->count++;
-   eina_spinlock_release(&ed->mutex);
+   eina_rwlock_release(&ed->rwlock);
    return cnt;
 
  on_error:
-   eina_spinlock_release(&ed->mutex);
+   eina_rwlock_release(&ed->rwlock);
    return -1;
 }
 
@@ -182,12 +184,12 @@ eet_dictionary_string_get_size(const Eet_Dictionary *ed,
 
    if (idx < 0) goto done;
 
-   eina_spinlock_take((Eina_Spinlock*) &ed->mutex);
+   eina_rwlock_take_read((Eina_RWLock *)&ed->rwlock);
 
    if (idx < ed->count)
      length = ed->all[idx].len;
 
-   eina_spinlock_release((Eina_Spinlock*) &ed->mutex);
+   eina_rwlock_release((Eina_RWLock *)&ed->rwlock);
 
  done:
    return length;
@@ -209,12 +211,12 @@ eet_dictionary_string_get_hash(const Eet_Dictionary *ed,
 
    if (idx < 0) goto done;
 
-   eina_spinlock_take((Eina_Spinlock*) &ed->mutex);
+   eina_rwlock_take_read((Eina_RWLock *)&ed->rwlock);
 
    if (idx < ed->count)
      hash = ed->all_hash[idx];
 
-   eina_spinlock_release((Eina_Spinlock*) &ed->mutex);
+   eina_rwlock_release((Eina_RWLock *)&ed->rwlock);
 
  done:
    return hash;
@@ -230,7 +232,7 @@ eet_dictionary_string_get_char(const Eet_Dictionary *ed,
 
    if (idx < 0) goto done;
 
-   eina_spinlock_take((Eina_Spinlock*) &ed->mutex);
+   eina_rwlock_take_read((Eina_RWLock *)&ed->rwlock);
 
    if (idx < ed->count)
      {
@@ -245,7 +247,7 @@ eet_dictionary_string_get_char(const Eet_Dictionary *ed,
         s = ed->all[idx].str;
      }
 
-   eina_spinlock_release((Eina_Spinlock*) &ed->mutex);
+   eina_rwlock_release((Eina_RWLock *)&ed->rwlock);
 
  done:
    return s;
@@ -323,14 +325,14 @@ _eet_dictionary_test(const Eet_Dictionary *ed,
 
    if (idx < 0) goto done;
 
-   eina_spinlock_take((Eina_Spinlock*) &ed->mutex);
+   eina_rwlock_take_read((Eina_RWLock *)&ed->rwlock);
 
    if (!(idx < ed->count)) goto unlock_done;
 
    limit = EINA_TRUE;
 
  unlock_done:
-   eina_spinlock_release((Eina_Spinlock*) &ed->mutex);
+   eina_rwlock_release((Eina_RWLock *)&ed->rwlock);
 
  done:
    return limit;
@@ -343,7 +345,7 @@ eet_dictionary_convert_get(const Eet_Dictionary *ed,
 {
    Eet_Convert *result;
 
-   eina_spinlock_take((Eina_Spinlock*) &ed->mutex);
+   eina_rwlock_take_read((Eina_RWLock *)&ed->rwlock);
 
    *str = ed->all[idx].str;
 
@@ -363,7 +365,7 @@ eet_dictionary_convert_get(const Eet_Dictionary *ed,
    eina_hash_add(ed->converts, &idx, result);
 
  done:
-   eina_spinlock_release((Eina_Spinlock*) &ed->mutex);
+   eina_rwlock_release((Eina_RWLock *)&ed->rwlock);
 
    return result;
 }
@@ -384,7 +386,7 @@ eet_dictionary_string_get_float(const Eet_Dictionary *ed,
 
    if (!(convert->type & EET_D_FLOAT))
      {
-        eina_spinlock_take((Eina_Spinlock*) &ed->mutex);
+        eina_rwlock_take_read((Eina_RWLock *)&ed->rwlock);
         if (!_eet_dictionary_string_get_float_cache(str, ed->all[idx].len,
                                                     &convert->f))
           {
@@ -394,13 +396,13 @@ eet_dictionary_string_get_float(const Eet_Dictionary *ed,
              if (eina_convert_atod(str, ed->all[idx].len, &mantisse,
                                    &exponent) == EINA_FALSE)
                {
-                  eina_spinlock_release((Eina_Spinlock*) &ed->mutex);
+                  eina_rwlock_release((Eina_RWLock *)&ed->rwlock);
                   return EINA_FALSE;
                }
 
              convert->f = ldexpf((float)mantisse, exponent);
           }
-        eina_spinlock_release((Eina_Spinlock*) &ed->mutex);
+        eina_rwlock_release((Eina_RWLock *)&ed->rwlock);
 
         convert->type |= EET_D_FLOAT;
      }
@@ -425,7 +427,7 @@ eet_dictionary_string_get_double(const Eet_Dictionary *ed,
 
    if (!(convert->type & EET_D_DOUBLE))
      {
-        eina_spinlock_take((Eina_Spinlock*) &ed->mutex);
+        eina_rwlock_take_read((Eina_RWLock *)&ed->rwlock);
 
         if (!_eet_dictionary_string_get_double_cache(str, ed->all[idx].len,
                                                      &convert->d))
@@ -436,13 +438,13 @@ eet_dictionary_string_get_double(const Eet_Dictionary *ed,
              if (eina_convert_atod(str, ed->all[idx].len, &mantisse,
                                    &exponent) == EINA_FALSE)
                {
-                  eina_spinlock_release((Eina_Spinlock*) &ed->mutex);
+                  eina_rwlock_release((Eina_RWLock *)&ed->rwlock);
                   return EINA_FALSE;
                }
 
              convert->d = ldexp((double)mantisse, exponent);
           }
-        eina_spinlock_release((Eina_Spinlock*) &ed->mutex);
+        eina_rwlock_release((Eina_RWLock *)&ed->rwlock);
 
         convert->type |= EET_D_DOUBLE;
      }
@@ -469,13 +471,13 @@ eet_dictionary_string_get_fp(const Eet_Dictionary *ed,
      {
         Eina_F32p32 fp;
 
-        eina_spinlock_take((Eina_Spinlock*) &ed->mutex);
+        eina_rwlock_take_read((Eina_RWLock *)&ed->rwlock);
         if (!eina_convert_atofp(str, ed->all[idx].len, &fp))
           {
-             eina_spinlock_release((Eina_Spinlock*) &ed->mutex);
+             eina_rwlock_release((Eina_RWLock *)&ed->rwlock);
              return EINA_FALSE;
           }
-        eina_spinlock_release((Eina_Spinlock*) &ed->mutex);
+        eina_rwlock_release((Eina_RWLock *)&ed->rwlock);
 
         convert->fp = fp;
         convert->type |= EET_D_FIXED_POINT;
@@ -495,7 +497,7 @@ eet_dictionary_string_check(Eet_Dictionary *ed,
    if ((!ed) || (!string))
      return 0;
 
-   eina_spinlock_take(&ed->mutex);
+   eina_rwlock_take_read((Eina_RWLock *)&ed->rwlock);
 
    if ((ed->start <= string) && (string < ed->end))
      res = 1;
@@ -510,7 +512,7 @@ eet_dictionary_string_check(Eet_Dictionary *ed,
             }
      }
 
-   eina_spinlock_release(&ed->mutex);
+   eina_rwlock_release((Eina_RWLock *)&ed->rwlock);
 
    return res;
 }