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

Start cleaning up the string interner #34772

Merged
merged 6 commits into from Jul 13, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/librustc_driver/driver.rs
Expand Up @@ -211,7 +211,7 @@ pub fn compile_input(sess: &Session,
}

// Discard interned strings as they are no longer required.
token::get_ident_interner().clear();
token::clear_ident_interner();

Ok((outputs, trans))
})??
Expand Down Expand Up @@ -480,7 +480,7 @@ pub fn phase_1_parse_input<'a>(sess: &'a Session,
input: &Input)
-> PResult<'a, ast::Crate> {
// These may be left in an incoherent state after a previous compile.
// `clear_tables` and `get_ident_interner().clear()` can be used to free
// `clear_tables` and `clear_ident_interner` can be used to free
// memory, but they do not restore the initial state.
syntax::ext::mtwt::reset_tables();
token::reset_ident_interner();
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_metadata/decoder.rs
Expand Up @@ -442,7 +442,7 @@ pub fn get_adt_def<'a, 'tcx>(cdata: Cmd,
struct_field_family_to_visibility(ff))
}).chain(reader::tagged_docs(doc, tag_item_unnamed_field).map(|f| {
let ff = item_family(f);
let name = token::get_ident_interner().intern(index.to_string());
let name = token::with_ident_interner(|interner| interner.intern(index.to_string()));
index += 1;
ty::FieldDefData::new(item_def_id(f, cdata), name,
struct_field_family_to_visibility(ff))
Expand Down Expand Up @@ -1147,7 +1147,7 @@ pub fn get_struct_field_names(cdata: Cmd, id: DefIndex) -> Vec<ast::Name> {
reader::tagged_docs(item, tag_item_field).map(|an_item| {
item_name(an_item)
}).chain(reader::tagged_docs(item, tag_item_unnamed_field).map(|_| {
let name = token::get_ident_interner().intern(index.to_string());
let name = token::with_ident_interner(|interner| interner.intern(index.to_string()));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is calling with_ident_interner even necessary here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, this is equivalent to token::intern(&index.to_string()) (except that it avoids a clone).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe token::intern should be as generic as Interner::intern?
Eventually this could be a generic Symbol::from, I guess.
I would like it if with_ident_interner was actually private.

Copy link
Contributor Author

@jseyfried jseyfried Jul 11, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I'd like token::intern to be generic but it's a syntax-[breaking-change] since token::intern(&my_string) where my_string: String wouldn't typecheck (intern(my_string) or intern(&*my_string) would, though).

I was planning on making token::intern generic in a future PR with other interner-related syntax-[breaking-change]s.

I agree that with_ident_interner should eventually be private and that token::intern should eventually be Symbol::from (or Symbol::intern to contrast with Symbol::gensym).

index += 1;
name
})).collect()
Expand Down
5 changes: 2 additions & 3 deletions src/librustc_trans/trans_item.rs
Expand Up @@ -31,7 +31,6 @@ use rustc::ty::subst;
use std::hash::{Hash, Hasher};
use syntax::ast::{self, NodeId};
use syntax::{attr,errors};
use syntax::parse::token;
use type_of;
use glue;
use abi::{Abi, FnType};
Expand Down Expand Up @@ -562,8 +561,8 @@ fn push_type_params<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,

for projection in projections {
let projection = projection.skip_binder();
let name = token::get_ident_interner().get(projection.projection_ty.item_name);
output.push_str(&name[..]);
let name = &projection.projection_ty.item_name.as_str();
output.push_str(name);
output.push_str("=");
push_unique_type_name(tcx, projection.ty, output);
output.push_str(", ");
Expand Down
25 changes: 13 additions & 12 deletions src/libsyntax/parse/token.rs
Expand Up @@ -477,17 +477,20 @@ pub type IdentInterner = Interner;
// if an interner exists in TLS, return it. Otherwise, prepare a
// fresh one.
// FIXME(eddyb) #8726 This should probably use a thread-local reference.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this looks obsolete

pub fn get_ident_interner() -> Rc<IdentInterner> {
thread_local!(static KEY: Rc<::parse::token::IdentInterner> = {
Rc::new(mk_fresh_ident_interner())
pub fn with_ident_interner<T, F: FnOnce(&IdentInterner) -> T>(f: F) -> T {
thread_local!(static KEY: IdentInterner = {
mk_fresh_ident_interner()
});
KEY.with(|k| k.clone())
KEY.with(f)
}

/// Reset the ident interner to its initial state.
pub fn reset_ident_interner() {
let interner = get_ident_interner();
interner.reset(mk_fresh_ident_interner());
with_ident_interner(|interner| interner.reset(mk_fresh_ident_interner()));
}

pub fn clear_ident_interner() {
with_ident_interner(|interner| interner.clear());
}

/// Represents a string stored in the thread-local interner. Because the
Expand Down Expand Up @@ -521,8 +524,7 @@ impl InternedString {

#[inline]
pub fn new_from_name(name: ast::Name) -> InternedString {
let interner = get_ident_interner();
InternedString::new_from_rc_str(interner.get(name))
with_ident_interner(|interner| InternedString::new_from_rc_str(interner.get(name)))
}
}

Expand Down Expand Up @@ -610,13 +612,13 @@ pub fn intern_and_get_ident(s: &str) -> InternedString {
/// Maps a string to its interned representation.
#[inline]
pub fn intern(s: &str) -> ast::Name {
get_ident_interner().intern(s)
with_ident_interner(|interner| interner.intern(s))
}

/// gensym's a new usize, using the current interner.
#[inline]
pub fn gensym(s: &str) -> ast::Name {
get_ident_interner().gensym(s)
with_ident_interner(|interner| interner.gensym(s))
}

/// Maps a string to an identifier with an empty syntax context.
Expand All @@ -635,8 +637,7 @@ pub fn gensym_ident(s: &str) -> ast::Ident {
// note that this guarantees that str_ptr_eq(ident_to_string(src),interner_get(fresh_name(src)));
// that is, that the new name and the old one are connected to ptr_eq strings.
pub fn fresh_name(src: ast::Ident) -> ast::Name {
let interner = get_ident_interner();
interner.gensym_copy(src.name)
with_ident_interner(|interner| interner.gensym_copy(src.name))
// following: debug version. Could work in final except that it's incompatible with
// good error messages and uses of struct names in ambiguous could-be-binding
// locations. Also definitely destroys the guarantee given above about ptr_eq.
Expand Down