Skip to content

Commit

Permalink
Reduce calls to POSSIBLE_CYCLES.with/try_with
Browse files Browse the repository at this point in the history
  • Loading branch information
frengor committed Nov 7, 2023
1 parent 279b7ac commit 848cad6
Showing 1 changed file with 24 additions and 9 deletions.
33 changes: 24 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ pub fn collect_cycles() {
return;
}

collect(state);
let _ = POSSIBLE_CYCLES.try_with(|pc| {
collect(state, pc);
});

#[cfg(feature = "auto-collect")]
adjust_trigger_point(state);
Expand All @@ -51,7 +53,10 @@ pub fn collect_cycles() {
pub(crate) fn trigger_collection() {
let _ = try_state(|state| {
if !state.is_collecting() && config::config(|config| config.should_collect(state)).unwrap_or(false) {
collect(state);
let _ = POSSIBLE_CYCLES.try_with(|pc| {
collect(state, pc);
});

adjust_trigger_point(state);
}
});
Expand All @@ -62,7 +67,7 @@ fn adjust_trigger_point(state: &State) {
let _ = config::config(|config| config.adjust(state));
}

fn collect(state: &State) {
fn collect(state: &State, possible_cycles: &RefCell<List>) {
state.set_collecting(true);
state.increment_executions_count();

Expand All @@ -80,23 +85,23 @@ fn collect(state: &State) {
let _drop_guard = DropGuard { state };

#[cfg(feature = "finalization")]
while let Ok(false) = POSSIBLE_CYCLES.try_with(|pc| pc.borrow().is_empty()) {
__collect(state);
while !is_empty(possible_cycles) {
__collect(state, possible_cycles);
}
#[cfg(not(feature = "finalization"))]
if let Ok(false) = POSSIBLE_CYCLES.try_with(|pc| pc.borrow().is_empty()) {
__collect(state);
if !is_empty(possible_cycles) {
__collect(state, possible_cycles);
}

// _drop_guard is dropped here, setting state.collecting to false
}

fn __collect(state: &State) {
fn __collect(state: &State, possible_cycles: &RefCell<List>) {
let mut non_root_list = List::new();
{
let mut root_list = List::new();

while let Some(ptr) = POSSIBLE_CYCLES.with(|pc| pc.borrow_mut().remove_first()) {
while let Some(ptr) = get_and_remove_first(possible_cycles) {
// remove_first already marks ptr as NonMarked
trace_counting(ptr, &mut root_list, &mut non_root_list);
}
Expand Down Expand Up @@ -157,6 +162,16 @@ fn __collect(state: &State) {
}
}

#[inline]
fn is_empty(list: &RefCell<List>) -> bool {
list.borrow().is_empty()
}

#[inline]
fn get_and_remove_first(list: &RefCell<List>) -> Option<NonNull<CcOnHeap<()>>> {
list.borrow_mut().remove_first()
}

#[inline]
fn deallocate_list(to_deallocate_list: List, state: &State) {
let _dropping_guard = replace_state_field!(dropping, true, state);
Expand Down

0 comments on commit 848cad6

Please sign in to comment.