Skip to content

Commit

Permalink
Rollup merge of rust-lang#120527 - GnomedDev:atomicu32-handle, r=petr…
Browse files Browse the repository at this point in the history
…ochenkov

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.
  • Loading branch information
matthiaskrgr authored Feb 5, 2024
2 parents 9b8f1a9 + 3cc601a commit d6ea81b
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 10 deletions.
9 changes: 5 additions & 4 deletions library/proc_macro/src/bridge/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use super::*;

use std::marker::PhantomData;
use std::sync::atomic::AtomicU32;

macro_rules! define_handles {
(
Expand All @@ -12,17 +13,17 @@ macro_rules! define_handles {
#[repr(C)]
#[allow(non_snake_case)]
pub struct HandleCounters {
$($oty: AtomicUsize,)*
$($ity: AtomicUsize,)*
$($oty: AtomicU32,)*
$($ity: AtomicU32,)*
}

impl HandleCounters {
// FIXME(eddyb) use a reference to the `static COUNTERS`, instead of
// 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
}
Expand Down
10 changes: 5 additions & 5 deletions library/proc_macro/src/bridge/handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::collections::BTreeMap;
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;

Expand All @@ -13,12 +13,12 @@ pub(super) type Handle = NonZeroU32;
/// 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);
Expand All @@ -30,7 +30,7 @@ impl<T> OwnedStore<T> {
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
}
Expand Down Expand Up @@ -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() }
}

Expand Down
1 change: 0 additions & 1 deletion library/proc_macro/src/bridge/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ use std::mem;
use std::ops::Bound;
use std::ops::Range;
use std::panic;
use std::sync::atomic::AtomicUsize;
use std::sync::Once;
use std::thread;

Expand Down

0 comments on commit d6ea81b

Please sign in to comment.