Skip to content

Commit

Permalink
perf(linter, ast-tools, coverage): Use FxHashSet instead of `std::c…
Browse files Browse the repository at this point in the history
…ollections::HashSet` (#6001)
  • Loading branch information
camchenry committed Sep 24, 2024
1 parent 28da771 commit 65d8f9e
Show file tree
Hide file tree
Showing 13 changed files with 42 additions and 43 deletions.
1 change: 1 addition & 0 deletions .clippy.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ disallowed-methods = [

disallowed-types = [
{ path = "std::collections::HashMap", reason = "Use `rustc_hash::FxHashMap` instead, which is typically faster." },
{ path = "std::collections::HashSet", reason = "Use `rustc_hash::FxHashSet` instead, which is typically faster." },
]
1 change: 1 addition & 0 deletions Cargo.lock

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

10 changes: 4 additions & 6 deletions crates/oxc_linter/src/rules/eslint/no_this_before_super.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::collections::HashSet;

use oxc_ast::{
ast::{Argument, Expression, MethodDefinitionKind},
AstKind,
Expand All @@ -12,7 +10,7 @@ use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_semantic::NodeId;
use oxc_span::{GetSpan, Span};
use rustc_hash::FxHashMap;
use rustc_hash::{FxHashMap, FxHashSet};

use crate::{context::LintContext, rule::Rule, AstNode};

Expand Down Expand Up @@ -62,7 +60,7 @@ impl Rule for NoThisBeforeSuper {

// first pass -> find super calls and local violations
let mut wanted_nodes = Vec::new();
let mut basic_blocks_with_super_called = HashSet::<BasicBlockId>::new();
let mut basic_blocks_with_super_called = FxHashSet::<BasicBlockId>::default();
let mut basic_blocks_with_local_violations =
FxHashMap::<BasicBlockId, Vec<NodeId>>::default();
for node in semantic.nodes() {
Expand Down Expand Up @@ -154,7 +152,7 @@ impl NoThisBeforeSuper {
fn analyze(
cfg: &ControlFlowGraph,
id: BasicBlockId,
basic_blocks_with_super_called: &HashSet<BasicBlockId>,
basic_blocks_with_super_called: &FxHashSet<BasicBlockId>,
basic_blocks_with_local_violations: &FxHashMap<BasicBlockId, Vec<NodeId>>,
follow_join: bool,
) -> Vec<DefinitelyCallsThisBeforeSuper> {
Expand Down Expand Up @@ -213,7 +211,7 @@ impl NoThisBeforeSuper {
fn check_for_violation(
cfg: &ControlFlowGraph,
output: Vec<DefinitelyCallsThisBeforeSuper>,
basic_blocks_with_super_called: &HashSet<BasicBlockId>,
basic_blocks_with_super_called: &FxHashSet<BasicBlockId>,
basic_blocks_with_local_violations: &FxHashMap<BasicBlockId, Vec<NodeId>>,
) -> bool {
// Deciding whether we definitely call this before super in all
Expand Down
5 changes: 2 additions & 3 deletions crates/oxc_linter/src/rules/react/jsx_boolean_value.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
use std::collections::HashSet;

use oxc_ast::{
ast::{Expression, JSXAttributeItem, JSXAttributeName, JSXAttributeValue},
AstKind,
};
use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_span::Span;
use rustc_hash::FxHashSet;

use crate::{
context::{ContextHost, LintContext},
Expand Down Expand Up @@ -43,7 +42,7 @@ pub enum EnforceBooleanAttribute {
#[derive(Debug, Default, Clone)]
pub struct JsxBooleanValueConfig {
pub enforce_boolean_attribute: EnforceBooleanAttribute,
pub exceptions: HashSet<String>,
pub exceptions: FxHashSet<String>,
pub assume_undefined_is_false: bool,
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::collections::HashSet;

use oxc_ast::{
ast::{
ArrowFunctionExpression, BindingPatternKind, Expression, FunctionType, JSXAttributeItem,
Expand All @@ -11,6 +9,7 @@ use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_span::Span;
use oxc_syntax::operator::UnaryOperator;
use rustc_hash::FxHashSet;

use crate::{
ast_util::outermost_paren_parent,
Expand All @@ -32,7 +31,7 @@ pub struct ExplicitFunctionReturnTypeConfig {
allow_direct_const_assertion_in_arrow_functions: bool,
allow_concise_arrow_function_expressions_starting_with_void: bool,
allow_functions_without_type_parameters: bool,
allowed_names: HashSet<String>,
allowed_names: FxHashSet<String>,
allow_higher_order_functions: bool,
allow_iifes: bool,
}
Expand Down
31 changes: 15 additions & 16 deletions crates/oxc_linter/src/rules/vitest/prefer_each.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use std::collections::HashSet;

use oxc_ast::AstKind;
use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_semantic::{AstNode, NodeId};
use oxc_span::{GetSpan, Span};
use rustc_hash::FxHashSet;

use crate::{
context::LintContext,
Expand Down Expand Up @@ -70,15 +69,15 @@ declare_oxc_lint!(

impl Rule for PreferEach {
fn run_once(&self, ctx: &LintContext<'_>) {
let mut skip = HashSet::<NodeId>::new();
let mut skip = FxHashSet::<NodeId>::default();
ctx.nodes().iter().for_each(|node| {
Self::run(node, ctx, &mut skip);
});
}
}

impl PreferEach {
fn run<'a>(node: &AstNode<'a>, ctx: &LintContext<'a>, skip: &mut HashSet<NodeId>) {
fn run<'a>(node: &AstNode<'a>, ctx: &LintContext<'a>, skip: &mut FxHashSet<NodeId>) {
let kind = node.kind();

let AstKind::CallExpression(call_expr) = kind else { return };
Expand Down Expand Up @@ -164,7 +163,7 @@ fn test() {
});"#,
r#"it("only returns numbers that are greater than seven", function () {
const numbers = getNumbers();
for (let i = 0; i < numbers.length; i++) {
expect(numbers[i]).toBeGreaterThan(7);
}
Expand All @@ -191,7 +190,7 @@ fn test() {
});
});
}
for (const [input, expected] of data) {
it.skip(`results in ${expected}`, () => {
expect(fn(input)).toBe(expected)
Expand All @@ -205,7 +204,7 @@ fn test() {
"it('is true', () => {
expect(true).toBe(false);
});
for (const [input, expected] of data) {
it.skip(`results in ${expected}`, () => {
expect(fn(input)).toBe(expected)
Expand All @@ -216,28 +215,28 @@ fn test() {
expect(fn(input)).toBe(expected)
});
}
it('is true', () => {
expect(true).toBe(false);
});",
" it('is true', () => {
expect(true).toBe(false);
});
for (const [input, expected] of data) {
it.skip(`results in ${expected}`, () => {
expect(fn(input)).toBe(expected)
});
}
it('is true', () => {
expect(true).toBe(false);
});",
"for (const [input, expected] of data) {
it(`results in ${expected}`, () => {
expect(fn(input)).toBe(expected)
});
it(`results in ${expected}`, () => {
expect(fn(input)).toBe(expected)
});
Expand All @@ -247,15 +246,15 @@ fn test() {
expect(fn(input)).toBe(expected)
});
}
for (const [input, expected] of data) {
it(`results in ${expected}`, () => {
expect(fn(input)).toBe(expected)
});
}",
"for (const [input, expected] of data) {
beforeEach(() => setupSomething(input));
test(`results in ${expected}`, () => {
expect(doSomething()).toBe(expected)
});
Expand All @@ -264,7 +263,7 @@ fn test() {
for (const [input, expected] of data) {
it("only returns numbers that are greater than seven", function () {
const numbers = getNumbers(input);
for (let i = 0; i < numbers.length; i++) {
expect(numbers[i]).toBeGreaterThan(7);
}
Expand All @@ -274,10 +273,10 @@ fn test() {
r#"
for (const [input, expected] of data) {
beforeEach(() => setupSomething(input));
it("only returns numbers that are greater than seven", function () {
const numbers = getNumbers();
for (let i = 0; i < numbers.length; i++) {
expect(numbers[i]).toBeGreaterThan(7);
}
Expand Down
8 changes: 4 additions & 4 deletions crates/oxc_linter/src/snapshots/prefer_each.snap
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ source: crates/oxc_linter/src/tester.rs

eslint-plugin-vitest(prefer-each): Enforce using `each` rather than manual loops
╭─[prefer_each.tsx:9:11]
8
8
9for (const [input, expected] of data) {
· ──────────────────────────────────────
10it.skip(`results in ${expected}`, () => {
Expand All @@ -44,7 +44,7 @@ source: crates/oxc_linter/src/tester.rs

eslint-plugin-vitest(prefer-each): Enforce using `each` rather than manual loops
╭─[prefer_each.tsx:5:11]
4
4
5for (const [input, expected] of data) {
· ──────────────────────────────────────
6it.skip(`results in ${expected}`, () => {
Expand All @@ -61,7 +61,7 @@ source: crates/oxc_linter/src/tester.rs

eslint-plugin-vitest(prefer-each): Enforce using `each` rather than manual loops
╭─[prefer_each.tsx:5:11]
4
4
5for (const [input, expected] of data) {
· ──────────────────────────────────────
6it.skip(`results in ${expected}`, () => {
Expand All @@ -86,7 +86,7 @@ source: crates/oxc_linter/src/tester.rs

eslint-plugin-vitest(prefer-each): Enforce using `each` rather than manual loops
╭─[prefer_each.tsx:7:11]
6
6
7for (const [input, expected] of data) {
· ──────────────────────────────────────
8it(`results in ${expected}`, () => {
Expand Down
5 changes: 2 additions & 3 deletions tasks/ast_tools/src/derives/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,10 @@ macro_rules! define_derive {
}

fn run(&mut self, ctx: &$crate::codegen::LateCtx) -> $crate::Result<Self::Output> {
use std::collections::{HashSet};
use std::vec::Vec;
use convert_case::{Case, Casing};
use itertools::Itertools;
use rustc_hash::FxHashMap;
use rustc_hash::{FxHashMap, FxHashSet};

use $crate::derives::DeriveTemplate;

Expand All @@ -91,7 +90,7 @@ macro_rules! define_derive {
.into_iter()
.filter(|def| def.generates_derive(trait_name))
.map(|def| (def, self.derive(def, ctx)))
.fold(FxHashMap::<&str, (HashSet<&str>, Vec<TokenStream>)>::default(), |mut acc, (def, stream)| {
.fold(FxHashMap::<&str, (FxHashSet<&str>, Vec<TokenStream>)>::default(), |mut acc, (def, stream)| {
let module_path = def.module_path();
let krate = module_path.split("::").next().unwrap();
if !acc.contains_key(krate) {
Expand Down
3 changes: 2 additions & 1 deletion tasks/ast_tools/src/schema/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use quote::ToTokens;
use rustc_hash::FxHashSet;
use serde::Serialize;

use crate::{
Expand Down Expand Up @@ -294,7 +295,7 @@ fn get_docs(attrs: &[syn::Attribute]) -> Vec<String> {
}

fn parse_generate_derive(attrs: &[syn::Attribute]) -> Vec<String> {
let mut derives = std::collections::HashSet::new();
let mut derives = FxHashSet::default();
for attr in attrs {
if !attr.path().is_ident("generate_derive") {
continue;
Expand Down
5 changes: 3 additions & 2 deletions tasks/coverage/src/driver.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{collections::HashSet, ops::ControlFlow, path::PathBuf};
use std::{ops::ControlFlow, path::PathBuf};

use oxc::{
allocator::Allocator,
Expand All @@ -19,6 +19,7 @@ use oxc::{
transformer::{TransformOptions, TransformerReturn},
CompilerInterface,
};
use rustc_hash::FxHashSet;

use crate::suite::TestResult;

Expand Down Expand Up @@ -146,7 +147,7 @@ impl Driver {
}

fn check_comments(&mut self, trivias: &Trivias) -> bool {
let mut uniq: HashSet<Span> = HashSet::new();
let mut uniq: FxHashSet<Span> = FxHashSet::default();
for comment in trivias.comments() {
if !uniq.insert(comment.span) {
self.errors
Expand Down
6 changes: 3 additions & 3 deletions tasks/coverage/src/runtime/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use std::{
collections::HashSet,
fs,
path::{Path, PathBuf},
time::Duration,
Expand All @@ -8,6 +7,7 @@ use std::{
use oxc::{allocator::Allocator, codegen::CodeGenerator, parser::Parser, span::SourceType};
use oxc_tasks_common::agent;
use phf::{phf_set, Set};
use rustc_hash::FxHashSet;
use serde_json::json;

use crate::{
Expand All @@ -19,8 +19,8 @@ use crate::{
pub const V8_TEST_262_FAILED_TESTS_PATH: &str = "src/runtime/v8_test262.status";

lazy_static::lazy_static! {
static ref V8_TEST_262_FAILED_TESTS: HashSet<String> = {
let mut set = HashSet::default();
static ref V8_TEST_262_FAILED_TESTS: FxHashSet<String> = {
let mut set = FxHashSet::default();
fs::read_to_string(workspace_root().join(V8_TEST_262_FAILED_TESTS_PATH))
.expect("Failed to read v8_test262.status")
.lines()
Expand Down
1 change: 1 addition & 0 deletions tasks/prettier_conformance/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,5 @@ oxc_span = { workspace = true }
oxc_tasks_common = { workspace = true }

pico-args = { workspace = true }
rustc-hash = { workspace = true }
walkdir = { workspace = true }
4 changes: 2 additions & 2 deletions tasks/prettier_conformance/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ mod ignore_list;
mod spec;

use std::{
collections::HashSet,
fs,
path::{Path, PathBuf},
};
Expand All @@ -13,6 +12,7 @@ use oxc_parser::{ParseOptions, Parser};
use oxc_prettier::{Prettier, PrettierOptions};
use oxc_span::SourceType;
use oxc_tasks_common::project_root;
use rustc_hash::FxHashSet;
use walkdir::WalkDir;

use crate::{
Expand Down Expand Up @@ -113,7 +113,7 @@ impl TestRunner {
.filter(|path| path.join("__snapshots__").exists())
.collect::<Vec<_>>();

let dir_set: HashSet<_> = dirs.iter().cloned().collect();
let dir_set: FxHashSet<_> = dirs.iter().cloned().collect();
dirs = dir_set.into_iter().collect();

dirs.sort_unstable();
Expand Down

0 comments on commit 65d8f9e

Please sign in to comment.