Skip to content

Commit

Permalink
Default cache size to 8K and trim largest objects on memory pressure
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Saveau <[email protected]>
  • Loading branch information
SUPERCILEX authored and nagisa committed Jan 1, 2024
1 parent a1cc397 commit 784b37b
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions tracing-tracy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ impl Default for TracyLayer {
}
}

static MAX_CACHE_SIZE: AtomicUsize = AtomicUsize::new(usize::MAX);
static MAX_CACHE_SIZE: AtomicUsize = AtomicUsize::new(8192);

/// Specify the maximum number of bytes used in thread local caches.
///
Expand All @@ -156,7 +156,7 @@ static MAX_CACHE_SIZE: AtomicUsize = AtomicUsize::new(usize::MAX);
/// no guarantees on the maximum memory used by tracing-tracy. Notably, changes to this value
/// are eventually consistent, i.e. caches are not flushed upon an update.
///
/// Defaults to [`usize::MAX`].
/// Defaults to `8192`.
pub fn set_max_cache_size(max_bytes_used_per_thread: usize) {
MAX_CACHE_SIZE.store(max_bytes_used_per_thread, Ordering::Relaxed);
}
Expand Down Expand Up @@ -410,13 +410,22 @@ mod utils {
// don't bother adding another cache entry as this keeps the logic simpler.
return;
};
if buf.capacity() == 0 || new_cache_size > MAX_CACHE_SIZE.load(Ordering::Relaxed) {
let max_size = MAX_CACHE_SIZE.load(Ordering::Relaxed);
if buf.capacity() == 0 || max_size == 0 {
return;
}
self.total_size.set(new_cache_size);

buf.clear();
self.str_bufs.push(buf);
self.total_size.set(new_cache_size);

if new_cache_size > max_size {
unsafe { &mut *self.str_bufs.0.get() }.sort_unstable_by_key(String::capacity);
if let Some(trimmed) = self.str_bufs.pop() {
self.total_size
.set(self.total_size.get() - trimmed.capacity());
}
}
}
}

Expand Down

0 comments on commit 784b37b

Please sign in to comment.