Skip to content

Commit

Permalink
Rollup merge of #70215 - petrochenkov:attrid, r=Centril
Browse files Browse the repository at this point in the history
ast: Compress `AttrId` from `usize` to `u32`

An easy size win for `ast::Attribute` (96 bytes -> 88 bytes).

Also stop encoding/decoding `AttrId` entirely.
  • Loading branch information
Dylan-DPC authored Mar 21, 2020
2 parents a3bdfc4 + 13dd9af commit 8e90533
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 18 deletions.
22 changes: 8 additions & 14 deletions src/librustc_ast/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
s.emit_unit()
fn encode<S: Encoder>(&self, _: &mut S) -> Result<(), S::Error> {
Ok(())
}
}

impl rustc_serialize::Decodable for AttrId {
fn decode<D: Decoder>(d: &mut D) -> Result<AttrId, D::Error> {
d.read_nil().map(|_| crate::attr::mk_attr_id())
fn decode<D: Decoder>(_: &mut D) -> Result<AttrId, D::Error> {
Ok(crate::attr::mk_attr_id())
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/librustc_ast/attr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 8e90533

Please sign in to comment.