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.
This commit is contained in:
bors 2024-02-07 11:57:50 +00:00
commit 0809f78c19
3 changed files with 10 additions and 10 deletions

View file

@ -3,6 +3,7 @@
use super::*;
use std::marker::PhantomData;
use std::sync::atomic::AtomicU32;
macro_rules! define_handles {
(
@ -12,8 +13,8 @@ macro_rules! define_handles {
#[repr(C)]
#[allow(non_snake_case)]
pub struct HandleCounters {
$($oty: AtomicUsize,)*
$($ity: AtomicUsize,)*
$($oty: AtomicU32,)*
$($ity: AtomicU32,)*
}
impl HandleCounters {
@ -21,8 +22,8 @@ impl HandleCounters {
// a wrapper `fn` pointer, once `const fn` can reference `static`s.
extern "C" fn get() -> &'static Self {
static COUNTERS: HandleCounters = HandleCounters {
$($oty: AtomicUsize::new(1),)*
$($ity: AtomicUsize::new(1),)*
$($oty: AtomicU32::new(1),)*
$($ity: AtomicU32::new(1),)*
};
&COUNTERS
}

View file

@ -4,7 +4,7 @@
use std::hash::Hash;
use std::num::NonZeroU32;
use std::ops::{Index, IndexMut};
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::atomic::{AtomicU32, Ordering};
use super::fxhash::FxHashMap;
@ -13,12 +13,12 @@
/// A store that associates values of type `T` with numeric handles. A value can
/// be looked up using its handle.
pub(super) struct OwnedStore<T: 'static> {
counter: &'static AtomicUsize,
counter: &'static AtomicU32,
data: BTreeMap<Handle, T>,
}
impl<T> OwnedStore<T> {
pub(super) fn new(counter: &'static AtomicUsize) -> Self {
pub(super) fn new(counter: &'static AtomicU32) -> Self {
// Ensure the handle counter isn't 0, which would panic later,
// when `NonZeroU32::new` (aka `Handle::new`) is called in `alloc`.
assert_ne!(counter.load(Ordering::SeqCst), 0);
@ -30,7 +30,7 @@ pub(super) fn new(counter: &'static AtomicUsize) -> Self {
impl<T> OwnedStore<T> {
pub(super) fn alloc(&mut self, x: T) -> Handle {
let counter = self.counter.fetch_add(1, Ordering::SeqCst);
let handle = Handle::new(counter as u32).expect("`proc_macro` handle counter overflowed");
let handle = Handle::new(counter).expect("`proc_macro` handle counter overflowed");
assert!(self.data.insert(handle, x).is_none());
handle
}
@ -60,7 +60,7 @@ pub(super) struct InternedStore<T: 'static> {
}
impl<T: Copy + Eq + Hash> InternedStore<T> {
pub(super) fn new(counter: &'static AtomicUsize) -> Self {
pub(super) fn new(counter: &'static AtomicU32) -> Self {
InternedStore { owned: OwnedStore::new(counter), interner: FxHashMap::default() }
}

View file

@ -16,7 +16,6 @@
use std::ops::Bound;
use std::ops::Range;
use std::panic;
use std::sync::atomic::AtomicUsize;
use std::sync::Once;
use std::thread;