-
-
Notifications
You must be signed in to change notification settings - Fork 14.8k
Expand file tree
/
Copy pathvec_cache.rs
More file actions
555 lines (486 loc) · 19.9 KB
/
vec_cache.rs
File metadata and controls
555 lines (486 loc) · 19.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
//! VecCache maintains a mapping from K -> (V, I) pairing. K and I must be roughly u32-sized, and V
//! must be Copy.
//!
//! VecCache supports efficient concurrent put/get across the key space, with write-once semantics
//! (i.e., a given key can only be put once). Subsequent puts will panic.
//!
//! This is currently used for query caching.
use std::fmt::{self, Debug};
use std::marker::PhantomData;
use std::ops::{Index, IndexMut};
use std::sync::atomic::{AtomicPtr, AtomicU32, AtomicUsize, Ordering};
use rustc_index::Idx;
#[cfg(test)]
mod tests;
struct Slot<V> {
// We never construct &Slot<V> so it's fine for this to not be in an UnsafeCell.
value: V,
// This is both an index and a once-lock.
//
// 0: not yet initialized.
// 1: lock held, initializing.
// 2..u32::MAX - 2: initialized.
index_and_lock: AtomicU32,
}
/// This uniquely identifies a single `Slot<V>` entry in the buckets map, and provides accessors for
/// either getting the value or putting a value.
#[derive(Copy, Clone, Debug)]
struct SlotIndex {
// the index of the bucket in VecCache (0 to 20)
bucket_idx: BucketIndex,
// the index of the slot within the bucket
index_in_bucket: usize,
}
// This makes sure the counts are consistent with what we allocate, precomputing each bucket a
// compile-time. Visiting all powers of two is enough to hit all the buckets.
//
// We confirm counts are accurate in the slot_index_exhaustive test.
const ENTRIES_BY_BUCKET: [usize; BUCKETS] = {
let mut entries = [0; BUCKETS];
let mut key = 0;
loop {
let si = SlotIndex::from_index(key);
entries[si.bucket_idx.to_usize()] = si.bucket_idx.capacity();
if key == 0 {
key = 1;
} else if key == (1 << 31) {
break;
} else {
key <<= 1;
}
}
entries
};
const BUCKETS: usize = 21;
impl SlotIndex {
/// Unpacks a flat 32-bit index into a [`BucketIndex`] and a slot offset within that bucket.
#[inline]
const fn from_index(idx: u32) -> Self {
let (bucket_idx, index_in_bucket) = BucketIndex::from_flat_index(idx as usize);
SlotIndex { bucket_idx, index_in_bucket }
}
// SAFETY: Buckets must be managed solely by functions here (i.e., get/put on SlotIndex) and
// `self` comes from SlotIndex::from_index
#[inline]
unsafe fn get<V: Copy>(&self, buckets: &[AtomicPtr<Slot<V>>; 21]) -> Option<(V, u32)> {
let bucket = &buckets[self.bucket_idx];
let ptr = bucket.load(Ordering::Acquire);
// Bucket is not yet initialized: then we obviously won't find this entry in that bucket.
if ptr.is_null() {
return None;
}
debug_assert!(self.index_in_bucket < self.bucket_idx.capacity());
// SAFETY: `bucket` was allocated (so <= isize in total bytes) to hold `entries`, so this
// must be inbounds.
let slot = unsafe { ptr.add(self.index_in_bucket) };
// SAFETY: initialized bucket has zeroed all memory within the bucket, so we are valid for
// AtomicU32 access.
let index_and_lock = unsafe { &(*slot).index_and_lock };
let current = index_and_lock.load(Ordering::Acquire);
let index = match current {
0 => return None,
// Treat "initializing" as actually just not initialized at all.
// The only reason this is a separate state is that `complete` calls could race and
// we can't allow that, but from load perspective there's no difference.
1 => return None,
_ => current - 2,
};
// SAFETY:
// * slot is a valid pointer (buckets are always valid for the index we get).
// * value is initialized since we saw a >= 2 index above.
// * `V: Copy`, so safe to read.
let value = unsafe { (*slot).value };
Some((value, index))
}
fn bucket_ptr<V>(&self, bucket: &AtomicPtr<Slot<V>>) -> *mut Slot<V> {
let ptr = bucket.load(Ordering::Acquire);
if ptr.is_null() { Self::initialize_bucket(bucket, self.bucket_idx) } else { ptr }
}
#[cold]
#[inline(never)]
fn initialize_bucket<V>(bucket: &AtomicPtr<Slot<V>>, bucket_idx: BucketIndex) -> *mut Slot<V> {
static LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(());
// If we are initializing the bucket, then acquire a global lock.
//
// This path is quite cold, so it's cheap to use a global lock. This ensures that we never
// have multiple allocations for the same bucket.
let _allocator_guard = LOCK.lock().unwrap_or_else(|e| e.into_inner());
let ptr = bucket.load(Ordering::Acquire);
// OK, now under the allocator lock, if we're still null then it's definitely us that will
// initialize this bucket.
if ptr.is_null() {
let bucket_layout =
std::alloc::Layout::array::<Slot<V>>(bucket_idx.capacity()).unwrap();
// This is more of a sanity check -- this code is very cold, so it's safe to pay a
// little extra cost here.
assert!(bucket_layout.size() > 0);
// SAFETY: Just checked that size is non-zero.
let allocated = unsafe { std::alloc::alloc_zeroed(bucket_layout).cast::<Slot<V>>() };
if allocated.is_null() {
std::alloc::handle_alloc_error(bucket_layout);
}
bucket.store(allocated, Ordering::Release);
allocated
} else {
// Otherwise some other thread initialized this bucket after we took the lock. In that
// case, just return early.
ptr
}
}
/// Returns true if this successfully put into the map.
#[inline]
fn put<V>(&self, buckets: &[AtomicPtr<Slot<V>>; 21], value: V, extra: u32) -> bool {
let bucket = &buckets[self.bucket_idx];
let ptr = self.bucket_ptr(bucket);
debug_assert!(self.index_in_bucket < self.bucket_idx.capacity());
// SAFETY: `bucket` was allocated (so <= isize in total bytes) to hold `entries`, so this
// must be inbounds.
let slot = unsafe { ptr.add(self.index_in_bucket) };
// SAFETY: initialized bucket has zeroed all memory within the bucket, so we are valid for
// AtomicU32 access.
let index_and_lock = unsafe { &(*slot).index_and_lock };
match index_and_lock.compare_exchange(0, 1, Ordering::AcqRel, Ordering::Acquire) {
Ok(_) => {
// We have acquired the initialization lock. It is our job to write `value` and
// then set the lock to the real index.
unsafe {
(&raw mut (*slot).value).write(value);
}
index_and_lock.store(extra.checked_add(2).unwrap(), Ordering::Release);
true
}
// Treat "initializing" as the caller's fault. Callers are responsible for ensuring that
// there are no races on initialization. In the compiler's current usage for query
// caches, that's the "active query map" which ensures each query actually runs once
// (even if concurrently started).
Err(1) => panic!("caller raced calls to put()"),
// This slot was already populated. Also ignore, currently this is the same as
// "initializing".
Err(_) => false,
}
}
/// Inserts into the map, given that the slot is unique, so it won't race with other threads.
#[inline]
unsafe fn put_unique<V>(&self, buckets: &[AtomicPtr<Slot<V>>; 21], value: V, extra: u32) {
let bucket = &buckets[self.bucket_idx];
let ptr = self.bucket_ptr(bucket);
debug_assert!(self.index_in_bucket < self.bucket_idx.capacity());
// SAFETY: `bucket` was allocated (so <= isize in total bytes) to hold `entries`, so this
// must be inbounds.
let slot = unsafe { ptr.add(self.index_in_bucket) };
// SAFETY: We known our slot is unique as a precondition of this function, so this can't race.
unsafe {
(&raw mut (*slot).value).write(value);
}
// SAFETY: initialized bucket has zeroed all memory within the bucket, so we are valid for
// AtomicU32 access.
let index_and_lock = unsafe { &(*slot).index_and_lock };
index_and_lock.store(extra.checked_add(2).unwrap(), Ordering::Release);
}
#[inline]
unsafe fn remove<V>(&self, buckets: &[AtomicPtr<Slot<V>>; 21]) {
let bucket = &buckets[self.bucket_idx];
let ptr = self.bucket_ptr(bucket);
debug_assert!(self.index_in_bucket < self.bucket_idx.capacity());
// SAFETY: `bucket` was allocated (so <= isize in total bytes) to hold `entries`, so this
// must be inbounds.
let slot = unsafe { ptr.add(self.index_in_bucket) };
// SAFETY: initialized bucket has zeroed all memory within the bucket, so we are valid for
// AtomicU32 access.
let index_and_lock = unsafe { &(*slot).index_and_lock };
index_and_lock.store(0, Ordering::Release);
}
}
/// In-memory cache for queries whose keys are densely-numbered IDs
/// (e.g `CrateNum`, `LocalDefId`), and can therefore be used as indices
/// into a dense vector of cached values.
///
/// (As of [#124780] the underlying storage is not an actual `Vec`, but rather
/// a series of increasingly-large buckets, for improved performance when the
/// parallel frontend is using multiple threads.)
///
/// Each entry in the cache stores the query's return value (`V`), and also
/// an associated index (`I`), which in practice is a `DepNodeIndex` used for
/// query dependency tracking.
///
/// [#124780]: https://github.com/rust-lang/rust/pull/124780
pub struct VecCache<K: Idx, V, I> {
// Entries per bucket:
// Bucket 0: 4096 2^12
// Bucket 1: 4096 2^12
// Bucket 2: 8192
// Bucket 3: 16384
// ...
// Bucket 19: 1073741824
// Bucket 20: 2147483648
// The total number of entries if all buckets are initialized is 2^32.
buckets: [AtomicPtr<Slot<V>>; BUCKETS],
// In the compiler's current usage these are only *read* during incremental and self-profiling.
// They are an optimization over iterating the full buckets array.
present: [AtomicPtr<Slot<()>>; BUCKETS],
len: AtomicUsize,
key: PhantomData<(K, I)>,
}
impl<K: Idx, V, I> Default for VecCache<K, V, I> {
fn default() -> Self {
VecCache {
buckets: Default::default(),
key: PhantomData,
len: Default::default(),
present: Default::default(),
}
}
}
// SAFETY: No access to `V` is made.
unsafe impl<K: Idx, #[may_dangle] V, I> Drop for VecCache<K, V, I> {
fn drop(&mut self) {
// We have unique ownership, so no locks etc. are needed. Since `K` and `V` are both `Copy`,
// we are also guaranteed to just need to deallocate any large arrays (not iterate over
// contents).
//
// Confirm no need to deallocate individual entries. Note that `V: Copy` is asserted on
// insert/lookup but not necessarily construction, primarily to avoid annoyingly propagating
// the bounds into struct definitions everywhere.
assert!(!std::mem::needs_drop::<K>());
assert!(!std::mem::needs_drop::<V>());
for (idx, bucket) in BucketIndex::enumerate_buckets(&self.buckets) {
let bucket = bucket.load(Ordering::Acquire);
if !bucket.is_null() {
let layout = std::alloc::Layout::array::<Slot<V>>(ENTRIES_BY_BUCKET[idx]).unwrap();
unsafe {
std::alloc::dealloc(bucket.cast(), layout);
}
}
}
for (idx, bucket) in BucketIndex::enumerate_buckets(&self.present) {
let bucket = bucket.load(Ordering::Acquire);
if !bucket.is_null() {
let layout = std::alloc::Layout::array::<Slot<()>>(ENTRIES_BY_BUCKET[idx]).unwrap();
unsafe {
std::alloc::dealloc(bucket.cast(), layout);
}
}
}
}
}
impl<K, V, I> VecCache<K, V, I>
where
K: Eq + Idx + Copy + Debug,
V: Copy,
I: Idx + Copy,
{
#[inline(always)]
pub fn lookup(&self, key: &K) -> Option<(V, I)> {
let key = u32::try_from(key.index()).unwrap();
let slot_idx = SlotIndex::from_index(key);
match unsafe { slot_idx.get(&self.buckets) } {
Some((value, idx)) => Some((value, I::new(idx as usize))),
None => None,
}
}
#[inline]
pub fn complete(&self, key: K, value: V, index: I) {
let key = u32::try_from(key.index()).unwrap();
let slot_idx = SlotIndex::from_index(key);
if slot_idx.put(&self.buckets, value, index.index() as u32) {
let present_idx = self.len.fetch_add(1, Ordering::Relaxed);
let slot = SlotIndex::from_index(u32::try_from(present_idx).unwrap());
// SAFETY: We should always be uniquely putting due to `len` fetch_add returning unique values.
// We can't get here if `len` overflows because `put` will not succeed u32::MAX + 1 times
// as it will run out of slots.
unsafe { slot.put_unique(&self.present, (), key) };
}
}
pub fn for_each(&self, f: &mut dyn FnMut(&K, &V, I)) {
for idx in 0..self.len.load(Ordering::Acquire) {
let key = SlotIndex::from_index(idx as u32);
match unsafe { key.get(&self.present) } {
// This shouldn't happen in our current usage (iter is really only
// used long after queries are done running), but if we hit this in practice it's
// probably fine to just break early.
None => unreachable!(),
Some(((), key)) => {
let key = K::new(key as usize);
// unwrap() is OK: present entries are always written only after we put the real
// entry.
let value = self.lookup(&key).unwrap();
f(&key, &value.0, value.1);
}
}
}
}
pub fn len(&self) -> usize {
self.len.load(Ordering::Acquire)
}
pub fn remove(&self, key: &K) {
let key = u32::try_from(key.index()).unwrap();
let slot_idx = SlotIndex::from_index(key);
unsafe { slot_idx.remove(&self.buckets) };
}
pub fn invalidate(&self, selector: impl Fn(K) -> bool) {
let mut to_remove = vec![];
let mut remaining = vec![];
self.for_each(&mut |key, _, _| {
if selector(*key) {
to_remove.push(*key);
} else {
remaining.push(*key);
}
});
for key in to_remove {
self.remove(&key);
}
for (index, key) in remaining.iter().enumerate() {
let slot = SlotIndex::from_index(u32::try_from(index).unwrap());
let key = u32::try_from(key.index()).unwrap();
unsafe { slot.put_unique(&self.present, (), key) };
}
self.len.store(remaining.len(), Ordering::Release);
}
}
/// Index into an array of buckets.
///
/// Using an enum lets us tell the compiler that values range from 0 to 20,
/// allowing array bounds checks to be optimized away,
/// without having to resort to pattern types or other unstable features.
#[derive(Clone, Copy, PartialEq, Eq)]
#[repr(usize)]
enum BucketIndex {
// tidy-alphabetical-start
Bucket00,
Bucket01,
Bucket02,
Bucket03,
Bucket04,
Bucket05,
Bucket06,
Bucket07,
Bucket08,
Bucket09,
Bucket10,
Bucket11,
Bucket12,
Bucket13,
Bucket14,
Bucket15,
Bucket16,
Bucket17,
Bucket18,
Bucket19,
Bucket20,
// tidy-alphabetical-end
}
impl Debug for BucketIndex {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
Debug::fmt(&self.to_usize(), f)
}
}
impl BucketIndex {
/// Capacity of bucket 0 (and also of bucket 1).
const BUCKET_0_CAPACITY: usize = 1 << (Self::NONZERO_BUCKET_SHIFT_ADJUST + 1);
/// Adjustment factor from the highest-set-bit-position of a flat index,
/// to its corresponding bucket number.
///
/// For example, the first flat-index in bucket 2 is 8192.
/// Its highest-set-bit-position is `(8192).ilog2() == 13`, and subtracting
/// the adjustment factor of 11 gives the bucket number of 2.
const NONZERO_BUCKET_SHIFT_ADJUST: usize = 11;
#[inline(always)]
const fn to_usize(self) -> usize {
self as usize
}
#[inline(always)]
const fn from_raw(raw: usize) -> Self {
match raw {
// tidy-alphabetical-start
00 => Self::Bucket00,
01 => Self::Bucket01,
02 => Self::Bucket02,
03 => Self::Bucket03,
04 => Self::Bucket04,
05 => Self::Bucket05,
06 => Self::Bucket06,
07 => Self::Bucket07,
08 => Self::Bucket08,
09 => Self::Bucket09,
10 => Self::Bucket10,
11 => Self::Bucket11,
12 => Self::Bucket12,
13 => Self::Bucket13,
14 => Self::Bucket14,
15 => Self::Bucket15,
16 => Self::Bucket16,
17 => Self::Bucket17,
18 => Self::Bucket18,
19 => Self::Bucket19,
20 => Self::Bucket20,
// tidy-alphabetical-end
_ => panic!("bucket index out of range"),
}
}
/// Total number of slots in this bucket.
#[inline(always)]
const fn capacity(self) -> usize {
match self {
Self::Bucket00 => Self::BUCKET_0_CAPACITY,
// Bucket 1 has a capacity of `1 << (1 + 11) == pow(2, 12) == 4096`.
// Bucket 2 has a capacity of `1 << (2 + 11) == pow(2, 13) == 8192`.
_ => 1 << (self.to_usize() + Self::NONZERO_BUCKET_SHIFT_ADJUST),
}
}
/// Converts a flat index in the range `0..=u32::MAX` into a bucket index,
/// and a slot offset within that bucket.
///
/// Panics if `flat > u32::MAX`.
#[inline(always)]
const fn from_flat_index(flat: usize) -> (Self, usize) {
if flat > u32::MAX as usize {
panic!();
}
// If the index is in bucket 0, the conversion is trivial.
// This also avoids calling `ilog2` when `flat == 0`.
if flat < Self::BUCKET_0_CAPACITY {
return (Self::Bucket00, flat);
}
// General-case conversion for a non-zero bucket index.
//
// | bucket | slot
// flat | ilog2 | index | offset
// ------------------------------
// 4096 | 12 | 1 | 0
// 4097 | 12 | 1 | 1
// ...
// 8191 | 12 | 1 | 4095
// 8192 | 13 | 2 | 0
let highest_bit_pos = flat.ilog2() as usize;
let bucket_index =
BucketIndex::from_raw(highest_bit_pos - Self::NONZERO_BUCKET_SHIFT_ADJUST);
// Clear the highest-set bit (which selects the bucket) to get the
// slot offset within this bucket.
let slot_offset = flat - (1 << highest_bit_pos);
(bucket_index, slot_offset)
}
#[inline(always)]
fn iter_all() -> impl ExactSizeIterator<Item = Self> {
(0usize..BUCKETS).map(BucketIndex::from_raw)
}
#[inline(always)]
fn enumerate_buckets<T>(buckets: &[T; BUCKETS]) -> impl ExactSizeIterator<Item = (Self, &T)> {
BucketIndex::iter_all().zip(buckets)
}
}
impl<T> Index<BucketIndex> for [T; BUCKETS] {
type Output = T;
#[inline(always)]
fn index(&self, index: BucketIndex) -> &Self::Output {
// The optimizer should be able to see that see that a bucket index is
// always in-bounds, and omit the runtime bounds check.
&self[index.to_usize()]
}
}
impl<T> IndexMut<BucketIndex> for [T; BUCKETS] {
#[inline(always)]
fn index_mut(&mut self, index: BucketIndex) -> &mut Self::Output {
&mut self[index.to_usize()]
}
}