Skip to content

Commit

Permalink
Use FnvHashMap in more places
Browse files Browse the repository at this point in the history
* A step towards #34902
* More stable error messages in some places related to crate loading
* Possible slight performance improvements since all `HashMap`s
  replaced had small keys where `FnvHashMap` should be faster
  (although I didn't measure)
  • Loading branch information
jonas-schievink committed Aug 22, 2016
1 parent 3c5a0fa commit c95236f
Show file tree
Hide file tree
Showing 14 changed files with 77 additions and 81 deletions.
7 changes: 4 additions & 3 deletions src/librustc/infer/freshen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,16 @@

use ty::{self, Ty, TyCtxt, TypeFoldable};
use ty::fold::TypeFolder;
use std::collections::hash_map::{self, Entry};
use util::nodemap::FnvHashMap;
use std::collections::hash_map::Entry;

use super::InferCtxt;
use super::unify_key::ToType;

pub struct TypeFreshener<'a, 'gcx: 'a+'tcx, 'tcx: 'a> {
infcx: &'a InferCtxt<'a, 'gcx, 'tcx>,
freshen_count: u32,
freshen_map: hash_map::HashMap<ty::InferTy, Ty<'tcx>>,
freshen_map: FnvHashMap<ty::InferTy, Ty<'tcx>>,
}

impl<'a, 'gcx, 'tcx> TypeFreshener<'a, 'gcx, 'tcx> {
Expand All @@ -49,7 +50,7 @@ impl<'a, 'gcx, 'tcx> TypeFreshener<'a, 'gcx, 'tcx> {
TypeFreshener {
infcx: infcx,
freshen_count: 0,
freshen_map: hash_map::HashMap::new(),
freshen_map: FnvHashMap(),
}
}

Expand Down
12 changes: 6 additions & 6 deletions src/librustc/middle/dead.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ use ty::{self, TyCtxt};
use hir::def::Def;
use hir::def_id::{DefId};
use lint;
use util::nodemap::FnvHashSet;

use std::collections::HashSet;
use syntax::{ast, codemap};
use syntax::attr;
use syntax_pos;
Expand All @@ -48,7 +48,7 @@ fn should_explore<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
struct MarkSymbolVisitor<'a, 'tcx: 'a> {
worklist: Vec<ast::NodeId>,
tcx: TyCtxt<'a, 'tcx, 'tcx>,
live_symbols: Box<HashSet<ast::NodeId>>,
live_symbols: Box<FnvHashSet<ast::NodeId>>,
struct_has_extern_repr: bool,
ignore_non_const_paths: bool,
inherited_pub_visibility: bool,
Expand All @@ -61,7 +61,7 @@ impl<'a, 'tcx> MarkSymbolVisitor<'a, 'tcx> {
MarkSymbolVisitor {
worklist: worklist,
tcx: tcx,
live_symbols: box HashSet::new(),
live_symbols: box FnvHashSet(),
struct_has_extern_repr: false,
ignore_non_const_paths: false,
inherited_pub_visibility: false,
Expand Down Expand Up @@ -162,7 +162,7 @@ impl<'a, 'tcx> MarkSymbolVisitor<'a, 'tcx> {
}

fn mark_live_symbols(&mut self) {
let mut scanned = HashSet::new();
let mut scanned = FnvHashSet();
while !self.worklist.is_empty() {
let id = self.worklist.pop().unwrap();
if scanned.contains(&id) {
Expand Down Expand Up @@ -395,7 +395,7 @@ fn create_and_seed_worklist<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
fn find_live<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
access_levels: &privacy::AccessLevels,
krate: &hir::Crate)
-> Box<HashSet<ast::NodeId>> {
-> Box<FnvHashSet<ast::NodeId>> {
let worklist = create_and_seed_worklist(tcx, access_levels, krate);
let mut symbol_visitor = MarkSymbolVisitor::new(tcx, worklist);
symbol_visitor.mark_live_symbols();
Expand All @@ -413,7 +413,7 @@ fn get_struct_ctor_id(item: &hir::Item) -> Option<ast::NodeId> {

struct DeadVisitor<'a, 'tcx: 'a> {
tcx: TyCtxt<'a, 'tcx, 'tcx>,
live_symbols: Box<HashSet<ast::NodeId>>,
live_symbols: Box<FnvHashSet<ast::NodeId>>,
}

impl<'a, 'tcx> DeadVisitor<'a, 'tcx> {
Expand Down
5 changes: 2 additions & 3 deletions src/librustc/middle/reachable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@ use hir::def_id::DefId;
use ty::{self, TyCtxt};
use middle::privacy;
use session::config;
use util::nodemap::NodeSet;
use util::nodemap::{NodeSet, FnvHashSet};

use std::collections::HashSet;
use syntax::abi::Abi;
use syntax::ast;
use syntax::attr;
Expand Down Expand Up @@ -204,7 +203,7 @@ impl<'a, 'tcx> ReachableContext<'a, 'tcx> {

// Step 2: Mark all symbols that the symbols on the worklist touch.
fn propagate(&mut self) {
let mut scanned = HashSet::new();
let mut scanned = FnvHashSet();
loop {
let search_item = match self.worklist.pop() {
Some(item) => item,
Expand Down
14 changes: 7 additions & 7 deletions src/librustc_metadata/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ use rustc::session::Session;
use rustc::session::filesearch::{FileSearch, FileMatches, FileDoesntMatch};
use rustc::session::search_paths::PathKind;
use rustc::util::common;
use rustc::util::nodemap::FnvHashMap;

use rustc_llvm as llvm;
use rustc_llvm::{False, ObjectFile, mk_section_iter};
Expand All @@ -230,7 +231,6 @@ use syntax_pos::Span;
use rustc_back::target::Target;

use std::cmp;
use std::collections::HashMap;
use std::fmt;
use std::fs;
use std::io;
Expand Down Expand Up @@ -413,7 +413,7 @@ impl<'a> Context<'a> {
let rlib_prefix = format!("lib{}", self.crate_name);
let staticlib_prefix = format!("{}{}", staticpair.0, self.crate_name);

let mut candidates = HashMap::new();
let mut candidates = FnvHashMap();
let mut staticlibs = vec!();

// First, find all possible candidate rlibs and dylibs purely based on
Expand Down Expand Up @@ -456,7 +456,7 @@ impl<'a> Context<'a> {

let hash_str = hash.to_string();
let slot = candidates.entry(hash_str)
.or_insert_with(|| (HashMap::new(), HashMap::new()));
.or_insert_with(|| (FnvHashMap(), FnvHashMap()));
let (ref mut rlibs, ref mut dylibs) = *slot;
fs::canonicalize(path).map(|p| {
if rlib {
Expand All @@ -477,7 +477,7 @@ impl<'a> Context<'a> {
// A Library candidate is created if the metadata for the set of
// libraries corresponds to the crate id and hash criteria that this
// search is being performed for.
let mut libraries = HashMap::new();
let mut libraries = FnvHashMap();
for (_hash, (rlibs, dylibs)) in candidates {
let mut slot = None;
let rlib = self.extract_one(rlibs, CrateFlavor::Rlib, &mut slot);
Expand Down Expand Up @@ -527,7 +527,7 @@ impl<'a> Context<'a> {
// read the metadata from it if `*slot` is `None`. If the metadata couldn't
// be read, it is assumed that the file isn't a valid rust library (no
// errors are emitted).
fn extract_one(&mut self, m: HashMap<PathBuf, PathKind>, flavor: CrateFlavor,
fn extract_one(&mut self, m: FnvHashMap<PathBuf, PathKind>, flavor: CrateFlavor,
slot: &mut Option<(Svh, MetadataBlob)>) -> Option<(PathBuf, PathKind)> {
let mut ret: Option<(PathBuf, PathKind)> = None;
let mut error = 0;
Expand Down Expand Up @@ -669,8 +669,8 @@ impl<'a> Context<'a> {
// rlibs/dylibs.
let sess = self.sess;
let dylibname = self.dylibname();
let mut rlibs = HashMap::new();
let mut dylibs = HashMap::new();
let mut rlibs = FnvHashMap();
let mut dylibs = FnvHashMap();
{
let locs = locs.map(|l| PathBuf::from(l)).filter(|loc| {
if !loc.exists() {
Expand Down
10 changes: 5 additions & 5 deletions src/librustc_metadata/macro_import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ use creader::CrateReader;
use cstore::CStore;

use rustc::session::Session;
use rustc::util::nodemap::{FnvHashSet, FnvHashMap};

use std::collections::{HashSet, HashMap};
use syntax::parse::token;
use syntax::ast;
use syntax::attr;
Expand Down Expand Up @@ -45,13 +45,13 @@ pub fn call_bad_macro_reexport(a: &Session, b: Span) {
span_err!(a, b, E0467, "bad macro reexport");
}

pub type MacroSelection = HashMap<token::InternedString, Span>;
pub type MacroSelection = FnvHashMap<token::InternedString, Span>;

impl<'a> ext::base::MacroLoader for MacroLoader<'a> {
fn load_crate(&mut self, extern_crate: &ast::Item, allows_macros: bool) -> Vec<ast::MacroDef> {
// Parse the attributes relating to macros.
let mut import = Some(HashMap::new()); // None => load all
let mut reexport = HashMap::new();
let mut import = Some(FnvHashMap()); // None => load all
let mut reexport = FnvHashMap();

for attr in &extern_crate.attrs {
let mut used = true;
Expand Down Expand Up @@ -120,7 +120,7 @@ impl<'a> MacroLoader<'a> {
}

let mut macros = Vec::new();
let mut seen = HashSet::new();
let mut seen = FnvHashSet();

for mut def in self.reader.read_exported_macros(vi) {
let name = def.ident.name.as_str();
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_resolve/assign_ids.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@

use Resolver;
use rustc::session::Session;
use rustc::util::nodemap::FnvHashMap;
use syntax::ast;
use syntax::ext::hygiene::Mark;
use syntax::fold::{self, Folder};
use syntax::ptr::P;
use syntax::util::move_map::MoveMap;
use syntax::util::small_vector::SmallVector;

use std::collections::HashMap;
use std::mem;

impl<'a> Resolver<'a> {
Expand All @@ -31,7 +31,7 @@ impl<'a> Resolver<'a> {

struct NodeIdAssigner<'a> {
sess: &'a Session,
macros_at_scope: &'a mut HashMap<ast::NodeId, Vec<Mark>>,
macros_at_scope: &'a mut FnvHashMap<ast::NodeId, Vec<Mark>>,
}

impl<'a> Folder for NodeIdAssigner<'a> {
Expand Down
Loading

0 comments on commit c95236f

Please sign in to comment.