[Git][ghc/ghc][wip/tvar-table] rts/STM: Fall back to linear search for small transactions
Ben Gamari (@bgamari)
gitlab at gitlab.haskell.org
Wed Feb 7 20:26:27 UTC 2024
Ben Gamari pushed to branch wip/tvar-table at Glasgow Haskell Compiler / GHC
Commits:
f0af9df8 by Ben Gamari at 2024-02-07T15:26:21-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,6 +642,7 @@ static void add_tvar (Capability *cap,
new_entry -> tvar = tvar;
new_entry -> expected_value = expected_value;
new_entry -> new_value = new_value;
+ trec->n_entries++;
insertHashTable(trec->tvar_table, (StgWord) tvar, new_entry);
}
@@ -1095,15 +1100,26 @@ static TRecEntry *get_entry_for(StgTRecHeader *trec, StgTVar *tvar, StgTRecHeade
ASSERT(trec != NO_TREC);
do {
- 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;
+ 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) {
+ *in = trec;
+ return e;
+ }
+ });
+ } 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;
+ }
+ trec = trec -> enclosing_trec;
}
- trec = trec -> enclosing_trec;
} while (trec != NO_TREC);
return NULL;
=====================================
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/f0af9df8796e593a5a70bf3436c7eab699767462
--
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/f0af9df8796e593a5a70bf3436c7eab699767462
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/20240207/36de2dd1/attachment-0001.html>
More information about the ghc-commits
mailing list