Optimize crates index generation (reduce 0.4% file size)

This commit is contained in:
Folyd 2022-11-20 15:27:24 +08:00
parent 6f940d32a4
commit 1e6e85a3eb
4 changed files with 9 additions and 5 deletions

File diff suppressed because one or more lines are too long

View file

@ -17,7 +17,10 @@ struct FrequencyWord {
impl FrequencyWord { impl FrequencyWord {
#[inline] #[inline]
fn score(&self) -> usize { fn score(&self) -> usize {
self.word.len() * self.frequency // Due to the prefix + suffix occupying two letters,
// we should minus the length to calculate the score.
// This will lead to a 0.4% reduction in file size.
(self.word.len() - 2) * self.frequency
} }
} }
@ -66,9 +69,9 @@ impl Minifier {
Minifier { Minifier {
mapping: words mapping: words
.iter() .into_iter()
.enumerate() .enumerate()
.map(|(index, fw)| (fw.word.clone(), keys.get(index).unwrap().to_owned())) .map(|(index, fw)| (fw.word, keys.get(index).unwrap().to_owned()))
.collect(), .collect(),
} }
} }

View file

@ -104,6 +104,7 @@ fn read_csv<D: DeserializeOwned>(file: impl Read) -> crate::Result<Vec<D>> {
fn generate_javascript_crates_index(crates: Vec<Crate>, minifier: &Minifier) -> String { fn generate_javascript_crates_index(crates: Vec<Crate>, minifier: &Minifier) -> String {
let mut contents = String::from("var N=null;"); let mut contents = String::from("var N=null;");
// <name, [optional description, version]>
let crates_map: HashMap<String, (Option<String>, Version)> = crates let crates_map: HashMap<String, (Option<String>, Version)> = crates
.into_par_iter() .into_par_iter()
.map(|item| { .map(|item| {

View file

@ -75,7 +75,7 @@ impl LintsTask {
.as_ref() .as_ref()
.and_then(|d| d.trim().strip_prefix("### What it does")) .and_then(|d| d.trim().strip_prefix("### What it does"))
{ {
let mut desc = docs.replace('`', "").replace('#', ""); let mut desc = docs.replace(['`', '#'], "");
desc.truncate(100); desc.truncate(100);
Some((lint.id.clone(), [lint.level.to_string(), desc])) Some((lint.id.clone(), [lint.level.to_string(), desc]))
} else { } else {