Unverified Commit 0bb13fa6 authored by Luke Sandberg's avatar Luke Sandberg Committed by GitHub

[turbopack] optimize the TurboMalloc threadlocals (#80265)

Improve memory tracking in turbo-tasks-malloc

### What?

Made the thread_local initializer `const` compatible.  This skips per-thread lazy initialization of the threadlocal datastructure which allows us to skip a small amount of bootstrapping logic.

Additionally, this allows us to access the native llvm implementation of threadlocals which can improve performance.  See https://matklad.github.io/2020/10/03/fast-thread-locals-in-rust.html

### Performance

Confusingly, the benchmarks are not positive, but vercel-site appears to be.  Perhaps the 'small' benchmarks are just too small/

![image.png](https://graphite-user-uploaded-assets-prod.s3.amazonaws.com/AwJ29EfoPcPdLSwCZxAz/51a29e9b-d653-4ba1-8e18-063b6a60c3a9.png)
parent 6d9304b8
...@@ -6,6 +6,10 @@ use std::{ ...@@ -6,6 +6,10 @@ use std::{
use crate::AllocationCounters; use crate::AllocationCounters;
/// Tracks the current total amount of memory allocated through all the [ThreadLocalCounter]
/// instances. This is an overestimate as individual threads 'preallocate' a [TARGET_BUFFER] bytes
/// to reduce the number of global synchronizations. This means at any given time this might
/// overcount by up to [MAX_BUFFER] bytes for each thread.
static ALLOCATED: AtomicUsize = AtomicUsize::new(0); static ALLOCATED: AtomicUsize = AtomicUsize::new(0);
const KB: usize = 1024; const KB: usize = 1024;
/// When global counter is updates we will keep a thread-local buffer of this /// When global counter is updates we will keep a thread-local buffer of this
...@@ -26,6 +30,12 @@ struct ThreadLocalCounter { ...@@ -26,6 +30,12 @@ struct ThreadLocalCounter {
} }
impl ThreadLocalCounter { impl ThreadLocalCounter {
const fn new() -> Self {
Self {
buffer: 0,
allocation_counters: AllocationCounters::new(),
}
}
fn add(&mut self, size: usize) { fn add(&mut self, size: usize) {
self.allocation_counters.allocations += size; self.allocation_counters.allocations += size;
self.allocation_counters.allocation_count += 1; self.allocation_counters.allocation_count += 1;
...@@ -88,7 +98,7 @@ impl ThreadLocalCounter { ...@@ -88,7 +98,7 @@ impl ThreadLocalCounter {
} }
thread_local! { thread_local! {
static LOCAL_COUNTER: UnsafeCell<ThreadLocalCounter> = UnsafeCell::new(ThreadLocalCounter::default()); static LOCAL_COUNTER: UnsafeCell<ThreadLocalCounter> = const {UnsafeCell::new(ThreadLocalCounter::new())};
} }
pub fn get() -> usize { pub fn get() -> usize {
......
...@@ -34,6 +34,15 @@ pub struct AllocationCounters { ...@@ -34,6 +34,15 @@ pub struct AllocationCounters {
} }
impl AllocationCounters { impl AllocationCounters {
const fn new() -> Self {
Self {
allocation_count: 0,
deallocation_count: 0,
allocations: 0,
deallocations: 0,
_not_send: PhantomData {},
}
}
pub fn until_now(&self) -> AllocationInfo { pub fn until_now(&self) -> AllocationInfo {
let new = TurboMalloc::allocation_counters(); let new = TurboMalloc::allocation_counters();
AllocationInfo { AllocationInfo {
...@@ -50,6 +59,7 @@ impl AllocationCounters { ...@@ -50,6 +59,7 @@ impl AllocationCounters {
pub struct TurboMalloc; pub struct TurboMalloc;
impl TurboMalloc { impl TurboMalloc {
// Returns the current amount of memory
pub fn memory_usage() -> usize { pub fn memory_usage() -> usize {
get() get()
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment