Skip to content

Commit

Permalink
perf(linter): use usize for RuleEnum hash (oxc-project#3336)
Browse files Browse the repository at this point in the history
  • Loading branch information
Boshen authored May 18, 2024
1 parent 8383b6e commit 8388c7b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
2 changes: 1 addition & 1 deletion crates/oxc_linter/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ impl LintOptions {
let mut rules = rules.into_iter().collect::<Vec<_>>();

// for stable diagnostics output ordering
rules.sort_unstable_by_key(RuleEnum::name);
rules.sort_unstable_by_key(RuleEnum::id);

Ok((rules, config.unwrap_or_default()))
}
Expand Down
13 changes: 10 additions & 3 deletions crates/oxc_macros/src/declare_all_lint_rules/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ pub fn declare_all_lint_rules(metadata: AllLintRulesMeta) -> TokenStream {
.collect::<Vec<_>>()
.join("/")
});
let ids = rules.iter().enumerate().map(|(i, _)| i).collect::<Vec<_>>();

let expanded = quote! {
#(#use_stmts)*
Expand All @@ -74,6 +75,12 @@ pub fn declare_all_lint_rules(metadata: AllLintRulesMeta) -> TokenStream {
}

impl RuleEnum {
pub fn id(&self) -> usize {
match self {
#(Self::#struct_names(_) => #ids),*
}
}

pub fn name(&self) -> &'static str {
match self {
#(Self::#struct_names(_) => #struct_names::NAME),*
Expand Down Expand Up @@ -127,21 +134,21 @@ pub fn declare_all_lint_rules(metadata: AllLintRulesMeta) -> TokenStream {

impl std::hash::Hash for RuleEnum {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.name().hash(state);
self.id().hash(state);
}
}

impl PartialEq for RuleEnum {
fn eq(&self, other: &Self) -> bool {
self.name() == other.name()
self.id() == other.id()
}
}

impl Eq for RuleEnum {}

impl Ord for RuleEnum {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.name().cmp(&other.name())
self.id().cmp(&other.id())
}
}

Expand Down

0 comments on commit 8388c7b

Please sign in to comment.