Skip to content

Commit

Permalink
Optimize crates index generation (reduce 0.4% file size)
Browse files Browse the repository at this point in the history
  • Loading branch information
Folyd committed Nov 20, 2022
1 parent 6f940d3 commit 1e6e85a
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 5 deletions.
2 changes: 1 addition & 1 deletion extension/index/crates.js

Large diffs are not rendered by default.

9 changes: 6 additions & 3 deletions rust/src/minify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ struct FrequencyWord {
impl FrequencyWord {
#[inline]
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
}
}

Expand Down Expand Up @@ -66,9 +69,9 @@ impl Minifier {

Minifier {
mapping: words
.iter()
.into_iter()
.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(),
}
}
Expand Down
1 change: 1 addition & 0 deletions rust/src/tasks/crates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
let mut contents = String::from("var N=null;");
// <name, [optional description, version]>
let crates_map: HashMap<String, (Option<String>, Version)> = crates
.into_par_iter()
.map(|item| {
Expand Down
2 changes: 1 addition & 1 deletion rust/src/tasks/lints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ impl LintsTask {
.as_ref()
.and_then(|d| d.trim().strip_prefix("### What it does"))
{
let mut desc = docs.replace('`', "").replace('#', "");
let mut desc = docs.replace(['`', '#'], "");
desc.truncate(100);
Some((lint.id.clone(), [lint.level.to_string(), desc]))
} else {
Expand Down

0 comments on commit 1e6e85a

Please sign in to comment.