aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorgingerBill <gingerBill@users.noreply.github.com>2026-02-02 11:03:22 +0000
committergingerBill <gingerBill@users.noreply.github.com>2026-02-02 11:03:50 +0000
commitc7f40b8b8fb129acf369cca7dd472626e983d1b4 (patch)
tree158833e49871e6ed797d67988d17c140d116b512
parentacabae8644dc5fb9cfb1c7dbb2f45625529ab760 (diff)
Use mutex striping for `add_type_and_value`
-rw-r--r--src/checker.cpp37
-rw-r--r--src/checker.hpp2
2 files changed, 29 insertions, 10 deletions
diff --git a/src/checker.cpp b/src/checker.cpp
index 453f3e241..34fa91384 100644
--- a/src/checker.cpp
+++ b/src/checker.cpp
@@ -1792,7 +1792,22 @@ gb_internal void add_untyped(CheckerContext *c, Ast *expr, AddressingMode mode,
check_set_expr_info(c, expr, mode, type, value);
}
-gb_internal void add_type_and_value(CheckerContext *ctx, Ast *expr, AddressingMode mode, Type *type, ExactValue const &value, bool use_mutex) {
+struct TypeAndValueMutexStripes alignas(GB_CACHE_LINE_SIZE) {
+ BlockingMutex mutex;
+ u8 padding[GB_CACHE_LINE_SIZE - gb_size_of(BlockingMutex)];
+};
+
+enum { TypeAndValueMutexStripes_COUNT = 128 };
+gb_global TypeAndValueMutexStripes tav_mutex_stripes[TypeAndValueMutexStripes_COUNT];
+
+gb_internal BlockingMutex *tav_mutex_for_node(Ast *node) {
+ GB_ASSERT(node != nullptr);
+ uintptr h = cast(uintptr)node;
+ h ^= h >> 6;
+ return &tav_mutex_stripes[h % TypeAndValueMutexStripes_COUNT].mutex;
+}
+
+gb_internal void add_type_and_value(CheckerContext *ctx, Ast *expr, AddressingMode mode, Type *type, ExactValue const &value) {
if (expr == nullptr) {
return;
}
@@ -1803,14 +1818,18 @@ gb_internal void add_type_and_value(CheckerContext *ctx, Ast *expr, AddressingMo
return;
}
- BlockingMutex *mutex = &ctx->info->type_and_value_mutex;
- if (ctx->decl) {
- mutex = &ctx->decl->type_and_value_mutex;
- } else if (ctx->pkg) {
- mutex = &ctx->pkg->type_and_value_mutex;
- }
+ BlockingMutex *mutex = tav_mutex_for_node(expr);
+
+ /* Previous logic:
+ BlockingMutex *mutex = &ctx->info->type_and_value_mutex;
+ if (ctx->decl) {
+ mutex = &ctx->decl->type_and_value_mutex;
+ } else if (ctx->pkg) {
+ mutex = &ctx->pkg->type_and_value_mutex;
+ }
+ */
- if (use_mutex) mutex_lock(mutex);
+ mutex_lock(mutex);
Ast *prev_expr = nullptr;
while (prev_expr != expr) {
prev_expr = expr;
@@ -1835,7 +1854,7 @@ gb_internal void add_type_and_value(CheckerContext *ctx, Ast *expr, AddressingMo
break;
};
}
- if (use_mutex) mutex_unlock(mutex);
+ mutex_unlock(mutex);
}
gb_internal void add_entity_definition(CheckerInfo *i, Ast *identifier, Entity *entity) {
diff --git a/src/checker.hpp b/src/checker.hpp
index 7ca50d593..374aaf10d 100644
--- a/src/checker.hpp
+++ b/src/checker.hpp
@@ -631,7 +631,7 @@ gb_internal void scope_lookup_parent (Scope *s, String const &name, Scope **s
gb_internal Entity *scope_insert (Scope *s, Entity *entity);
-gb_internal void add_type_and_value (CheckerContext *c, Ast *expression, AddressingMode mode, Type *type, ExactValue const &value, bool use_mutex=true);
+gb_internal void add_type_and_value (CheckerContext *c, Ast *expression, AddressingMode mode, Type *type, ExactValue const &value);
gb_internal ExprInfo *check_get_expr_info (CheckerContext *c, Ast *expr);
gb_internal void add_untyped (CheckerContext *c, Ast *expression, AddressingMode mode, Type *basic_type, ExactValue const &value);
gb_internal void add_entity_use (CheckerContext *c, Ast *identifier, Entity *entity);