Przeglądaj źródła

base: BPending: small optimization by removing the "pending" flag

ambrop7 13 lat temu
rodzic
commit
a27159ec27
2 zmienionych plików z 23 dodań i 5 usunięć
  1. 20 4
      base/BPending.c
  2. 3 1
      base/BPending.h

+ 20 - 4
base/BPending.c

@@ -70,13 +70,17 @@ void BPendingGroup_ExecuteJob (BPendingGroup *g)
     
     // get a job
     BPending *p = BPending__List_First(&g->jobs);
+    ASSERT(p->pending_node.next != p)
     ASSERT(p->pending)
     
     // remove from jobs list
     BPending__List_Remove(&g->jobs, p);
     
     // set not pending
+    p->pending_node.next = p;
+#ifndef NDEBUG
     p->pending = 0;
+#endif
     
     // execute job
     p->handler(p->user);
@@ -98,7 +102,10 @@ void BPending_Init (BPending *o, BPendingGroup *g, BPending_handler handler, voi
     o->user = user;
     
     // set not pending
+    o->pending_node.next = o;
+#ifndef NDEBUG
     o->pending = 0;
+#endif
     
     // increment pending counter
     DebugCounter_Increment(&o->g->pending_ctr);
@@ -111,9 +118,10 @@ void BPending_Free (BPending *o)
 {
     DebugCounter_Decrement(&o->g->pending_ctr);
     DebugObject_Free(&o->d_obj);
+    ASSERT(o->pending == (o->pending_node.next != o))
     
     // remove from jobs list
-    if (o->pending) {
+    if (o->pending_node.next != o) {
         BPending__List_Remove(&o->g->jobs, o);
     }
 }
@@ -121,9 +129,10 @@ void BPending_Free (BPending *o)
 void BPending_Set (BPending *o)
 {
     DebugObject_Access(&o->d_obj);
+    ASSERT(o->pending == (o->pending_node.next != o))
     
     // remove from jobs list
-    if (o->pending) {
+    if (o->pending_node.next != o) {
         BPending__List_Remove(&o->g->jobs, o);
     }
     
@@ -131,25 +140,32 @@ void BPending_Set (BPending *o)
     BPending__List_Prepend(&o->g->jobs, o);
     
     // set pending
+#ifndef NDEBUG
     o->pending = 1;
+#endif
 }
 
 void BPending_Unset (BPending *o)
 {
     DebugObject_Access(&o->d_obj);
+    ASSERT(o->pending == (o->pending_node.next != o))
     
-    if (o->pending) {
+    if (o->pending_node.next != o) {
         // remove from jobs list
         BPending__List_Remove(&o->g->jobs, o);
         
         // set not pending
+        o->pending_node.next = o;
+#ifndef NDEBUG
         o->pending = 0;
+#endif
     }
 }
 
 int BPending_IsSet (BPending *o)
 {
     DebugObject_Access(&o->d_obj);
+    ASSERT(o->pending == (o->pending_node.next != o))
     
-    return o->pending;
+    return (o->pending_node.next != o);
 }

+ 3 - 1
base/BPending.h

@@ -72,8 +72,10 @@ typedef struct BPending_s {
     BPendingGroup *g;
     BPending_handler handler;
     void *user;
+    BPending__ListNode pending_node; // optimization: if not pending, .next is this
+#ifndef NDEBUG
     uint8_t pending;
-    BPending__ListNode pending_node;
+#endif
     DebugObject d_obj;
 } BPending;