From 13dd9aff64a9730574188b373d63dadc719f73b6 Mon Sep 17 00:00:00 2001 From: Vadim Petrochenkov Date: Sat, 21 Mar 2020 02:16:27 +0300 Subject: [PATCH] ast: Compress `AttrId` from `usize` to `u32` Also stop encoding/decoding it entirely --- src/librustc_ast/ast.rs | 22 ++++++++-------------- src/librustc_ast/attr/mod.rs | 8 ++++---- 2 files changed, 12 insertions(+), 18 deletions(-) diff --git a/src/librustc_ast/ast.rs b/src/librustc_ast/ast.rs index e3077b9897c18..b7b50617eaaf8 100644 --- a/src/librustc_ast/ast.rs +++ b/src/librustc_ast/ast.rs @@ -31,7 +31,6 @@ use crate::tokenstream::{DelimSpan, TokenStream, TokenTree}; use rustc_data_structures::stable_hasher::{HashStable, StableHasher}; use rustc_data_structures::sync::Lrc; use rustc_data_structures::thin_vec::ThinVec; -use rustc_index::vec::Idx; use rustc_macros::HashStable_Generic; use rustc_serialize::{self, Decoder, Encoder}; use rustc_span::source_map::{respan, Spanned}; @@ -2251,27 +2250,22 @@ pub enum AttrStyle { Inner, } -#[derive(Clone, PartialEq, Eq, Hash, Debug, PartialOrd, Ord, Copy)] -pub struct AttrId(pub usize); - -impl Idx for AttrId { - fn new(idx: usize) -> Self { - AttrId(idx) - } - fn index(self) -> usize { - self.0 +rustc_index::newtype_index! { + pub struct AttrId { + ENCODABLE = custom + DEBUG_FORMAT = "AttrId({})" } } impl rustc_serialize::Encodable for AttrId { - fn encode(&self, s: &mut S) -> Result<(), S::Error> { - s.emit_unit() + fn encode(&self, _: &mut S) -> Result<(), S::Error> { + Ok(()) } } impl rustc_serialize::Decodable for AttrId { - fn decode(d: &mut D) -> Result { - d.read_nil().map(|_| crate::attr::mk_attr_id()) + fn decode(_: &mut D) -> Result { + Ok(crate::attr::mk_attr_id()) } } diff --git a/src/librustc_ast/attr/mod.rs b/src/librustc_ast/attr/mod.rs index 249311851fb1b..d53d776778598 100644 --- a/src/librustc_ast/attr/mod.rs +++ b/src/librustc_ast/attr/mod.rs @@ -366,14 +366,14 @@ pub fn mk_nested_word_item(ident: Ident) -> NestedMetaItem { } crate fn mk_attr_id() -> AttrId { - use std::sync::atomic::AtomicUsize; + use std::sync::atomic::AtomicU32; use std::sync::atomic::Ordering; - static NEXT_ATTR_ID: AtomicUsize = AtomicUsize::new(0); + static NEXT_ATTR_ID: AtomicU32 = AtomicU32::new(0); let id = NEXT_ATTR_ID.fetch_add(1, Ordering::SeqCst); - assert!(id != ::std::usize::MAX); - AttrId(id) + assert!(id != u32::MAX); + AttrId::from_u32(id) } pub fn mk_attr(style: AttrStyle, path: Path, args: MacArgs, span: Span) -> Attribute {