Skip to content

Commit

Permalink
Use FnvHash{Map,Set}
Browse files Browse the repository at this point in the history
  • Loading branch information
nathan committed Sep 1, 2018
1 parent 09b51e6 commit 287c9b4
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 19 deletions.
1 change: 1 addition & 0 deletions pax/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ bench = []
[dependencies]
esparse = { version = "0.1.0", path = "../esparse" }
regex = "1"
fnv = "1.0.3"
lazy_static = "1.1.0"
cfg-if = "0.1.4"
crossbeam = "0.2"
Expand Down
6 changes: 3 additions & 3 deletions pax/src/es6.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::fmt;
use std::fmt::Write;
use std::borrow::Cow;
use std::collections::HashSet;
use fnv::FnvHashSet;

use esparse;
use esparse::lex::{self, Tt};
Expand Down Expand Up @@ -104,7 +104,7 @@ pub struct CjsModule<'s> {
pub source_prefix: String,
pub source: String,
pub source_suffix: String,
pub deps: HashSet<Cow<'s, str>>,
pub deps: FnvHashSet<Cow<'s, str>>,
}

pub type Result<T> = ::std::result::Result<T, Error>;
Expand Down Expand Up @@ -157,7 +157,7 @@ impl fmt::Display for ErrorKind {

pub fn module_to_cjs<'f, 's>(lex: &mut lex::Lexer<'f, 's>, allow_require: bool) -> Result<CjsModule<'s>> {
let mut source = String::new();
let mut deps = HashSet::new();
let mut deps = FnvHashSet::default();
let mut imports = Vec::new();
let mut exports = Vec::new();
// TODO source map lines won't match up when module string literal contains newlines
Expand Down
29 changes: 15 additions & 14 deletions pax/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ extern crate serde_json;
extern crate memchr;
extern crate base64;
extern crate regex;
extern crate fnv;
#[macro_use]
extern crate matches;
#[macro_use]
Expand All @@ -28,10 +29,10 @@ use std::path::{self, PathBuf, Path, Component};
use std::sync::mpsc;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use std::collections::{HashMap, HashSet};
use std::any::Any;
use std::borrow::Cow;
use std::ffi::OsString;
use fnv::{FnvHashMap, FnvHashSet};
use crossbeam::sync::SegQueue;
use notify::Watcher;
use esparse::lex::{self, Tt};
Expand All @@ -46,9 +47,9 @@ const HEAD_JS: &str = include_str!("head.js");
const TAIL_JS: &str = include_str!("tail.js");
const CORE_MODULES: &[&str] = &["assert", "buffer", "child_process", "cluster", "crypto", "dgram", "dns", "domain", "events", "fs", "http", "https", "net", "os", "path", "punycode", "querystring", "readline", "stream", "string_decoder", "tls", "tty", "url", "util", "v8", "vm", "zlib"];

fn cjs_parse_deps<'f, 's>(lex: &mut lex::Lexer<'f, 's>) -> Result<HashSet<Cow<'s, str>>, CliError> {
fn cjs_parse_deps<'f, 's>(lex: &mut lex::Lexer<'f, 's>) -> Result<FnvHashSet<Cow<'s, str>>, CliError> {
// TODO should we panic on dynamic requires?
let mut deps = HashSet::new();
let mut deps = FnvHashSet::default();
loop {
eat!(lex,
// Tt::Id(s) if s == "require" => eat!(lex,
Expand Down Expand Up @@ -92,7 +93,7 @@ fn cjs_parse_deps<'f, 's>(lex: &mut lex::Lexer<'f, 's>) -> Result<HashSet<Cow<'s

#[derive(Debug)]
struct Writer<'a, 'b> {
modules: HashMap<PathBuf, Module>,
modules: FnvHashMap<PathBuf, Module>,
entry_point: &'a Path,
map_output: &'b SourceMapOutput<'b>,
}
Expand Down Expand Up @@ -291,7 +292,7 @@ impl<'a, 'b> Writer<'a, 'b> {
})
}

fn stringify_deps(deps: &HashMap<String, Resolved>) -> String {
fn stringify_deps(deps: &FnvHashMap<String, Resolved>) -> String {
let mut result = "{".to_owned();
let mut comma = false;
for (name, resolved) in deps {
Expand Down Expand Up @@ -424,7 +425,7 @@ enum ModuleState {
#[derive(Debug)]
pub struct Module {
pub source: Source,
pub deps: HashMap<String, Resolved>,
pub deps: FnvHashMap<String, Resolved>,
}
#[derive(Debug)]
struct ModuleInfo {
Expand All @@ -449,7 +450,7 @@ pub enum Resolved {
pub struct InputOptions {
pub es6_syntax: bool,
pub es6_syntax_everywhere: bool,
pub external: HashSet<String>,
pub external: FnvHashSet<String>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
Expand All @@ -471,7 +472,7 @@ impl ModuleState {
}
}

pub fn bundle(entry_point: &Path, input_options: InputOptions, output: &str, map_output: &SourceMapOutput) -> Result<HashMap<PathBuf, Module>, CliError> {
pub fn bundle(entry_point: &Path, input_options: InputOptions, output: &str, map_output: &SourceMapOutput) -> Result<FnvHashMap<PathBuf, Module>, CliError> {
let mut pending = 0;
let thread_count = num_cpus::get();
let (tx, rx) = mpsc::channel();
Expand All @@ -485,7 +486,7 @@ pub fn bundle(entry_point: &Path, input_options: InputOptions, output: &str, map
// TODO: context.require('…')
// TODO: watch for missing files on error?

let mut modules = HashMap::<PathBuf, ModuleState>::new();
let mut modules = FnvHashMap::<PathBuf, ModuleState>::default();

worker.add_work(Work::Include { module: entry_point.to_owned() });
pending += 1;
Expand Down Expand Up @@ -534,7 +535,7 @@ pub fn bundle(entry_point: &Path, input_options: InputOptions, output: &str, map
WorkDone::Include { module, info } => {
let old = modules.insert(module.clone(), ModuleState::Loaded(Module {
source: info.source,
deps: HashMap::new(),
deps: FnvHashMap::default(),
}));
debug_assert_matches!(old, Some(ModuleState::Loading));
for dep in info.deps {
Expand Down Expand Up @@ -612,7 +613,7 @@ fn run() -> Result<(), CliError> {
let mut no_map = false;
let mut watch = false;
let mut quiet_watch = false;
let mut external = HashSet::new();
let mut external = FnvHashSet::default();

let mut iter = opts::args();
while let Some(arg) = iter.next() {
Expand Down Expand Up @@ -755,8 +756,8 @@ fn run() -> Result<(), CliError> {
eprintln!("{bs}in {ms} ms", ms = ms, bs = "\u{8}".repeat(3));

{
let mut to_unwatch = modules.keys().collect::<HashSet<_>>();
let mut to_watch = new_modules.keys().collect::<HashSet<_>>();
let mut to_unwatch = modules.keys().collect::<FnvHashSet<_>>();
let mut to_watch = new_modules.keys().collect::<FnvHashSet<_>>();
for path in modules.keys() {
to_watch.remove(&path);
}
Expand Down Expand Up @@ -1334,7 +1335,7 @@ impl Worker {
new_source = Some(module.source);

} else if matches!(ext, Some(s) if s == "json") {
deps = HashSet::new();
deps = FnvHashSet::default();
prefix = "module.exports =".to_owned();
suffix = String::new();

Expand Down
4 changes: 2 additions & 2 deletions pax/src/test/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ cfg_if! {
fn bench_write_map_to(b: &mut test::Bencher) {
let writer = Writer {
modules: {
let mut modules = HashMap::new();
let mut modules = FnvHashMap::default();
for i in 0..1000 {
let mut path = PathBuf::new();
path.push(i.to_string());
Expand All @@ -133,7 +133,7 @@ cfg_if! {
original: None,
},
deps: {
let mut deps = HashMap::new();
let mut deps = FnvHashMap::new();
deps.insert("./math".to_owned(), Resolved::Normal(
Path::new("examples/es6-everywhere-simple/math.js").to_owned(),
));
Expand Down

0 comments on commit 287c9b4

Please sign in to comment.