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

perf: use fxhash #9

Merged
merged 1 commit into from
May 29, 2024
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
48 changes: 7 additions & 41 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ name = "browserslist"
crate-type = ["cdylib", "rlib"]

[dependencies]
ahash = { version = "0.8", features = ["serde"] }
rustc-hash = "1.1.0"
chrono = { version = "0.4.31", features = [
"clock",
"oldtime",
Expand Down
8 changes: 4 additions & 4 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{error::Error, opts::Opts};
use ahash::AHashMap;
use either::Either;
use parser::parse;
use rustc_hash::FxHashMap;
use serde::Deserialize;
#[cfg(test)]
use serde::Serialize;
Expand All @@ -15,7 +15,7 @@ use std::{

mod parser;

type Config = AHashMap<String, Vec<String>>;
type Config = FxHashMap<String, Vec<String>>;

#[derive(Debug)]
#[cfg_attr(test, derive(PartialEq, Eq))]
Expand All @@ -35,7 +35,7 @@ pub enum PkgConfig {

impl Default for PkgConfig {
fn default() -> Self {
Self::Obj(AHashMap::default())
Self::Obj(FxHashMap::default())
}
}

Expand Down Expand Up @@ -288,7 +288,7 @@ mod tests {
);

// package.json with object format
let mut config_obj = AHashMap::new();
let mut config_obj = FxHashMap::default();
let _ = config_obj.insert("production".into(), vec!["> 1%".into(), "not dead".into()]);
let _ = config_obj.insert("modern".into(), vec!["last 1 version".into()]);
let _ = config_obj.insert("xp".into(), vec!["chrome >= 49".into()]);
Expand Down
4 changes: 2 additions & 2 deletions src/config/parser.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use super::PartialConfig;
use crate::error::Error;
use ahash::AHashSet;
use rustc_hash::FxHashSet;

pub fn parse<S: AsRef<str>>(
source: &str,
env: S,
throw_on_missing: bool,
) -> Result<PartialConfig, Error> {
let env = env.as_ref();
let mut encountered_sections = AHashSet::new();
let mut encountered_sections = FxHashSet::default();
let mut current_section = Some("defaults");

let config = source
Expand Down
12 changes: 6 additions & 6 deletions src/data/caniuse.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::data::BrowserName;
use ahash::AHashMap;
use once_cell::sync::Lazy;
use rustc_hash::FxHashMap;
use std::borrow::Cow;

pub mod features;
Expand All @@ -22,13 +22,13 @@ pub struct VersionDetail {
pub release_date: Option<i64>,
}

pub type CaniuseData = AHashMap<BrowserName, BrowserStat>;
pub type CaniuseData = FxHashMap<BrowserName, BrowserStat>;

pub use crate::generated::caniuse_browsers::CANIUSE_BROWSERS;
pub use crate::generated::caniuse_global_usage::CANIUSE_GLOBAL_USAGE;

pub static BROWSER_VERSION_ALIASES: Lazy<
AHashMap<BrowserName, AHashMap<&'static str, &'static str>>,
FxHashMap<BrowserName, FxHashMap<&'static str, &'static str>>,
> = Lazy::new(|| {
let mut aliases = CANIUSE_BROWSERS
.iter()
Expand All @@ -43,7 +43,7 @@ pub static BROWSER_VERSION_ALIASES: Lazy<
.map(|(bottom, top)| (bottom, top, version.version))
})
.fold(
AHashMap::<&str, &str>::new(),
FxHashMap::<&str, &str>::default(),
move |mut aliases, (bottom, top, version)| {
let _ = aliases.insert(bottom, version);
let _ = aliases.insert(top, version);
Expand All @@ -56,9 +56,9 @@ pub static BROWSER_VERSION_ALIASES: Lazy<
Some((*name, aliases))
}
})
.collect::<AHashMap<BrowserName, _>>();
.collect::<FxHashMap<BrowserName, _>>();
let _ = aliases.insert("op_mob", {
let mut aliases = AHashMap::new();
let mut aliases = FxHashMap::default();
let _ = aliases.insert("59", "58");
aliases
});
Expand Down
4 changes: 2 additions & 2 deletions src/data/caniuse/features.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use super::BrowserName;
use ahash::AHashMap;
use indexmap::IndexMap;
use rustc_hash::FxHashMap;

type Feature = AHashMap<BrowserName, IndexMap<&'static str, u8>>;
type Feature = FxHashMap<BrowserName, IndexMap<&'static str, u8>>;

pub fn get_feature_stat(name: &str) -> Option<&'static Feature> {
crate::generated::caniuse_feature_matching::_get_feature_stat(name)
Expand Down
4 changes: 2 additions & 2 deletions src/generated/caniuse_browsers.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use crate::data::caniuse::BrowserStat;
use crate::data::caniuse::CaniuseData;
use crate::data::caniuse::VersionDetail;
use ahash::AHashMap;
use once_cell::sync::Lazy;
use rustc_hash::FxHashMap;
pub static CANIUSE_BROWSERS: Lazy<CaniuseData> = Lazy::new(|| {
let mut map = AHashMap::with_capacity(19);
let mut map = FxHashMap::default();
map.insert(
"ie",
BrowserStat {
Expand Down
2,176 changes: 1,088 additions & 1,088 deletions src/generated/caniuse_feature_matching.rs

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions src/generated/node_release_schedule.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use ahash::AHashMap;
use chrono::{NaiveDate, NaiveDateTime};
use once_cell::sync::Lazy;
pub static RELEASE_SCHEDULE: Lazy<AHashMap<&'static str, (NaiveDateTime, NaiveDateTime)>> =
use rustc_hash::FxHashMap;
pub static RELEASE_SCHEDULE: Lazy<FxHashMap<&'static str, (NaiveDateTime, NaiveDateTime)>> =
Lazy::new(|| {
let date_format = "%Y-%m-%d";
let mut map = ahash::AHashMap::with_capacity(24usize);
let mut map = FxHashMap::default();
map.insert("0.8", ("2012-06-25", "2014-07-31"));
map.insert("0.10", ("2013-03-11", "2016-10-31"));
map.insert("0.12", ("2015-02-06", "2016-12-31"));
Expand Down
22 changes: 9 additions & 13 deletions xtask/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,6 @@ pub fn build_node_release_schedule() -> Result<()> {
root().join("node_modules/node-releases/data/release-schedule/release-schedule.json");
let schedule: IndexMap<String, NodeRelease> =
serde_json::from_slice(&fs::read(schedule_path)?)?;
let cap = schedule.len();
let versions = schedule
.into_iter()
.map(|(version, NodeRelease { start, end })| {
Expand All @@ -142,15 +141,15 @@ pub fn build_node_release_schedule() -> Result<()> {
});

let output = quote! {
use ahash::AHashMap;
use rustc_hash::FxHashMap;
use chrono::{NaiveDate, NaiveDateTime};
use once_cell::sync::Lazy;

pub static RELEASE_SCHEDULE: Lazy<AHashMap<&'static str, (NaiveDateTime, NaiveDateTime)>> =
pub static RELEASE_SCHEDULE: Lazy<FxHashMap<&'static str, (NaiveDateTime, NaiveDateTime)>> =
Lazy::new(|| {
let date_format = "%Y-%m-%d";

let mut map = ahash::AHashMap::with_capacity(#cap);
let mut map = FxHashMap::default();
#(#versions)*;
map
.into_iter()
Expand Down Expand Up @@ -181,7 +180,6 @@ pub fn build_node_release_schedule() -> Result<()> {
pub fn build_caniuse_global() -> Result<()> {
let data = parse_caniuse_global()?;

let map_cap = data.agents.len();
let browser_stat = data.agents.iter().map(|(name, agent)| {
let detail = agent.version_list.iter().map(|version| {
let ver = &version.version;
Expand All @@ -207,17 +205,15 @@ pub fn build_caniuse_global() -> Result<()> {
}
});

let map_cap = syn::Index::from(map_cap);

let output = quote! {
use ahash::AHashMap;
use rustc_hash::FxHashMap;
use crate::data::caniuse::VersionDetail;
use crate::data::caniuse::BrowserStat;
use crate::data::caniuse::CaniuseData;
use once_cell::sync::Lazy;
pub static CANIUSE_BROWSERS: Lazy<CaniuseData> =
Lazy::new(|| {
let mut map = AHashMap::with_capacity(#map_cap);
let mut map = FxHashMap::default();
#(#browser_stat)*;
map
});
Expand Down Expand Up @@ -291,19 +287,19 @@ pub fn build_caniuse_global() -> Result<()> {
let keys = data.data.keys().collect::<Vec<_>>();

let output = quote! {
use ahash::AHashMap;
use rustc_hash::FxHashMap;
use indexmap::IndexMap;
use once_cell::sync::Lazy;
use serde_json::from_str;
use crate::data::BrowserName;

type Feature = AHashMap<BrowserName, IndexMap<&'static str, u8>>;
type Feature = FxHashMap<BrowserName, IndexMap<&'static str, u8>>;

pub(crate) fn _get_feature_stat(name: &str) -> Option<&'static Feature> {
match name {
#( #keys => {
static STAT: Lazy<AHashMap<BrowserName, IndexMap<&'static str, u8>>> = Lazy::new(|| {
from_str::<AHashMap::<u8, IndexMap<&'static str, u8>>>(#features)
static STAT: Lazy<FxHashMap<BrowserName, IndexMap<&'static str, u8>>> = Lazy::new(|| {
from_str::<FxHashMap::<u8, IndexMap<&'static str, u8>>>(#features)
.unwrap()
.into_iter()
.map(|(browser, versions)| (crate::data::browser_name::decode_browser_name(browser), versions))
Expand Down
Loading