Skip to content

Commit 0809f78

Browse files
committed
Auto merge of #120527 - GnomedDev:atomicu32-handle, r=petrochenkov
Switch OwnedStore handle count to AtomicU32 This is already panics if overflowing a u32, so let's use the smaller int size to save a tiny bit of memory.
2 parents d4f6f9e + 3cc601a commit 0809f78

File tree

3 files changed

+10
-10
lines changed

3 files changed

+10
-10
lines changed

library/proc_macro/src/bridge/client.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use super::*;
44

55
use std::marker::PhantomData;
6+
use std::sync::atomic::AtomicU32;
67

78
macro_rules! define_handles {
89
(
@@ -12,17 +13,17 @@ macro_rules! define_handles {
1213
#[repr(C)]
1314
#[allow(non_snake_case)]
1415
pub struct HandleCounters {
15-
$($oty: AtomicUsize,)*
16-
$($ity: AtomicUsize,)*
16+
$($oty: AtomicU32,)*
17+
$($ity: AtomicU32,)*
1718
}
1819

1920
impl HandleCounters {
2021
// FIXME(eddyb) use a reference to the `static COUNTERS`, instead of
2122
// a wrapper `fn` pointer, once `const fn` can reference `static`s.
2223
extern "C" fn get() -> &'static Self {
2324
static COUNTERS: HandleCounters = HandleCounters {
24-
$($oty: AtomicUsize::new(1),)*
25-
$($ity: AtomicUsize::new(1),)*
25+
$($oty: AtomicU32::new(1),)*
26+
$($ity: AtomicU32::new(1),)*
2627
};
2728
&COUNTERS
2829
}

library/proc_macro/src/bridge/handle.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::collections::BTreeMap;
44
use std::hash::Hash;
55
use std::num::NonZeroU32;
66
use std::ops::{Index, IndexMut};
7-
use std::sync::atomic::{AtomicUsize, Ordering};
7+
use std::sync::atomic::{AtomicU32, Ordering};
88

99
use super::fxhash::FxHashMap;
1010

@@ -13,12 +13,12 @@ pub(super) type Handle = NonZeroU32;
1313
/// A store that associates values of type `T` with numeric handles. A value can
1414
/// be looked up using its handle.
1515
pub(super) struct OwnedStore<T: 'static> {
16-
counter: &'static AtomicUsize,
16+
counter: &'static AtomicU32,
1717
data: BTreeMap<Handle, T>,
1818
}
1919

2020
impl<T> OwnedStore<T> {
21-
pub(super) fn new(counter: &'static AtomicUsize) -> Self {
21+
pub(super) fn new(counter: &'static AtomicU32) -> Self {
2222
// Ensure the handle counter isn't 0, which would panic later,
2323
// when `NonZeroU32::new` (aka `Handle::new`) is called in `alloc`.
2424
assert_ne!(counter.load(Ordering::SeqCst), 0);
@@ -30,7 +30,7 @@ impl<T> OwnedStore<T> {
3030
impl<T> OwnedStore<T> {
3131
pub(super) fn alloc(&mut self, x: T) -> Handle {
3232
let counter = self.counter.fetch_add(1, Ordering::SeqCst);
33-
let handle = Handle::new(counter as u32).expect("`proc_macro` handle counter overflowed");
33+
let handle = Handle::new(counter).expect("`proc_macro` handle counter overflowed");
3434
assert!(self.data.insert(handle, x).is_none());
3535
handle
3636
}
@@ -60,7 +60,7 @@ pub(super) struct InternedStore<T: 'static> {
6060
}
6161

6262
impl<T: Copy + Eq + Hash> InternedStore<T> {
63-
pub(super) fn new(counter: &'static AtomicUsize) -> Self {
63+
pub(super) fn new(counter: &'static AtomicU32) -> Self {
6464
InternedStore { owned: OwnedStore::new(counter), interner: FxHashMap::default() }
6565
}
6666

library/proc_macro/src/bridge/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ use std::mem;
1616
use std::ops::Bound;
1717
use std::ops::Range;
1818
use std::panic;
19-
use std::sync::atomic::AtomicUsize;
2019
use std::sync::Once;
2120
use std::thread;
2221

0 commit comments

Comments
 (0)