[glib: 1/2] grefcount: Optimise g_atomic_ref_count_dec




commit 8cba1f4c17a3301dc2674ed750b9fce6f2c2dfb1
Author: Nishal Kulkarni <kulknishu gmail com>
Date:   Tue May 11 15:08:20 2021 +0530

    grefcount: Optimise g_atomic_ref_count_dec
    
    Currently, `g_atomic_ref_count_dec ()` does two operations.
    We try to get this down to one operation.
    
    Replace `g_atomic_int_dec_and_test ()` with `g_atomic_int_add ()`
    Passing -1 as value.
    
    Note: changes current behaviour, checks are now done after decrement.

 glib/grefcount.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
---
diff --git a/glib/grefcount.c b/glib/grefcount.c
index c0ee2985d..6826a0005 100644
--- a/glib/grefcount.c
+++ b/glib/grefcount.c
@@ -262,10 +262,13 @@ void
 gboolean
 (g_atomic_ref_count_dec) (gatomicrefcount *arc)
 {
+  gint old_value;
+
   g_return_val_if_fail (arc != NULL, FALSE);
-  g_return_val_if_fail (g_atomic_int_get (arc) > 0, FALSE);
+  old_value = g_atomic_int_add (arc, -1);
+  g_return_val_if_fail (old_value > 0, FALSE);
 
-  return g_atomic_int_dec_and_test (arc);
+  return old_value == 1;
 }
 
 /**


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