Skip to content

Commit

Permalink
Use FnvHashMap instead of HashMap in rustc
Browse files Browse the repository at this point in the history
  • Loading branch information
arielb1 committed Nov 9, 2014
1 parent efc9a44 commit 85f1262
Show file tree
Hide file tree
Showing 35 changed files with 250 additions and 289 deletions.
5 changes: 2 additions & 3 deletions src/librustc/lint/builtin.rs
Expand Up @@ -32,11 +32,10 @@ use middle::typeck::infer;
use middle::{typeck, ty, def, pat_util, stability};
use middle::const_eval::{eval_const_expr_partial, const_int, const_uint};
use util::ppaux::{ty_to_string};
use util::nodemap::NodeSet;
use util::nodemap::{FnvHashMap, NodeSet};
use lint::{Context, LintPass, LintArray};

use std::cmp;
use std::collections::HashMap;
use std::collections::hash_map::{Occupied, Vacant};
use std::slice;
use std::{i8, i16, i32, i64, u8, u16, u32, u64, f32, f64};
Expand Down Expand Up @@ -1284,7 +1283,7 @@ impl UnusedMut {
// collect all mutable pattern and group their NodeIDs by their Identifier to
// avoid false warnings in match arms with multiple patterns

let mut mutables = HashMap::new();
let mut mutables = FnvHashMap::new();
for p in pats.iter() {
pat_util::pat_bindings(&cx.tcx.def_map, &**p, |mode, id, _, path1| {
let ident = path1.node;
Expand Down
21 changes: 11 additions & 10 deletions src/librustc/lint/context.rs
Expand Up @@ -34,8 +34,8 @@ use driver::early_error;
use lint::{Level, LevelSource, Lint, LintId, LintArray, LintPass, LintPassObject};
use lint::{Default, CommandLine, Node, Allow, Warn, Deny, Forbid};
use lint::builtin;
use util::nodemap::FnvHashMap;

use std::collections::HashMap;
use std::rc::Rc;
use std::cell::RefCell;
use std::tuple::Tuple2;
Expand Down Expand Up @@ -63,14 +63,14 @@ pub struct LintStore {
passes: Option<Vec<LintPassObject>>,

/// Lints indexed by name.
by_name: HashMap<String, TargetLint>,
by_name: FnvHashMap<String, TargetLint>,

/// Current levels of each lint, and where they were set.
levels: HashMap<LintId, LevelSource>,
levels: FnvHashMap<LintId, LevelSource>,

/// Map of registered lint groups to what lints they expand to. The bool
/// is true if the lint group was added by a plugin.
lint_groups: HashMap<&'static str, (Vec<LintId>, bool)>,
lint_groups: FnvHashMap<&'static str, (Vec<LintId>, bool)>,
}

/// The targed of the `by_name` map, which accounts for renaming/deprecation.
Expand Down Expand Up @@ -102,9 +102,9 @@ impl LintStore {
LintStore {
lints: vec!(),
passes: Some(vec!()),
by_name: HashMap::new(),
levels: HashMap::new(),
lint_groups: HashMap::new(),
by_name: FnvHashMap::new(),
levels: FnvHashMap::new(),
lint_groups: FnvHashMap::new(),
}
}

Expand Down Expand Up @@ -279,7 +279,8 @@ impl LintStore {
Some(lint_id) => self.set_level(lint_id, (level, CommandLine)),
None => {
match self.lint_groups.iter().map(|(&x, pair)| (x, pair.ref0().clone()))
.collect::<HashMap<&'static str, Vec<LintId>>>()
.collect::<FnvHashMap<&'static str,
Vec<LintId>>>()
.find_equiv(lint_name.as_slice()) {
Some(v) => {
v.iter()
Expand Down Expand Up @@ -317,7 +318,7 @@ pub struct Context<'a, 'tcx: 'a> {

/// Level of lints for certain NodeIds, stored here because the body of
/// the lint needs to run in trans.
node_levels: RefCell<HashMap<(ast::NodeId, LintId), LevelSource>>,
node_levels: RefCell<FnvHashMap<(ast::NodeId, LintId), LevelSource>>,
}

/// Convenience macro for calling a `LintPass` method on every pass in the context.
Expand Down Expand Up @@ -425,7 +426,7 @@ impl<'a, 'tcx> Context<'a, 'tcx> {
exported_items: exported_items,
lints: lint_store,
level_stack: vec![],
node_levels: RefCell::new(HashMap::new()),
node_levels: RefCell::new(FnvHashMap::new()),
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/librustc/metadata/creader.rs
Expand Up @@ -21,9 +21,9 @@ use metadata::decoder;
use metadata::loader;
use metadata::loader::CratePaths;
use plugin::load::PluginMetadata;
use util::nodemap::FnvHashMap;

use std::rc::Rc;
use std::collections::HashMap;
use std::collections::hash_map::{Occupied, Vacant};
use syntax::ast;
use syntax::abi;
Expand Down Expand Up @@ -85,7 +85,7 @@ fn dump_crates(cstore: &CStore) {
}

fn warn_if_multiple_versions(diag: &SpanHandler, cstore: &CStore) {
let mut map = HashMap::new();
let mut map = FnvHashMap::new();
cstore.iter_crate_data(|cnum, data| {
match map.entry(data.name()) {
Vacant(entry) => { entry.set(vec![cnum]); },
Expand Down
16 changes: 7 additions & 9 deletions src/librustc/metadata/cstore.rs
Expand Up @@ -16,11 +16,11 @@
use back::svh::Svh;
use metadata::decoder;
use metadata::loader;
use util::nodemap::{FnvHashMap, NodeMap};

use std::cell::RefCell;
use std::c_vec::CVec;
use std::rc::Rc;
use std::collections::HashMap;
use syntax::ast;
use syntax::codemap::Span;
use syntax::parse::token::IdentInterner;
Expand All @@ -29,7 +29,7 @@ use syntax::parse::token::IdentInterner;
// local crate numbers (as generated during this session). Each external
// crate may refer to types in other external crates, and each has their
// own crate numbers.
pub type cnum_map = HashMap<ast::CrateNum, ast::CrateNum>;
pub type cnum_map = FnvHashMap<ast::CrateNum, ast::CrateNum>;

pub enum MetadataBlob {
MetadataVec(CVec<u8>),
Expand Down Expand Up @@ -67,22 +67,20 @@ pub struct CrateSource {
}

pub struct CStore {
metas: RefCell<HashMap<ast::CrateNum, Rc<crate_metadata>>>,
extern_mod_crate_map: RefCell<extern_mod_crate_map>,
metas: RefCell<FnvHashMap<ast::CrateNum, Rc<crate_metadata>>>,
/// Map from NodeId's of local extern crate statements to crate numbers
extern_mod_crate_map: RefCell<NodeMap<ast::CrateNum>>,
used_crate_sources: RefCell<Vec<CrateSource>>,
used_libraries: RefCell<Vec<(String, NativeLibaryKind)>>,
used_link_args: RefCell<Vec<String>>,
pub intr: Rc<IdentInterner>,
}

// Map from NodeId's of local extern crate statements to crate numbers
type extern_mod_crate_map = HashMap<ast::NodeId, ast::CrateNum>;

impl CStore {
pub fn new(intr: Rc<IdentInterner>) -> CStore {
CStore {
metas: RefCell::new(HashMap::new()),
extern_mod_crate_map: RefCell::new(HashMap::new()),
metas: RefCell::new(FnvHashMap::new()),
extern_mod_crate_map: RefCell::new(FnvHashMap::new()),
used_crate_sources: RefCell::new(Vec::new()),
used_libraries: RefCell::new(Vec::new()),
used_link_args: RefCell::new(Vec::new()),
Expand Down
7 changes: 3 additions & 4 deletions src/librustc/metadata/encoder.rs
Expand Up @@ -23,13 +23,12 @@ use middle::ty::{lookup_item_type};
use middle::ty;
use middle::stability;
use middle;
use util::nodemap::{NodeMap, NodeSet};
use util::nodemap::{FnvHashMap, NodeMap, NodeSet};

use serialize::Encodable;
use std::cell::RefCell;
use std::hash::Hash;
use std::hash;
use std::collections::HashMap;
use syntax::abi;
use syntax::ast::*;
use syntax::ast;
Expand Down Expand Up @@ -2062,7 +2061,7 @@ fn encode_metadata_inner(wr: &mut SeekableMemWriter, parms: EncodeParams, krate:
link_meta: link_meta,
cstore: cstore,
encode_inlined_item: RefCell::new(encode_inlined_item),
type_abbrevs: RefCell::new(HashMap::new()),
type_abbrevs: RefCell::new(FnvHashMap::new()),
reachable: reachable,
};

Expand Down Expand Up @@ -2167,7 +2166,7 @@ pub fn encoded_ty(tcx: &ty::ctxt, t: ty::t) -> String {
diag: tcx.sess.diagnostic(),
ds: def_to_string,
tcx: tcx,
abbrevs: &RefCell::new(HashMap::new())
abbrevs: &RefCell::new(FnvHashMap::new())
}, t);
String::from_utf8(wr.unwrap()).unwrap()
}
4 changes: 2 additions & 2 deletions src/librustc/metadata/tyencode.rs
Expand Up @@ -14,12 +14,12 @@
#![allow(non_camel_case_types)]

use std::cell::RefCell;
use std::collections::HashMap;

use middle::subst;
use middle::subst::VecPerParamSpace;
use middle::ty::ParamTy;
use middle::ty;
use util::nodemap::FnvHashMap;

use syntax::abi::Abi;
use syntax::ast;
Expand Down Expand Up @@ -47,7 +47,7 @@ pub struct ty_abbrev {
s: String
}

pub type abbrev_map = RefCell<HashMap<ty::t, ty_abbrev>>;
pub type abbrev_map = RefCell<FnvHashMap<ty::t, ty_abbrev>>;

pub fn enc_ty(w: &mut SeekableMemWriter, cx: &ctxt, t: ty::t) {
match cx.abbrevs.borrow_mut().get(&t) {
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/middle/astencode.rs
Expand Up @@ -1161,7 +1161,7 @@ fn encode_side_tables_for_id(ecx: &e::EncodeContext,
})
}

for &ty in tcx.node_types.borrow().get(&(id as uint)).iter() {
for &ty in tcx.node_types.borrow().get(&id).iter() {
rbml_w.tag(c::tag_table_node_type, |rbml_w| {
rbml_w.id(id);
rbml_w.tag(c::tag_table_val, |rbml_w| {
Expand Down Expand Up @@ -1825,7 +1825,7 @@ fn decode_side_tables(dcx: &DecodeContext,
let ty = val_dsr.read_ty(dcx);
debug!("inserting ty for node {}: {}",
id, ty_to_string(dcx.tcx, ty));
dcx.tcx.node_types.borrow_mut().insert(id as uint, ty);
dcx.tcx.node_types.borrow_mut().insert(id, ty);
}
c::tag_table_item_subst => {
let item_substs = ty::ItemSubsts {
Expand Down
10 changes: 5 additions & 5 deletions src/librustc/middle/borrowck/move_data.rs
Expand Up @@ -18,7 +18,6 @@ comments in the section "Moves and initialization" and in `doc.rs`.
use std::cell::RefCell;
use std::rc::Rc;
use std::uint;
use std::collections::{HashMap, HashSet};
use middle::borrowck::*;
use middle::cfg;
use middle::dataflow::DataFlowContext;
Expand All @@ -30,14 +29,15 @@ use middle::ty;
use syntax::ast;
use syntax::ast_util;
use syntax::codemap::Span;
use util::nodemap::{FnvHashMap, NodeSet};
use util::ppaux::Repr;

pub struct MoveData {
/// Move paths. See section "Move paths" in `doc.rs`.
pub paths: RefCell<Vec<MovePath>>,

/// Cache of loan path to move path index, for easy lookup.
pub path_map: RefCell<HashMap<Rc<LoanPath>, MovePathIndex>>,
pub path_map: RefCell<FnvHashMap<Rc<LoanPath>, MovePathIndex>>,

/// Each move or uninitialized variable gets an entry here.
pub moves: RefCell<Vec<Move>>,
Expand All @@ -53,7 +53,7 @@ pub struct MoveData {
pub path_assignments: RefCell<Vec<Assignment>>,

/// Assignments to a variable or path, like `x = foo`, but not `x += foo`.
pub assignee_ids: RefCell<HashSet<ast::NodeId>>,
pub assignee_ids: RefCell<NodeSet>,
}

pub struct FlowedMoveData<'a, 'tcx: 'a> {
Expand Down Expand Up @@ -183,11 +183,11 @@ impl MoveData {
pub fn new() -> MoveData {
MoveData {
paths: RefCell::new(Vec::new()),
path_map: RefCell::new(HashMap::new()),
path_map: RefCell::new(FnvHashMap::new()),
moves: RefCell::new(Vec::new()),
path_assignments: RefCell::new(Vec::new()),
var_assignments: RefCell::new(Vec::new()),
assignee_ids: RefCell::new(HashSet::new()),
assignee_ids: RefCell::new(NodeSet::new()),
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/librustc/middle/dependency_format.rs
Expand Up @@ -61,14 +61,14 @@
//! Additionally, the algorithm is geared towards finding *any* solution rather
//! than finding a number of solutions (there are normally quite a few).

use std::collections::HashMap;
use syntax::ast;

use driver::session;
use driver::config;
use metadata::cstore;
use metadata::csearch;
use middle::ty;
use util::nodemap::FnvHashMap;

/// A list of dependencies for a certain crate type.
///
Expand All @@ -81,7 +81,7 @@ pub type DependencyList = Vec<Option<cstore::LinkagePreference>>;
/// A mapping of all required dependencies for a particular flavor of output.
///
/// This is local to the tcx, and is generally relevant to one session.
pub type Dependencies = HashMap<config::CrateType, DependencyList>;
pub type Dependencies = FnvHashMap<config::CrateType, DependencyList>;

pub fn calculate(tcx: &ty::ctxt) {
let mut fmts = tcx.dependency_formats.borrow_mut();
Expand Down Expand Up @@ -137,7 +137,7 @@ fn calculate_type(sess: &session::Session,
config::CrateTypeExecutable | config::CrateTypeDylib => {},
}

let mut formats = HashMap::new();
let mut formats = FnvHashMap::new();

// Sweep all crates for found dylibs. Add all dylibs, as well as their
// dependencies, ensuring there are no conflicts. The only valid case for a
Expand Down Expand Up @@ -208,7 +208,7 @@ fn calculate_type(sess: &session::Session,
fn add_library(sess: &session::Session,
cnum: ast::CrateNum,
link: cstore::LinkagePreference,
m: &mut HashMap<ast::CrateNum, cstore::LinkagePreference>) {
m: &mut FnvHashMap<ast::CrateNum, cstore::LinkagePreference>) {
match m.get(&cnum) {
Some(&link2) => {
// If the linkages differ, then we'd have two copies of the library
Expand Down
7 changes: 4 additions & 3 deletions src/librustc/middle/lang_items.rs
Expand Up @@ -24,6 +24,8 @@ use driver::session::Session;
use metadata::csearch::each_lang_item;
use middle::ty;
use middle::weak_lang_items;
use util::nodemap::FnvHashMap;

use syntax::ast;
use syntax::ast_util::local_def;
use syntax::attr::AttrMetaMethods;
Expand All @@ -32,7 +34,6 @@ use syntax::parse::token::InternedString;
use syntax::visit::Visitor;
use syntax::visit;

use std::collections::HashMap;
use std::iter::Enumerate;
use std::slice;

Expand Down Expand Up @@ -123,7 +124,7 @@ struct LanguageItemCollector<'a> {

session: &'a Session,

item_refs: HashMap<&'static str, uint>,
item_refs: FnvHashMap<&'static str, uint>,
}

impl<'a, 'v> Visitor<'v> for LanguageItemCollector<'a> {
Expand All @@ -148,7 +149,7 @@ impl<'a, 'v> Visitor<'v> for LanguageItemCollector<'a> {

impl<'a> LanguageItemCollector<'a> {
pub fn new(session: &'a Session) -> LanguageItemCollector<'a> {
let mut item_refs = HashMap::new();
let mut item_refs = FnvHashMap::new();

$( item_refs.insert($name, $variant as uint); )*

Expand Down
6 changes: 3 additions & 3 deletions src/librustc/middle/pat_util.rs
Expand Up @@ -11,18 +11,18 @@
use middle::def::*;
use middle::resolve;
use middle::ty;
use util::nodemap::FnvHashMap;

use std::collections::HashMap;
use syntax::ast::*;
use syntax::ast_util::{walk_pat};
use syntax::codemap::{Span, DUMMY_SP};

pub type PatIdMap = HashMap<Ident, NodeId>;
pub type PatIdMap = FnvHashMap<Ident, NodeId>;

// This is used because same-named variables in alternative patterns need to
// use the NodeId of their namesake in the first pattern.
pub fn pat_id_map(dm: &resolve::DefMap, pat: &Pat) -> PatIdMap {
let mut map = HashMap::new();
let mut map = FnvHashMap::new();
pat_bindings(dm, pat, |_bm, p_id, _s, path1| {
map.insert(path1.node, p_id);
});
Expand Down

5 comments on commit 85f1262

@bors
Copy link
Contributor

@bors bors commented on 85f1262 Nov 11, 2014

Choose a reason for hiding this comment

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

saw approval from eddyb
at arielb1@85f1262

@bors
Copy link
Contributor

@bors bors commented on 85f1262 Nov 11, 2014

Choose a reason for hiding this comment

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

merging arielb1/rust/fnv-hash-map = 85f1262 into auto

@bors
Copy link
Contributor

@bors bors commented on 85f1262 Nov 11, 2014

Choose a reason for hiding this comment

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

arielb1/rust/fnv-hash-map = 85f1262 merged ok, testing candidate = 8f87538

@bors
Copy link
Contributor

@bors bors commented on 85f1262 Nov 11, 2014

Choose a reason for hiding this comment

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

@bors
Copy link
Contributor

@bors bors commented on 85f1262 Nov 11, 2014

Choose a reason for hiding this comment

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

fast-forwarding master to auto = 8f87538

Please sign in to comment.