[gegl] buffer: avoid unnecesarily locking global cache mutex when inserting tile



commit eac67d7b1591ec9e16e58337318969ae2b01ca2e
Author: Ell <ell_se yahoo com>
Date:   Sun Dec 2 09:13:44 2018 -0500

    buffer: avoid unnecesarily locking global cache mutex when inserting tile
    
    In gegl_tile_handler_cache_insert(), only call
    gegl_tile_handler_cache_trim() if the cache actually needs to be
    trimmed, i.e., if the total cache size exceeds the maximum.  The
    latter function always acquires the global cache mutex, which is
    wasteful if there is nothing to actually trim.

 gegl/buffer/gegl-tile-handler-cache.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
---
diff --git a/gegl/buffer/gegl-tile-handler-cache.c b/gegl/buffer/gegl-tile-handler-cache.c
index f25c222d7..ae7c4e962 100644
--- a/gegl/buffer/gegl-tile-handler-cache.c
+++ b/gegl/buffer/gegl-tile-handler-cache.c
@@ -815,6 +815,7 @@ gegl_tile_handler_cache_insert (GeglTileHandlerCache *cache,
                                 gint                  z)
 {
   CacheItem *item = g_slice_new (CacheItem);
+  guintptr   total;
 
   item->tile      = gegl_tile_ref (tile);
   item->link.data = item;
@@ -837,12 +838,15 @@ gegl_tile_handler_cache_insert (GeglTileHandlerCache *cache,
   cache->time = ++cache_time;
 
   if (g_atomic_int_add (gegl_tile_n_cached_clones (tile), 1) == 0)
-    g_atomic_pointer_add (&cache_total, tile->size);
+    total = g_atomic_pointer_add (&cache_total, tile->size) + tile->size;
+  else
+    total = (guintptr) g_atomic_pointer_get (&cache_total);
   g_atomic_pointer_add (&cache_total_uncloned, tile->size);
   g_hash_table_insert (cache->items, item, item);
   g_queue_push_head_link (&cache->queue, &item->link);
 
-  gegl_tile_handler_cache_trim (cache);
+  if (total > gegl_buffer_config ()->tile_cache_size)
+    gegl_tile_handler_cache_trim (cache);
 
   /* there's a race between this assignment, and the one at the bottom of
    * gegl_tile_handler_cache_tile_uncloned().  this is acceptable, though,


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]