Skip to content

Commit 15bd4be

Browse files
Skip the write barrier when the plan has none
rb_gc_impl_writebarrier and rb_gc_impl_writebarrier_remember looked up the Ractor cache and called into Rust for every store, even for Immix, MarkSweep and NoGC, whose barrier is NoBarrier and does nothing. Lobsters runs about 920k barriers per iteration. In this commit we read the plan's barrier once in objspace_init through a new mmtk_plan_has_write_barrier, and return before the Ractor cache lookup and the FFI call when it is NoBarrier. StickyImmix keeps its object barrier.
1 parent 76a2123 commit 15bd4be

3 files changed

Lines changed: 18 additions & 1 deletion

File tree

‎gc/mmtk/mmtk.c‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ struct objspace {
5959
uintptr_t vo_bit_log_region_size;
6060
uintptr_t vo_bit_base_addr;
6161
size_t max_non_los_default_alloc_bytes;
62+
bool write_barrier_required;
6263
};
6364

6465
#define OBJ_FREE_BUF_CAPACITY 128
@@ -619,6 +620,7 @@ rb_gc_impl_objspace_init(void *objspace_ptr)
619620
objspace->vo_bit_log_region_size = mmtk_get_vo_bit_log_region_size();
620621
objspace->vo_bit_base_addr = mmtk_get_vo_bit_base_addr();
621622
objspace->max_non_los_default_alloc_bytes = mmtk_max_non_los_default_alloc_bytes();
623+
objspace->write_barrier_required = mmtk_plan_has_write_barrier();
622624
}
623625

624626
void
@@ -1235,7 +1237,7 @@ rb_gc_impl_location(void *objspace_ptr, VALUE obj)
12351237
void
12361238
rb_gc_impl_writebarrier(void *objspace_ptr, VALUE a, VALUE b)
12371239
{
1238-
struct MMTk_ractor_cache *cache = rb_gc_get_ractor_newobj_cache();
1240+
struct objspace *objspace = objspace_ptr;
12391241

12401242
if (SPECIAL_CONST_P(b)) return;
12411243

@@ -1254,6 +1256,9 @@ rb_gc_impl_writebarrier(void *objspace_ptr, VALUE a, VALUE b)
12541256
MMTK_ASSERT(BUILTIN_TYPE(a) != T_NONE);
12551257
MMTK_ASSERT(BUILTIN_TYPE(b) != T_NONE);
12561258

1259+
if (!objspace->write_barrier_required) return;
1260+
1261+
struct MMTk_ractor_cache *cache = rb_gc_get_ractor_newobj_cache();
12571262
mmtk_object_reference_write_post(cache->mutator, (MMTk_ObjectReference)a);
12581263
}
12591264

@@ -1272,6 +1277,10 @@ rb_gc_impl_obj_became_shareable(void *objspace_ptr, VALUE obj)
12721277
void
12731278
rb_gc_impl_writebarrier_remember(void *objspace_ptr, VALUE obj)
12741279
{
1280+
struct objspace *objspace = objspace_ptr;
1281+
1282+
if (!objspace->write_barrier_required) return;
1283+
12751284
if (!ruby_native_thread_p()) {
12761285
/* Called from an MMTk GC worker thread (via gc_ref_update_object's
12771286
* re-embedding path during reference updating). There is no mutator

‎gc/mmtk/mmtk.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ bool mmtk_gc_enabled_p(void);
9090

9191
size_t mmtk_max_non_los_default_alloc_bytes(void);
9292

93+
bool mmtk_plan_has_write_barrier(void);
94+
9395
MMTk_Address mmtk_alloc(MMTk_Mutator *mutator,
9496
size_t size,
9597
size_t align,

‎gc/mmtk/src/api.rs‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ use mmtk::MMTKBuilder;
2626
use mmtk::Mutator;
2727
use mmtk::memory_manager;
2828
use mmtk::memory_manager::mmtk_init;
29+
use mmtk::plan::BarrierSelector;
2930
use mmtk::util::Address;
3031
use mmtk::util::ObjectReference;
3132
use mmtk::util::VMMutatorThread;
@@ -344,6 +345,11 @@ pub extern "C" fn mmtk_max_non_los_default_alloc_bytes() -> usize {
344345
.max_non_los_default_alloc_bytes
345346
}
346347

348+
#[unsafe(no_mangle)]
349+
pub extern "C" fn mmtk_plan_has_write_barrier() -> bool {
350+
mmtk().get_plan().constraints().barrier != BarrierSelector::NoBarrier
351+
}
352+
347353
#[unsafe(no_mangle)]
348354
pub unsafe extern "C" fn mmtk_alloc(
349355
mutator: *mut RubyMutator,

0 commit comments

Comments
 (0)