Skip to content

Commit 633ad98

Browse files
committed
Remove redundant policy merge checking in walk_free_list
Because we only set the `NEXT_FREE_CELL_CAN_MERGE` bit when the policy says we should do merges, we don't need to double check with the policy (which is a virtual call) when walking the free list before checking if the bit is set.
1 parent 27ad865 commit 633ad98

File tree

1 file changed

+21
-19
lines changed

1 file changed

+21
-19
lines changed

wee_alloc/src/lib.rs

+21-19
Original file line numberDiff line numberDiff line change
@@ -844,27 +844,29 @@ where
844844

845845
let mut current_free = &mut *current_free;
846846

847-
if policy.should_merge_adjacent_free_cells() {
848-
// Now check if this cell can merge with the next cell in the free
849-
// list. We do this after the initial allocation attempt so that we
850-
// don't merge, only to immediately split the cell again right
851-
// afterwards.
852-
while current_free.next_free_can_merge() {
853-
let prev_adjacent = current_free.header.prev_cell_raw as *mut FreeCell;
854-
extra_assert_eq!(prev_adjacent, current_free.next_free());
855-
let prev_adjacent = &mut *prev_adjacent;
856-
857-
(*prev_adjacent).header.next_cell_raw = current_free.header.next_cell_raw;
858-
if let Some(next) = current_free.header.next_cell() {
859-
(*next).prev_cell_raw = &mut prev_adjacent.header;
860-
}
847+
// Now check if this cell can merge with the next cell in the free
848+
// list.
849+
//
850+
// We don't re-check `policy.should_merge_adjacent_free_cells()` because
851+
// the `NEXT_FREE_CELL_CAN_MERGE` bit only gets set after checking with
852+
// the policy.
853+
while current_free.next_free_can_merge() {
854+
extra_assert!(policy.should_merge_adjacent_free_cells());
855+
856+
let prev_adjacent = current_free.header.prev_cell_raw as *mut FreeCell;
857+
extra_assert_eq!(prev_adjacent, current_free.next_free());
858+
let prev_adjacent = &mut *prev_adjacent;
859+
860+
(*prev_adjacent).header.next_cell_raw = current_free.header.next_cell_raw;
861+
if let Some(next) = current_free.header.next_cell() {
862+
(*next).prev_cell_raw = &mut prev_adjacent.header;
863+
}
861864

862-
*previous_free = prev_adjacent;
863-
current_free = prev_adjacent;
865+
*previous_free = prev_adjacent;
866+
current_free = prev_adjacent;
864867

865-
write_free_pattern(current_free, policy);
866-
assert_local_cell_invariants(&mut current_free.header);
867-
}
868+
write_free_pattern(current_free, policy);
869+
assert_local_cell_invariants(&mut current_free.header);
868870
}
869871

870872
if let Some(result) = f(previous_free, current_free) {

0 commit comments

Comments
 (0)