[glib: 1/2] grefcount: Optimise g_atomic_ref_count_inc




commit 827b4ec85523ca488d48e358ec211718ffd40a65
Author: Nishal Kulkarni <kulknishu gmail com>
Date:   Mon May 10 15:45:00 2021 +0530

    grefcount: Optimise g_atomic_ref_count_inc
    
    Currently, `g_atomic_ref_count_inc ()` does three operations.
    We try to get this down to one operation.
    
    Fixed by replacing `g_atomic_int_inc ()` with `g_atomic_int_add ()`
    which increments by 1 and returns old value
    
    Note: changes current behaviour, checks are now done after increment.
    
    Closes: #1583

 glib/grefcount.c | 14 ++++++--------
 1 file changed, 6 insertions(+), 8 deletions(-)
---
diff --git a/glib/grefcount.c b/glib/grefcount.c
index 72f76634c..c0ee2985d 100644
--- a/glib/grefcount.c
+++ b/glib/grefcount.c
@@ -235,16 +235,14 @@ void
 void
 (g_atomic_ref_count_inc) (gatomicrefcount *arc)
 {
-  g_return_if_fail (arc != NULL);
-  g_return_if_fail (g_atomic_int_get (arc) > 0);
+  gint old_value;
 
-  if (g_atomic_int_get (arc) == G_MAXINT)
-    {
-      g_critical ("Reference count has reached saturation");
-      return;
-    }
+  g_return_if_fail (arc != NULL);
+  old_value = g_atomic_int_add (arc, 1);
+  g_return_if_fail (old_value > 0);
 
-  g_atomic_int_inc (arc);
+  if (old_value == G_MAXINT)
+    g_critical ("Reference count has reached saturation");
 }
 
 /**


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