You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@datafusion.apache.org by gi...@apache.org on 2026/09/24 23:50:51 UTC
(datafusion-comet) 01/01: perf: make one thread-local access per tracked allocation (#6166)
This is an automated email from the ASF dual-hosted git repository.
github-merge-queue[bot] pushed a commit to branch gh-readonly-queue/main/pr-6166-801b099101b685d661e9332588f9de6fc9e194da
in repository https://gitbox.apache.org/repos/asf/datafusion-comet.git
commit 4453c57249aa4bf9c47fd4ff88bd13a09cd2a9f8
Author: Andy Grove <ag...@apache.org>
AuthorDate: Thu Sep 24 23:50:28 2026 +0000
perf: make one thread-local access per tracked allocation (#6166)
* perf: make one thread-local access per tracked allocation
libcomet is loaded with dlopen, so its thread-locals use the
general-dynamic TLS model and every access calls __tls_get_addr. track()
made three accesses per allocation and per free (the re-entrancy flag
twice, and the drift cell with its lazy-destructor state check), which
cost about 5% on TPC-H Q21.
Keep the drift and a phase in one const-initialized thread-local with
no destructor, so the fast path is one access, a phase check and an add.
The exit-time settle moves to a zero-sized guard thread-local that is
registered once per thread, on its first tracked allocation, with the
phase marking the re-entrant allocations registration can make. After
the guard's destructor runs, deltas go straight to the shared balance.
Closes #6165.
* perf: delete IN_TRACK rather than restructure the thread state
The re-entrancy guard was the only reason track() made a second
thread-local access. It protected against registering LOCAL_DRIFT's
destructor allocating through the global allocator, which std stopped
doing in Rust 1.93: the destructor list moved to the System allocator,
and GlobalAlloc now documents that thread_local! never uses
#[global_allocator]. The workspace already requires Rust 1.94.
Without the guard, LOCAL_DRIFT.try_with is the only thread-local access,
and std already keeps LOCAL_DRIFT's Initial/Alive/Destroyed state next
to the value. That is the state machine the previous commit built by
hand, so this returns to main's design minus the guard.
---
native/core/src/alloc_accounting.rs | 26 +++++++-------------------
1 file changed, 7 insertions(+), 19 deletions(-)
diff --git a/native/core/src/alloc_accounting.rs b/native/core/src/alloc_accounting.rs
index 461e26739e..d59c1d6788 100644
--- a/native/core/src/alloc_accounting.rs
+++ b/native/core/src/alloc_accounting.rs
@@ -43,15 +43,9 @@ const SETTLE_THRESHOLD: isize = 64 * 1024;
static BALANCE: AtomicIsize = AtomicIsize::new(0);
thread_local! {
- /// Set while this thread is inside [`track`], so an allocation made *by* `track` settles
- /// directly instead of recursing. The only such allocation today is the one some platforms
- /// make when registering `LOCAL_DRIFT`'s destructor on first touch.
- ///
- /// Const-initialized and destructor-free, so reading it never allocates and never fails,
- /// which is what makes it safe to consult before touching `LOCAL_DRIFT`.
- static IN_TRACK: Cell<bool> = const { Cell::new(false) };
-
- /// This thread's un-flushed delta.
+ /// This thread's un-flushed delta, and the only thread-local [`track`] touches. `libcomet` is
+ /// loaded with `dlopen`, so each thread-local an allocation touches costs a call into the
+ /// dynamic loader.
static LOCAL_DRIFT: ThreadDrift = const { ThreadDrift(Cell::new(0)) };
}
@@ -100,13 +94,9 @@ fn track(delta: isize) {
return;
}
- // A re-entrant call is one made by `track` itself; the outer frame owns the flag and will
- // clear it, so this frame must only settle and return.
- if IN_TRACK.with(|in_track| in_track.replace(true)) {
- BALANCE.fetch_add(delta, Ordering::Relaxed);
- return;
- }
-
+ // `thread_local!` never allocates through the global allocator (a `GlobalAlloc` guarantee
+ // since Rust 1.93), so first touching `LOCAL_DRIFT` cannot re-enter `track`.
+ //
// `try_with` rather than `with`: during thread teardown `LOCAL_DRIFT`'s destructor has already
// run, and any allocation after that point must not panic inside the allocator.
if LOCAL_DRIFT
@@ -115,8 +105,6 @@ fn track(delta: isize) {
{
BALANCE.fetch_add(delta, Ordering::Relaxed);
}
-
- IN_TRACK.with(|in_track| in_track.set(false));
}
/// Wraps a global allocator, accounting the `Layout` bytes it hands out.
@@ -135,7 +123,7 @@ impl<A: GlobalAlloc> AccountingAllocator<A> {
}
// SAFETY: every method delegates to `inner`, which upholds the `GlobalAlloc` contract. The
-// accounting is pure bookkeeping over an `AtomicIsize` and thread-local `Cell`s: it does not
+// accounting is pure bookkeeping over an `AtomicIsize` and a thread-local `Cell`: it does not
// inspect, retain, or alter any pointer, and it cannot unwind.
unsafe impl<A: GlobalAlloc> GlobalAlloc for AccountingAllocator<A> {
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@datafusion.apache.org
For additional commands, e-mail: commits-help@datafusion.apache.org