Skip to content

Use a hash-table when selecting impls #24965

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

Merged
merged 3 commits into from
May 1, 2015
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion src/librustc/metadata/csearch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ pub fn get_impl_polarity<'tcx>(tcx: &ty::ctxt<'tcx>,
// if there is one.
pub fn get_impl_trait<'tcx>(tcx: &ty::ctxt<'tcx>,
def: ast::DefId)
-> Option<Rc<ty::TraitRef<'tcx>>> {
-> Option<ty::TraitRef<'tcx>> {
let cstore = &tcx.sess.cstore;
let cdata = cstore.get_crate_data(def.krate);
decoder::get_impl_trait(&*cdata, def.node, tcx)
Expand Down
11 changes: 8 additions & 3 deletions src/librustc/metadata/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ use middle::subst;
use middle::ty::{ImplContainer, TraitContainer};
use middle::ty::{self, Ty};
use middle::astencode::vtable_decoder_helpers;
use util::nodemap::FnvHashMap;

use std::cell::{Cell, RefCell};
use std::collections::HashMap;
use std::hash::{self, Hash, SipHasher};
use std::io::prelude::*;
Expand Down Expand Up @@ -247,13 +249,13 @@ pub fn item_type<'tcx>(_item_id: ast::DefId, item: rbml::Doc,
}

fn doc_trait_ref<'tcx>(doc: rbml::Doc, tcx: &ty::ctxt<'tcx>, cdata: Cmd)
-> Rc<ty::TraitRef<'tcx>> {
-> ty::TraitRef<'tcx> {
parse_trait_ref_data(doc.data, cdata.cnum, doc.start, tcx,
|_, did| translate_def_id(cdata, did))
}

fn item_trait_ref<'tcx>(doc: rbml::Doc, tcx: &ty::ctxt<'tcx>, cdata: Cmd)
-> Rc<ty::TraitRef<'tcx>> {
-> ty::TraitRef<'tcx> {
let tp = reader::get_doc(doc, tag_item_trait_ref);
doc_trait_ref(tp, tcx, cdata)
}
Expand Down Expand Up @@ -420,6 +422,9 @@ pub fn get_trait_def<'tcx>(cdata: Cmd,
generics: generics,
trait_ref: item_trait_ref(item_doc, tcx, cdata),
associated_type_names: associated_type_names,
nonblanket_impls: RefCell::new(FnvHashMap()),
blanket_impls: RefCell::new(vec![]),
flags: Cell::new(ty::TraitFlags::NO_TRAIT_FLAGS)
}
}

Expand Down Expand Up @@ -490,7 +495,7 @@ pub fn get_impl_polarity<'tcx>(cdata: Cmd,
pub fn get_impl_trait<'tcx>(cdata: Cmd,
id: ast::NodeId,
tcx: &ty::ctxt<'tcx>)
-> Option<Rc<ty::TraitRef<'tcx>>>
-> Option<ty::TraitRef<'tcx>>
{
let item_doc = lookup_item(id, cdata.data());
let fam = item_family(item_doc);
Expand Down
28 changes: 13 additions & 15 deletions src/librustc/metadata/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ struct entry<T> {

fn encode_trait_ref<'a, 'tcx>(rbml_w: &mut Encoder,
ecx: &EncodeContext<'a, 'tcx>,
trait_ref: &ty::TraitRef<'tcx>,
trait_ref: ty::TraitRef<'tcx>,
tag: usize) {
let ty_str_ctxt = &tyencode::ctxt {
diag: ecx.diag,
Expand Down Expand Up @@ -191,7 +191,7 @@ pub fn write_trait_ref<'a, 'tcx>(ecx: &EncodeContext<'a, 'tcx>,
tcx: ecx.tcx,
abbrevs: &ecx.type_abbrevs
};
tyencode::enc_trait_ref(rbml_w, ty_str_ctxt, trait_ref);
tyencode::enc_trait_ref(rbml_w, ty_str_ctxt, *trait_ref);
}

pub fn write_region(ecx: &EncodeContext,
Expand Down Expand Up @@ -974,16 +974,14 @@ fn encode_inherent_implementations(ecx: &EncodeContext,
fn encode_extension_implementations(ecx: &EncodeContext,
rbml_w: &mut Encoder,
trait_def_id: DefId) {
match ecx.tcx.trait_impls.borrow().get(&trait_def_id) {
None => {}
Some(implementations) => {
for &impl_def_id in &*implementations.borrow() {
rbml_w.start_tag(tag_items_data_item_extension_impl);
encode_def_id(rbml_w, impl_def_id);
rbml_w.end_tag();
}
}
}
assert!(ast_util::is_local(trait_def_id));
let def = ty::lookup_trait_def(ecx.tcx, trait_def_id);

def.for_each_impl(ecx.tcx, |impl_def_id| {
rbml_w.start_tag(tag_items_data_item_extension_impl);
encode_def_id(rbml_w, impl_def_id);
rbml_w.end_tag();
});
}

fn encode_stability(rbml_w: &mut Encoder, stab_opt: Option<attr::Stability>) {
Expand Down Expand Up @@ -1201,7 +1199,7 @@ fn encode_info_for_item(ecx: &EncodeContext,
encode_unsafety(rbml_w, unsafety);

let trait_ref = ty::impl_id_to_trait_ref(tcx, item.id);
encode_trait_ref(rbml_w, ecx, &*trait_ref, tag_item_trait_ref);
encode_trait_ref(rbml_w, ecx, trait_ref, tag_item_trait_ref);
rbml_w.end_tag();
}
ast::ItemImpl(unsafety, polarity, _, ref opt_trait, ref ty, ref ast_items) => {
Expand Down Expand Up @@ -1246,7 +1244,7 @@ fn encode_info_for_item(ecx: &EncodeContext,
}
if opt_trait.is_some() {
let trait_ref = ty::impl_id_to_trait_ref(tcx, item.id);
encode_trait_ref(rbml_w, ecx, &*trait_ref, tag_item_trait_ref);
encode_trait_ref(rbml_w, ecx, trait_ref, tag_item_trait_ref);
}
encode_path(rbml_w, path.clone());
encode_stability(rbml_w, stab);
Expand Down Expand Up @@ -1314,7 +1312,7 @@ fn encode_info_for_item(ecx: &EncodeContext,
tag_item_generics);
encode_predicates(rbml_w, ecx, &ty::lookup_super_predicates(tcx, def_id),
tag_item_super_predicates);
encode_trait_ref(rbml_w, ecx, &*trait_def.trait_ref, tag_item_trait_ref);
encode_trait_ref(rbml_w, ecx, trait_def.trait_ref, tag_item_trait_ref);
encode_name(rbml_w, item.ident.name);
encode_attributes(rbml_w, &item.attrs);
encode_visibility(rbml_w, vis);
Expand Down
9 changes: 4 additions & 5 deletions src/librustc/metadata/tydecode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ use middle::subst;
use middle::subst::VecPerParamSpace;
use middle::ty::{self, AsPredicate, Ty};

use std::rc::Rc;
use std::str;
use syntax::abi;
use syntax::ast;
Expand Down Expand Up @@ -182,7 +181,7 @@ pub fn parse_bare_fn_ty_data<'tcx, F>(data: &[u8], crate_num: ast::CrateNum, pos

pub fn parse_trait_ref_data<'tcx, F>(data: &[u8], crate_num: ast::CrateNum, pos: usize,
tcx: &ty::ctxt<'tcx>, conv: F)
-> Rc<ty::TraitRef<'tcx>> where
-> ty::TraitRef<'tcx> where
F: FnMut(DefIdSource, ast::DefId) -> ast::DefId,
{
debug!("parse_trait_ref_data {}", data_log_string(data, pos));
Expand Down Expand Up @@ -434,19 +433,19 @@ fn parse_str(st: &mut PState, term: char) -> String {
}

fn parse_trait_ref<'a, 'tcx, F>(st: &mut PState<'a, 'tcx>, mut conv: F)
-> Rc<ty::TraitRef<'tcx>> where
-> ty::TraitRef<'tcx> where
F: FnMut(DefIdSource, ast::DefId) -> ast::DefId,
{
parse_trait_ref_(st, &mut conv)
}

fn parse_trait_ref_<'a, 'tcx, F>(st: &mut PState<'a, 'tcx>, conv: &mut F)
-> Rc<ty::TraitRef<'tcx>> where
-> ty::TraitRef<'tcx> where
F: FnMut(DefIdSource, ast::DefId) -> ast::DefId,
{
let def = parse_def_(st, NominalType, conv);
let substs = st.tcx.mk_substs(parse_substs_(st, conv));
Rc::new(ty::TraitRef {def_id: def, substs: substs})
ty::TraitRef {def_id: def, substs: substs}
}

fn parse_ty<'a, 'tcx, F>(st: &mut PState<'a, 'tcx>, mut conv: F) -> Ty<'tcx> where
Expand Down
12 changes: 6 additions & 6 deletions src/librustc/metadata/tyencode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ pub fn enc_ty<'a, 'tcx>(w: &mut Encoder, cx: &ctxt<'a, 'tcx>, t: Ty<'tcx>) {
ty::ty_trait(box ty::TyTrait { ref principal,
ref bounds }) => {
mywrite!(w, "x[");
enc_trait_ref(w, cx, &*principal.0);
enc_trait_ref(w, cx, principal.0);
enc_existential_bounds(w, cx, bounds);
mywrite!(w, "]");
}
Expand Down Expand Up @@ -149,7 +149,7 @@ pub fn enc_ty<'a, 'tcx>(w: &mut Encoder, cx: &ctxt<'a, 'tcx>, t: Ty<'tcx>) {
}
ty::ty_projection(ref data) => {
mywrite!(w, "P[");
enc_trait_ref(w, cx, &*data.trait_ref);
enc_trait_ref(w, cx, data.trait_ref);
mywrite!(w, "{}]", token::get_name(data.item_name));
}
ty::ty_err => {
Expand Down Expand Up @@ -309,7 +309,7 @@ fn enc_bound_region(w: &mut Encoder, cx: &ctxt, br: ty::BoundRegion) {
}

pub fn enc_trait_ref<'a, 'tcx>(w: &mut Encoder, cx: &ctxt<'a, 'tcx>,
s: &ty::TraitRef<'tcx>) {
s: ty::TraitRef<'tcx>) {
mywrite!(w, "{}|", (cx.ds)(s.def_id));
enc_substs(w, cx, s.substs);
}
Expand Down Expand Up @@ -394,7 +394,7 @@ pub fn enc_bounds<'a, 'tcx>(w: &mut Encoder, cx: &ctxt<'a, 'tcx>,

for tp in &bs.trait_bounds {
mywrite!(w, "I");
enc_trait_ref(w, cx, &*tp.0);
enc_trait_ref(w, cx, tp.0);
}

for tp in &bs.projection_bounds {
Expand Down Expand Up @@ -446,7 +446,7 @@ pub fn enc_predicate<'a, 'tcx>(w: &mut Encoder,
match *p {
ty::Predicate::Trait(ref trait_ref) => {
mywrite!(w, "t");
enc_trait_ref(w, cx, &*trait_ref.0.trait_ref);
enc_trait_ref(w, cx, trait_ref.0.trait_ref);
}
ty::Predicate::Equate(ty::Binder(ty::EquatePredicate(a, b))) => {
mywrite!(w, "e");
Expand All @@ -473,7 +473,7 @@ pub fn enc_predicate<'a, 'tcx>(w: &mut Encoder,
fn enc_projection_predicate<'a, 'tcx>(w: &mut Encoder,
cx: &ctxt<'a, 'tcx>,
data: &ty::ProjectionPredicate<'tcx>) {
enc_trait_ref(w, cx, &*data.projection_ty.trait_ref);
enc_trait_ref(w, cx, data.projection_ty.trait_ref);
mywrite!(w, "{}|", token::get_name(data.projection_ty.item_name));
enc_ty(w, cx, data.ty);
}
11 changes: 5 additions & 6 deletions src/librustc/middle/astencode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ use syntax;
use std::cell::Cell;
use std::io::SeekFrom;
use std::io::prelude::*;
use std::rc::Rc;
use std::fmt::Debug;

use rbml::reader;
Expand Down Expand Up @@ -890,7 +889,7 @@ impl<'a, 'tcx> rbml_writer_helpers<'tcx> for Encoder<'a> {
this.emit_enum_variant("MethodTypeParam", 2, 1, |this| {
this.emit_struct("MethodParam", 2, |this| {
try!(this.emit_struct_field("trait_ref", 0, |this| {
Ok(this.emit_trait_ref(ecx, &*p.trait_ref))
Ok(this.emit_trait_ref(ecx, &p.trait_ref))
}));
try!(this.emit_struct_field("method_num", 0, |this| {
this.emit_uint(p.method_num)
Expand All @@ -914,7 +913,7 @@ impl<'a, 'tcx> rbml_writer_helpers<'tcx> for Encoder<'a> {
this.emit_enum_variant("MethodTraitObject", 3, 1, |this| {
this.emit_struct("MethodObject", 2, |this| {
try!(this.emit_struct_field("trait_ref", 0, |this| {
Ok(this.emit_trait_ref(ecx, &*o.trait_ref))
Ok(this.emit_trait_ref(ecx, &o.trait_ref))
}));
try!(this.emit_struct_field("object_trait_id", 0, |this| {
Ok(this.emit_def_id(o.object_trait_id))
Expand Down Expand Up @@ -1208,7 +1207,7 @@ fn encode_side_tables_for_id(ecx: &e::EncodeContext,
if let Some(trait_ref) = tcx.object_cast_map.borrow().get(&id) {
rbml_w.tag(c::tag_table_object_cast_map, |rbml_w| {
rbml_w.id(id);
rbml_w.emit_trait_ref(ecx, &*trait_ref.0);
rbml_w.emit_trait_ref(ecx, &trait_ref.0);
})
}

Expand Down Expand Up @@ -1275,7 +1274,7 @@ trait rbml_decoder_decoder_helpers<'tcx> {
fn read_ty<'a, 'b>(&mut self, dcx: &DecodeContext<'a, 'b, 'tcx>) -> Ty<'tcx>;
fn read_tys<'a, 'b>(&mut self, dcx: &DecodeContext<'a, 'b, 'tcx>) -> Vec<Ty<'tcx>>;
fn read_trait_ref<'a, 'b>(&mut self, dcx: &DecodeContext<'a, 'b, 'tcx>)
-> Rc<ty::TraitRef<'tcx>>;
-> ty::TraitRef<'tcx>;
fn read_poly_trait_ref<'a, 'b>(&mut self, dcx: &DecodeContext<'a, 'b, 'tcx>)
-> ty::PolyTraitRef<'tcx>;
fn read_type_param_def<'a, 'b>(&mut self, dcx: &DecodeContext<'a, 'b, 'tcx>)
Expand Down Expand Up @@ -1469,7 +1468,7 @@ impl<'a, 'tcx> rbml_decoder_decoder_helpers<'tcx> for reader::Decoder<'a> {
}

fn read_trait_ref<'b, 'c>(&mut self, dcx: &DecodeContext<'b, 'c, 'tcx>)
-> Rc<ty::TraitRef<'tcx>> {
-> ty::TraitRef<'tcx> {
self.read_opaque(|this, doc| {
let ty = tydecode::parse_trait_ref_data(
doc.data,
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/middle/const_eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -942,8 +942,8 @@ fn resolve_trait_associated_const<'a, 'tcx: 'a>(tcx: &'a ty::ctxt<'tcx>,
let trait_substs = tcx.mk_substs(trait_substs);
debug!("resolve_trait_associated_const: trait_substs={}",
trait_substs.repr(tcx));
let trait_ref = ty::Binder(Rc::new(ty::TraitRef { def_id: trait_id,
substs: trait_substs }));
let trait_ref = ty::Binder(ty::TraitRef { def_id: trait_id,
substs: trait_substs });

ty::populate_implementations_for_trait_if_necessary(tcx, trait_ref.def_id());
let infcx = infer::new_infer_ctxt(tcx);
Expand Down
5 changes: 2 additions & 3 deletions src/librustc/middle/implicator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ use middle::traits;
use middle::ty::{self, RegionEscape, ToPolyTraitRef, Ty};
use middle::ty_fold::{TypeFoldable, TypeFolder};

use std::rc::Rc;
use syntax::ast;
use syntax::codemap::Span;

Expand Down Expand Up @@ -324,7 +323,7 @@ impl<'a, 'tcx> Implicator<'a, 'tcx> {
}

fn accumulate_from_assoc_types(&mut self,
trait_ref: Rc<ty::TraitRef<'tcx>>)
trait_ref: ty::TraitRef<'tcx>)
{
debug!("accumulate_from_assoc_types({})",
trait_ref.repr(self.tcx()));
Expand Down Expand Up @@ -442,7 +441,7 @@ pub fn object_region_bounds<'tcx>(
// Note that we preserve the overall binding levels here.
assert!(!open_ty.has_escaping_regions());
let substs = tcx.mk_substs(principal.0.substs.with_self_ty(open_ty));
let trait_refs = vec!(ty::Binder(Rc::new(ty::TraitRef::new(principal.0.def_id, substs))));
let trait_refs = vec!(ty::Binder(ty::TraitRef::new(principal.0.def_id, substs)));

let param_bounds = ty::ParamBounds {
region_bounds: Vec::new(),
Expand Down
11 changes: 5 additions & 6 deletions src/librustc/middle/infer/error_reporting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ use middle::ty::{self, Ty};
use middle::ty::{Region, ReFree};
use std::cell::{Cell, RefCell};
use std::char::from_u32;
use std::rc::Rc;
use std::string::String;
use syntax::ast;
use syntax::ast_map;
Expand Down Expand Up @@ -1680,13 +1679,13 @@ impl<'tcx> Resolvable<'tcx> for Ty<'tcx> {
}
}

impl<'tcx> Resolvable<'tcx> for Rc<ty::TraitRef<'tcx>> {
impl<'tcx> Resolvable<'tcx> for ty::TraitRef<'tcx> {
fn resolve<'a>(&self, infcx: &InferCtxt<'a, 'tcx>)
-> Rc<ty::TraitRef<'tcx>> {
Rc::new(infcx.resolve_type_vars_if_possible(&**self))
-> ty::TraitRef<'tcx> {
infcx.resolve_type_vars_if_possible(self)
}
fn contains_error(&self) -> bool {
ty::trait_ref_contains_error(&**self)
ty::trait_ref_contains_error(self)
}
}

Expand All @@ -1699,7 +1698,7 @@ impl<'tcx> Resolvable<'tcx> for ty::PolyTraitRef<'tcx> {
}

fn contains_error(&self) -> bool {
ty::trait_ref_contains_error(&*self.0)
ty::trait_ref_contains_error(&self.0)
}
}

Expand Down
13 changes: 6 additions & 7 deletions src/librustc/middle/infer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ use middle::ty_relate::{Relate, RelateResult, TypeRelation};
use rustc_data_structures::unify::{self, UnificationTable};
use std::cell::{RefCell};
use std::fmt;
use std::rc::Rc;
use syntax::ast;
use syntax::codemap;
use syntax::codemap::Span;
Expand Down Expand Up @@ -155,7 +154,7 @@ impl fmt::Display for TypeOrigin {
#[derive(Clone, Debug)]
pub enum ValuePairs<'tcx> {
Types(ty::expected_found<Ty<'tcx>>),
TraitRefs(ty::expected_found<Rc<ty::TraitRef<'tcx>>>),
TraitRefs(ty::expected_found<ty::TraitRef<'tcx>>),
PolyTraitRefs(ty::expected_found<ty::PolyTraitRef<'tcx>>),
}

Expand Down Expand Up @@ -663,8 +662,8 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
pub fn sub_trait_refs(&self,
a_is_expected: bool,
origin: TypeOrigin,
a: Rc<ty::TraitRef<'tcx>>,
b: Rc<ty::TraitRef<'tcx>>)
a: ty::TraitRef<'tcx>,
b: ty::TraitRef<'tcx>)
-> UnitResult<'tcx>
{
debug!("sub_trait_refs({} <: {})",
Expand All @@ -675,7 +674,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
origin: origin,
values: TraitRefs(expected_found(a_is_expected, a.clone(), b.clone()))
};
self.sub(a_is_expected, trace).relate(&*a, &*b).map(|_| ())
self.sub(a_is_expected, trace).relate(&a, &b).map(|_| ())
})
}

Expand Down Expand Up @@ -873,8 +872,8 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
format!("({})", tstrs.connect(", "))
}

pub fn trait_ref_to_string(&self, t: &Rc<ty::TraitRef<'tcx>>) -> String {
let t = self.resolve_type_vars_if_possible(&**t);
pub fn trait_ref_to_string(&self, t: &ty::TraitRef<'tcx>) -> String {
let t = self.resolve_type_vars_if_possible(t);
t.user_string(self.tcx)
}

Expand Down
Loading