Skip to content

Commit

Permalink
Move checks and borrows outside the loop
Browse files Browse the repository at this point in the history
  • Loading branch information
qinsoon committed Nov 29, 2024
1 parent a4d427b commit b8c8223
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
17 changes: 13 additions & 4 deletions src/scheduler/gc_work.rs
Original file line number Diff line number Diff line change
Expand Up @@ -828,12 +828,21 @@ pub trait ScanObjectsWork<VM: VMBinding>: GCWork<VM> + Sized {
let mut scan_later = vec![];
{
let mut closure = ObjectsClosure::<Self::E>::new(worker, self.get_bucket());
for object in objects_to_scan.iter().copied() {
// For any object we need to scan, we count its liv bytes
if *mmtk.get_options().count_live_bytes_in_gc {
closure.worker.shared.increase_live_bytes(object);

// For any object we need to scan, we count its live bytes.
// Check the option outside the loop for better performance.
if *mmtk.get_options().count_live_bytes_in_gc {
// Borrow before the loop.
let mut live_bytes_stats = closure.worker.shared.live_bytes_per_space.borrow_mut();
for object in objects_to_scan.iter().copied() {
crate::scheduler::worker::GCWorkerShared::<VM>::increase_live_bytes(
&mut live_bytes_stats,
object,
);
}
}

for object in objects_to_scan.iter().copied() {
if <VM as VMBinding>::VMScanning::support_slot_enqueuing(tls, object) {
trace!("Scan object (slot) {}", object);
// If an object supports slot-enqueuing, we enqueue its slots.
Expand Down
10 changes: 6 additions & 4 deletions src/scheduler/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ pub struct GCWorkerShared<VM: VMBinding> {
/// objects, we increase the live bytes. We get this value from each worker
/// at the end of a GC, and reset this counter.
/// The live bytes are stored in an array. The index is the index from the space descriptor.
live_bytes_per_space: AtomicRefCell<[usize; MAX_SPACES]>,
pub live_bytes_per_space: AtomicRefCell<[usize; MAX_SPACES]>,
/// A queue of GCWork that can only be processed by the owned thread.
pub designated_work: ArrayQueue<Box<dyn GCWork<VM>>>,
/// Handle for stealing packets from the current worker
Expand All @@ -60,7 +60,10 @@ impl<VM: VMBinding> GCWorkerShared<VM> {
}
}

pub(crate) fn increase_live_bytes(&self, object: ObjectReference) {
pub(crate) fn increase_live_bytes(
live_bytes_per_space: &mut [usize; MAX_SPACES],
object: ObjectReference,
) {
use crate::mmtk::VM_MAP;
use crate::vm::object_model::ObjectModel;

Expand All @@ -76,8 +79,7 @@ impl<VM: VMBinding> GCWorkerShared<VM> {
MAX_SPACES
);
// Accumulate the live bytes for the index
let mut array = self.live_bytes_per_space.borrow_mut();
array[space_descriptor.get_index()] += bytes;
live_bytes_per_space[space_descriptor.get_index()] += bytes;
}
}

Expand Down

0 comments on commit b8c8223

Please sign in to comment.