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(mangler): reduce hash table lookups #8558

Closed
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
1 change: 0 additions & 1 deletion Cargo.lock

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

1 change: 0 additions & 1 deletion crates/oxc_mangler/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ oxc_allocator = { workspace = true }
oxc_ast = { workspace = true }
oxc_index = { workspace = true }
oxc_semantic = { workspace = true }
oxc_span = { workspace = true }

itertools = { workspace = true }
rustc-hash = { workspace = true }
54 changes: 34 additions & 20 deletions crates/oxc_mangler/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
use std::ops::Deref;

use itertools::Itertools;
use rustc_hash::FxHashSet;
use rustc_hash::{FxBuildHasher, FxHashSet};

use oxc_allocator::{Allocator, Vec};
use oxc_ast::ast::{Declaration, Program, Statement};
use oxc_index::Idx;
use oxc_semantic::{ReferenceId, ScopeTree, SemanticBuilder, SymbolId, SymbolTable};
use oxc_span::Atom;

#[derive(Default, Debug, Clone, Copy)]
pub struct MangleOptions {
Expand Down Expand Up @@ -183,24 +182,36 @@ impl Mangler {
&allocator,
);

// Create `HashSet` of symbol names which are unavailable
// (keywords, unresolved references, and top level bindings)
let root_unresolved_references = scope_tree.root_unresolved_references();
let root_bindings = scope_tree.get_bindings(scope_tree.root_scope_id());

let unavailable_names_count = KEYWORDS_AND_SPECIAL_NAMES.len()
+ root_unresolved_references.len()
+ root_bindings.len();
let mut unavailable_names =
FxHashSet::with_capacity_and_hasher(unavailable_names_count, FxBuildHasher);

unavailable_names.extend(KEYWORDS_AND_SPECIAL_NAMES);
unavailable_names.extend(root_unresolved_references.keys().copied());

if self.options.top_level {
unavailable_names
.extend(root_bindings.keys().copied().filter(|name| exported_names.contains(name)));
} else {
unavailable_names.extend(root_bindings.keys().copied());
}

// Create list of symbol names to use
let mut reserved_names = Vec::with_capacity_in(total_number_of_slots, &allocator);

let mut count = 0;
for _ in 0..total_number_of_slots {
let name = loop {
let name = generate_name(count);
count += 1;
// Do not mangle keywords and unresolved references
let n = name.as_str();
if !is_keyword(n)
&& !is_special_name(n)
&& !root_unresolved_references.contains_key(n)
&& !(root_bindings.contains_key(n)
&& (!self.options.top_level || exported_names.contains(n)))
{
if !unavailable_names.contains(name.as_str()) {
break name;
}
};
Expand Down Expand Up @@ -298,7 +309,7 @@ impl Mangler {

fn collect_exported_symbols<'a>(
program: &Program<'a>,
) -> (FxHashSet<Atom<'a>>, FxHashSet<SymbolId>) {
) -> (FxHashSet<&'a str>, FxHashSet<SymbolId>) {
program
.body
.iter()
Expand All @@ -317,7 +328,7 @@ impl Mangler {
itertools::Either::Right(decl.id().into_iter())
}
})
.map(|id| (id.name.clone(), id.symbol_id()))
.map(|id| (id.name.as_str(), id.symbol_id()))
.collect()
}
}
Expand All @@ -326,6 +337,17 @@ fn is_special_name(name: &str) -> bool {
matches!(name, "exports" | "arguments")
}

#[rustfmt::skip]
static KEYWORDS_AND_SPECIAL_NAMES: [&str; 28] = [
// Keywords
"as", "do", "if", "in", "is", "of", "any", "for", "get",
"let", "new", "out", "set", "try", "var", "case", "else",
"enum", "from", "meta", "null", "this", "true", "type",
"void", "with",
// Special names
"exports", "arguments",
];

#[derive(Debug)]
struct SlotFrequency<'a> {
pub slot: Slot,
Expand All @@ -339,14 +361,6 @@ impl<'a> SlotFrequency<'a> {
}
}

#[rustfmt::skip]
fn is_keyword(s: &str) -> bool {
matches!(s, "as" | "do" | "if" | "in" | "is" | "of" | "any" | "for" | "get"
| "let" | "new" | "out" | "set" | "try" | "var" | "case" | "else"
| "enum" | "from" | "meta" | "null" | "this" | "true" | "type"
| "void" | "with")
}

#[repr(C, align(64))]
struct Aligned64([u8; 64]);

Expand Down
Loading