Skip to content

Commit

Permalink
fix counting of reallocations
Browse files Browse the repository at this point in the history
  • Loading branch information
sokra committed Jun 11, 2024
1 parent 973eb82 commit 4552f45
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 11 deletions.
30 changes: 30 additions & 0 deletions crates/turbo-tasks-malloc/src/counter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,31 @@ impl ThreadLocalCounter {
}
}

fn update(&mut self, old_size: usize, new_size: usize) {
self.allocation_counters.deallocations += old_size;
self.allocation_counters.deallocation_count += 1;
self.allocation_counters.allocations += new_size;
self.allocation_counters.allocation_count += 1;
if old_size > new_size {

Check failure on line 57 in crates/turbo-tasks-malloc/src/counter.rs

View workflow job for this annotation

GitHub Actions / Turbopack rust clippy

`if` chain can be rewritten with `match`
let size = old_size - new_size;
self.buffer += size;
if self.buffer > MAX_BUFFER {
let offset = self.buffer - TARGET_BUFFER;
self.buffer = TARGET_BUFFER;
ALLOCATED.fetch_sub(offset, Ordering::Relaxed);
}
} else if new_size > old_size {
let size = new_size - old_size;
if self.buffer >= size {
self.buffer -= size;
} else {
let offset = size - self.buffer + TARGET_BUFFER;
self.buffer = TARGET_BUFFER;
ALLOCATED.fetch_add(offset, Ordering::Relaxed);
}
}
}

fn unload(&mut self) {
if self.buffer > 0 {
ALLOCATED.fetch_sub(self.buffer, Ordering::Relaxed);
Expand Down Expand Up @@ -93,6 +118,11 @@ pub fn remove(size: usize) {
with_local_counter(|local| local.remove(size));
}

/// Adds some `size` to the global counter in a thread-local buffered way.
pub fn update(old_size: usize, new_size: usize) {
with_local_counter(|local| local.update(old_size, new_size));
}

/// Flushes the thread-local buffer to the global counter. This should be called
/// e. g. when a thread is stopped or goes to sleep for a long time.
pub fn flush() {
Expand Down
14 changes: 3 additions & 11 deletions crates/turbo-tasks-malloc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::{
marker::PhantomData,
};

use self::counter::{add, flush, get, remove};
use self::counter::{add, flush, get, remove, update};

#[derive(Default, Clone, Debug)]
pub struct AllocationInfo {
Expand Down Expand Up @@ -97,11 +97,7 @@ unsafe impl GlobalAlloc for TurboMalloc {
let ret = mimalloc::MiMalloc.realloc(ptr, layout, new_size);
if !ret.is_null() {
let old_size = layout.size();
if old_size < new_size {
add(new_size - old_size);
} else {
remove(old_size - new_size);
}
update(old_size, new_size);
}
ret
}
Expand Down Expand Up @@ -137,11 +133,7 @@ unsafe impl GlobalAlloc for TurboMalloc {
let ret = std::alloc::System.realloc(ptr, layout, new_size);
if !ret.is_null() {
let old_size = layout.size();
if old_size < new_size {
add(new_size - old_size);
} else {
remove(old_size - new_size);
}
update(old_size, new_size);
}
ret
}
Expand Down

0 comments on commit 4552f45

Please sign in to comment.