[Git][ghc/ghc][wip/tvar-table] rts/STM: Fall back to linear search for small transactions

Ben Gamari (@bgamari) gitlab at gitlab.haskell.org
Thu Feb 8 13:41:41 UTC 2024



Ben Gamari pushed to branch wip/tvar-table at Glasgow Haskell Compiler / GHC


Commits:
54f88701 by Ben Gamari at 2024-02-08T08:41:35-05:00
rts/STM: Fall back to linear search for small transactions

- - - - -


3 changed files:

- rts/STM.c
- rts/StgMiscClosures.cmm
- rts/include/rts/storage/Closures.h


Changes:

=====================================
rts/STM.c
=====================================
@@ -108,6 +108,8 @@
 #define NACQ_ASSERT(_X) ASSERT(_X)
 #endif
 
+#define SMALL_TRANSACTION_THRESHOLD 100
+
 static StgWord stm_gc_epoch = 0;
 
 static void free_tvar_table(StgTRecHeader *trec);
@@ -425,6 +427,7 @@ static StgTRecHeader *new_stg_trec_header(Capability *cap,
 
   result -> enclosing_trec = enclosing_trec;
   result -> current_chunk = new_stg_trec_chunk(cap);
+  result -> n_entries = 0;
   result -> tvar_table = NULL;
   result -> tvar_table_epoch = -1;
 
@@ -496,6 +499,7 @@ static StgTRecHeader *alloc_stg_trec_header(Capability *cap,
     cap -> free_trec_headers = result -> enclosing_trec;
     result -> enclosing_trec = enclosing_trec;
     result -> current_chunk -> next_entry_idx = 0;
+    result -> n_entries = 0;
     result -> tvar_table = NULL;
     result -> tvar_table_epoch = -1;;
     if (enclosing_trec == NO_TREC) {
@@ -638,7 +642,10 @@ static void add_tvar (Capability *cap,
   new_entry -> tvar = tvar;
   new_entry -> expected_value = expected_value;
   new_entry -> new_value = new_value;
-  insertHashTable(trec->tvar_table, (StgWord) tvar, new_entry);
+  trec->n_entries++;
+  if (trec->tvar_table) {
+    insertHashTable(trec->tvar_table, (StgWord) tvar, new_entry);
+  }
 }
 
 /*......................................................................*/
@@ -1090,18 +1097,39 @@ static void rebuild_tvar_table(StgTRecHeader *trec) {
   });
 }
 
-static TRecEntry *get_entry_for(StgTRecHeader *trec, StgTVar *tvar, StgTRecHeader **in) {
-  TRACE("%p : get_entry_for TVar %p", trec, tvar);
-  ASSERT(trec != NO_TREC);
-
-  do {
+static TRecEntry *lookup_trec_entry(StgTRecHeader *trec, StgTVar *tvar) {
+  if (trec->n_entries < SMALL_TRANSACTION_THRESHOLD) {
+    // In the case of small transactions we don't bother building 
+    // a TVar table and instead resort to linear search.
+    FOR_EACH_ENTRY(trec, e, {
+      if (e->tvar == tvar) {
+        return e;
+      }
+    });
+    return NULL;
+  } else {
     if (trec->tvar_table_epoch != stm_gc_epoch) {
       rebuild_tvar_table(trec);
     }
+
     TRecEntry *result = lookupHashTable(trec->tvar_table, (StgWord) tvar);
     if (result) {
-      *in = trec;
       return result;
+    } else {
+      return NULL;
+    }
+  }
+}
+
+static TRecEntry *get_entry_for(StgTRecHeader *trec, StgTVar *tvar, StgTRecHeader **in) {
+  TRACE("%p : get_entry_for TVar %p", trec, tvar);
+  ASSERT(trec != NO_TREC);
+
+  do {
+    TRecEntry *e = lookup_trec_entry(trec, tvar);
+    if (e) {
+      *in = trec;
+      return e;
     }
     trec = trec -> enclosing_trec;
   } while (trec != NO_TREC);


=====================================
rts/StgMiscClosures.cmm
=====================================
@@ -773,7 +773,7 @@ INFO_TABLE(stg_TVAR_WATCH_QUEUE, 3, 0, MUT_PRIM, "TVAR_WATCH_QUEUE", "TVAR_WATCH
 INFO_TABLE(stg_TREC_CHUNK, 0, 0, TREC_CHUNK, "TREC_CHUNK", "TREC_CHUNK")
 { foreign "C" barf("TREC_CHUNK object (%p) entered!", R1) never returns; }
 
-INFO_TABLE(stg_TREC_HEADER, 2, 3, MUT_PRIM, "TREC_HEADER", "TREC_HEADER")
+INFO_TABLE(stg_TREC_HEADER, 2, 4, MUT_PRIM, "TREC_HEADER", "TREC_HEADER")
 { foreign "C" barf("TREC_HEADER object (%p) entered!", R1) never returns; }
 
 INFO_TABLE_CONSTR(stg_END_STM_WATCH_QUEUE,0,0,0,CONSTR_NOCAF,"END_STM_WATCH_QUEUE","END_STM_WATCH_QUEUE")


=====================================
rts/include/rts/storage/Closures.h
=====================================
@@ -550,6 +550,7 @@ struct StgTRecHeader_ {
   struct StgTRecHeader_     *enclosing_trec;
   StgTRecChunk              *current_chunk MUT_FIELD;
   TRecState                  state;
+  StgWord                    n_entries;
   struct hashtable          *tvar_table;
   StgWord                    tvar_table_epoch;
 };



View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/54f8870130cf012690a75babcea53d4b69f6edd7

-- 
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/54f8870130cf012690a75babcea53d4b69f6edd7
You're receiving this email because of your account on gitlab.haskell.org.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/ghc-commits/attachments/20240208/dd20d014/attachment-0001.html>


More information about the ghc-commits mailing list