Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Rc instead of Box for interned strings. #50549

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions src/libsyntax_pos/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use serialize::{Decodable, Decoder, Encodable, Encoder};
use std::fmt;
use std::cmp::{PartialEq, Ordering, PartialOrd, Ord};
use std::hash::{Hash, Hasher};
use std::rc::Rc;

#[derive(Copy, Clone, Eq)]
pub struct Ident {
Expand Down Expand Up @@ -200,11 +201,16 @@ impl<T: ::std::ops::Deref<Target=str>> PartialEq<T> for Symbol {

#[derive(Default)]
pub struct Interner {
names: FxHashMap<Box<str>, Symbol>,
strings: Vec<Box<str>>,
names: FxHashMap<Rc<str>, Symbol>,
strings: Vec<Rc<str>>,
gensyms: Vec<Symbol>,
}

// The impl is safe because the ref counts are only modified by one thread at a
// time, during insertion and during destruction. These may happen on different
// threads, but are mutually excluded.
unsafe impl Send for Interner {}

impl Interner {
pub fn new() -> Self {
Interner::default()
Expand All @@ -224,7 +230,7 @@ impl Interner {
}

let name = Symbol(self.strings.len() as u32);
let string = string.to_string().into_boxed_str();
let string: Rc<str> = Rc::from(string);
self.strings.push(string.clone());
self.names.insert(string, name);
name
Expand Down