diff --git a/yjit/src/codegen.rs b/yjit/src/codegen.rs index aac6dab18c..2f41439a41 100644 --- a/yjit/src/codegen.rs +++ b/yjit/src/codegen.rs @@ -831,7 +831,7 @@ pub fn gen_single_block( let gc_offsets = asm.compile(cb); // Add the GC offsets to the block - block.add_gc_obj_offsets(gc_offsets); + block.set_gc_obj_offsets(gc_offsets); // Mark the end position of the block block.set_end_addr(cb.get_write_ptr()); diff --git a/yjit/src/core.rs b/yjit/src/core.rs index e296afc4da..cd10bd0f04 100644 --- a/yjit/src/core.rs +++ b/yjit/src/core.rs @@ -544,7 +544,7 @@ pub struct Block { // FIXME: should these be code pointers instead? // Offsets for GC managed objects in the mainline code block - gc_obj_offsets: Vec, + gc_obj_offsets: Box<[u32]>, // CME dependencies of this block, to help to remove all pointers to this // block in the system. @@ -796,7 +796,7 @@ pub extern "C" fn rb_yjit_iseq_mark(payload: *mut c_void) { } // Walk over references to objects in generated code. - for offset in &block.gc_obj_offsets { + for offset in block.gc_obj_offsets.iter() { let value_address: *const u8 = cb.get_ptr(offset.as_usize()).raw_ptr(); // Creating an unaligned pointer is well defined unlike in C. let value_address = value_address as *const VALUE; @@ -843,7 +843,7 @@ pub extern "C" fn rb_yjit_iseq_update_references(payload: *mut c_void) { } // Walk over references to objects in generated code. - for offset in &block.gc_obj_offsets { + for offset in block.gc_obj_offsets.iter() { let offset_to_value = offset.as_usize(); let value_code_ptr = cb.get_ptr(offset_to_value); let value_ptr: *const u8 = value_code_ptr.raw_ptr(); @@ -1043,7 +1043,7 @@ fn add_block_version(blockref: &BlockRef, cb: &CodeBlock) { } // Run write barriers for all objects in generated code. - for offset in &block.gc_obj_offsets { + for offset in block.gc_obj_offsets.iter() { let value_address: *const u8 = cb.get_ptr(offset.as_usize()).raw_ptr(); // Creating an unaligned pointer is well defined unlike in C. let value_address: *const VALUE = value_address.cast(); @@ -1088,7 +1088,7 @@ impl Block { end_addr: None, incoming: Vec::new(), outgoing: Vec::new(), - gc_obj_offsets: Vec::new(), + gc_obj_offsets: Box::new([]), cme_dependencies: Vec::new(), entry_exit: None, }; @@ -1147,12 +1147,12 @@ impl Block { self.end_idx = end_idx; } - pub fn add_gc_obj_offsets(self: &mut Block, gc_offsets: Vec) { - for offset in gc_offsets { - self.gc_obj_offsets.push(offset); - incr_counter!(num_gc_obj_refs); + pub fn set_gc_obj_offsets(self: &mut Block, gc_offsets: Vec) { + assert_eq!(self.gc_obj_offsets.len(), 0); + if !gc_offsets.is_empty() { + incr_counter_by!(num_gc_obj_refs, gc_offsets.len()); + self.gc_obj_offsets = gc_offsets.into_boxed_slice(); } - self.gc_obj_offsets.shrink_to_fit(); } /// Instantiate a new CmeDependency struct and add it to the list of diff --git a/yjit/src/stats.rs b/yjit/src/stats.rs index 05e970702c..5d32f613c8 100644 --- a/yjit/src/stats.rs +++ b/yjit/src/stats.rs @@ -141,6 +141,19 @@ macro_rules! make_counters { } } +/// Macro to increase a counter by name and count +macro_rules! incr_counter_by { + // Unsafe is ok here because options are initialized + // once before any Ruby code executes + ($counter_name:ident, $count:expr) => { + #[allow(unused_unsafe)] + { + unsafe { $crate::stats::COUNTERS.$counter_name += $count as u64 } + } + }; +} +pub(crate) use incr_counter_by; + /// Macro to increment a counter by name macro_rules! incr_counter { // Unsafe is ok here because options are initialized