Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 34c5ab9

Browse files
committedNov 25, 2023
Auto merge of rust-lang#118227 - Mark-Simulacrum:worker-local-outline, r=cjgillot
Optimize QueryArena allocation This shifts the WorkerLocal wrapper to be outside the QueryArena, meaning that instead of having each query allocate distinct arenas per-worker we allocate the full set of arenas per-worker. This is primarily a code size optimization (locally, ~85 kilobytes, [perf is reporting >100 kilobytes](https://perf.rust-lang.org/compare.html?start=1fd418f92ed13db88a21865ba5d909abcf16b6cc&end=884c95a3f1fe8d28630ec3cdb0c8f95b2e539fde&stat=instructions%3Au&tab=artifact-size)), saving a bunch of code in the initialization of the arenas which was previously duplicated lots of times (per arena type). Additionally this tells LLVM that the thread count can't be zero in this code (I believe this is true?) which shaves some small amount of bytes off as well since we eliminate checks for zero in the vec allocations.
2 parents 2a48155 + 107ea5d commit 34c5ab9

File tree

4 files changed

+12
-9
lines changed

4 files changed

+12
-9
lines changed
 

‎compiler/rustc_data_structures/src/sync/worker_local.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use parking_lot::Mutex;
22
use std::cell::Cell;
33
use std::cell::OnceCell;
4+
use std::num::NonZeroUsize;
45
use std::ops::Deref;
56
use std::ptr;
67
use std::sync::Arc;
@@ -30,7 +31,7 @@ impl RegistryId {
3031
}
3132

3233
struct RegistryData {
33-
thread_limit: usize,
34+
thread_limit: NonZeroUsize,
3435
threads: Mutex<usize>,
3536
}
3637

@@ -60,7 +61,7 @@ thread_local! {
6061

6162
impl Registry {
6263
/// Creates a registry which can hold up to `thread_limit` threads.
63-
pub fn new(thread_limit: usize) -> Self {
64+
pub fn new(thread_limit: NonZeroUsize) -> Self {
6465
Registry(Arc::new(RegistryData { thread_limit, threads: Mutex::new(0) }))
6566
}
6667

@@ -73,7 +74,7 @@ impl Registry {
7374
/// Panics if the thread limit is hit or if the thread already has an associated registry.
7475
pub fn register(&self) {
7576
let mut threads = self.0.threads.lock();
76-
if *threads < self.0.thread_limit {
77+
if *threads < self.0.thread_limit.get() {
7778
REGISTRY.with(|registry| {
7879
if registry.get().is_some() {
7980
drop(threads);
@@ -126,7 +127,9 @@ impl<T> WorkerLocal<T> {
126127
{
127128
let registry = Registry::current();
128129
WorkerLocal {
129-
locals: (0..registry.0.thread_limit).map(|i| CacheAligned(initial(i))).collect(),
130+
locals: (0..registry.0.thread_limit.get())
131+
.map(|i| CacheAligned(initial(i)))
132+
.collect(),
130133
registry,
131134
}
132135
}

‎compiler/rustc_interface/src/util.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ pub(crate) fn run_in_thread_pool_with_globals<F: FnOnce() -> R + Send, R: Send>(
107107
use rustc_query_impl::QueryCtxt;
108108
use rustc_query_system::query::{deadlock, QueryContext};
109109

110-
let registry = sync::Registry::new(threads);
110+
let registry = sync::Registry::new(std::num::NonZeroUsize::new(threads).unwrap());
111111

112112
if !sync::is_dyn_thread_safe() {
113113
return run_in_thread_with_globals(edition, || {

‎compiler/rustc_middle/src/query/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ use rustc_data_structures::fx::{FxHashMap, FxIndexMap, FxIndexSet};
6161
use rustc_data_structures::steal::Steal;
6262
use rustc_data_structures::svh::Svh;
6363
use rustc_data_structures::sync::Lrc;
64-
use rustc_data_structures::sync::WorkerLocal;
6564
use rustc_data_structures::unord::UnordSet;
6665
use rustc_errors::ErrorGuaranteed;
6766
use rustc_hir as hir;

‎compiler/rustc_middle/src/query/plumbing.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use field_offset::FieldOffset;
1111
use measureme::StringId;
1212
use rustc_data_structures::fx::FxHashMap;
1313
use rustc_data_structures::sync::AtomicU64;
14+
use rustc_data_structures::sync::WorkerLocal;
1415
use rustc_hir::def::DefKind;
1516
use rustc_hir::def_id::{DefId, LocalDefId};
1617
use rustc_hir::hir_id::OwnerId;
@@ -71,7 +72,7 @@ pub struct QuerySystemFns<'tcx> {
7172

7273
pub struct QuerySystem<'tcx> {
7374
pub states: QueryStates<'tcx>,
74-
pub arenas: QueryArenas<'tcx>,
75+
pub arenas: WorkerLocal<QueryArenas<'tcx>>,
7576
pub caches: QueryCaches<'tcx>,
7677
pub dynamic_queries: DynamicQueries<'tcx>,
7778

@@ -370,7 +371,7 @@ macro_rules! define_callbacks {
370371

371372
pub struct QueryArenas<'tcx> {
372373
$($(#[$attr])* pub $name: query_if_arena!([$($modifiers)*]
373-
(WorkerLocal<TypedArena<<$V as Deref>::Target>>)
374+
(TypedArena<<$V as Deref>::Target>)
374375
()
375376
),)*
376377
}
@@ -379,7 +380,7 @@ macro_rules! define_callbacks {
379380
fn default() -> Self {
380381
Self {
381382
$($name: query_if_arena!([$($modifiers)*]
382-
(WorkerLocal::new(|_| Default::default()))
383+
(Default::default())
383384
()
384385
),)*
385386
}

0 commit comments

Comments
 (0)
Please sign in to comment.