Skip to content

Commit 65d8f9e

Browse files
authoredSep 24, 2024··
perf(linter, ast-tools, coverage): Use FxHashSet instead of std::collections::HashSet (#6001)
1 parent 28da771 commit 65d8f9e

File tree

13 files changed

+42
-43
lines changed

13 files changed

+42
-43
lines changed
 

‎.clippy.toml

+1
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@ disallowed-methods = [
1111

1212
disallowed-types = [
1313
{ path = "std::collections::HashMap", reason = "Use `rustc_hash::FxHashMap` instead, which is typically faster." },
14+
{ path = "std::collections::HashSet", reason = "Use `rustc_hash::FxHashSet` instead, which is typically faster." },
1415
]

‎Cargo.lock

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎crates/oxc_linter/src/rules/eslint/no_this_before_super.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use std::collections::HashSet;
2-
31
use oxc_ast::{
42
ast::{Argument, Expression, MethodDefinitionKind},
53
AstKind,
@@ -12,7 +10,7 @@ use oxc_diagnostics::OxcDiagnostic;
1210
use oxc_macros::declare_oxc_lint;
1311
use oxc_semantic::NodeId;
1412
use oxc_span::{GetSpan, Span};
15-
use rustc_hash::FxHashMap;
13+
use rustc_hash::{FxHashMap, FxHashSet};
1614

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

@@ -62,7 +60,7 @@ impl Rule for NoThisBeforeSuper {
6260

6361
// first pass -> find super calls and local violations
6462
let mut wanted_nodes = Vec::new();
65-
let mut basic_blocks_with_super_called = HashSet::<BasicBlockId>::new();
63+
let mut basic_blocks_with_super_called = FxHashSet::<BasicBlockId>::default();
6664
let mut basic_blocks_with_local_violations =
6765
FxHashMap::<BasicBlockId, Vec<NodeId>>::default();
6866
for node in semantic.nodes() {
@@ -154,7 +152,7 @@ impl NoThisBeforeSuper {
154152
fn analyze(
155153
cfg: &ControlFlowGraph,
156154
id: BasicBlockId,
157-
basic_blocks_with_super_called: &HashSet<BasicBlockId>,
155+
basic_blocks_with_super_called: &FxHashSet<BasicBlockId>,
158156
basic_blocks_with_local_violations: &FxHashMap<BasicBlockId, Vec<NodeId>>,
159157
follow_join: bool,
160158
) -> Vec<DefinitelyCallsThisBeforeSuper> {
@@ -213,7 +211,7 @@ impl NoThisBeforeSuper {
213211
fn check_for_violation(
214212
cfg: &ControlFlowGraph,
215213
output: Vec<DefinitelyCallsThisBeforeSuper>,
216-
basic_blocks_with_super_called: &HashSet<BasicBlockId>,
214+
basic_blocks_with_super_called: &FxHashSet<BasicBlockId>,
217215
basic_blocks_with_local_violations: &FxHashMap<BasicBlockId, Vec<NodeId>>,
218216
) -> bool {
219217
// Deciding whether we definitely call this before super in all

‎crates/oxc_linter/src/rules/react/jsx_boolean_value.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
use std::collections::HashSet;
2-
31
use oxc_ast::{
42
ast::{Expression, JSXAttributeItem, JSXAttributeName, JSXAttributeValue},
53
AstKind,
64
};
75
use oxc_diagnostics::OxcDiagnostic;
86
use oxc_macros::declare_oxc_lint;
97
use oxc_span::Span;
8+
use rustc_hash::FxHashSet;
109

1110
use crate::{
1211
context::{ContextHost, LintContext},
@@ -43,7 +42,7 @@ pub enum EnforceBooleanAttribute {
4342
#[derive(Debug, Default, Clone)]
4443
pub struct JsxBooleanValueConfig {
4544
pub enforce_boolean_attribute: EnforceBooleanAttribute,
46-
pub exceptions: HashSet<String>,
45+
pub exceptions: FxHashSet<String>,
4746
pub assume_undefined_is_false: bool,
4847
}
4948

‎crates/oxc_linter/src/rules/typescript/explicit_function_return_type.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use std::collections::HashSet;
2-
31
use oxc_ast::{
42
ast::{
53
ArrowFunctionExpression, BindingPatternKind, Expression, FunctionType, JSXAttributeItem,
@@ -11,6 +9,7 @@ use oxc_diagnostics::OxcDiagnostic;
119
use oxc_macros::declare_oxc_lint;
1210
use oxc_span::Span;
1311
use oxc_syntax::operator::UnaryOperator;
12+
use rustc_hash::FxHashSet;
1413

1514
use crate::{
1615
ast_util::outermost_paren_parent,
@@ -32,7 +31,7 @@ pub struct ExplicitFunctionReturnTypeConfig {
3231
allow_direct_const_assertion_in_arrow_functions: bool,
3332
allow_concise_arrow_function_expressions_starting_with_void: bool,
3433
allow_functions_without_type_parameters: bool,
35-
allowed_names: HashSet<String>,
34+
allowed_names: FxHashSet<String>,
3635
allow_higher_order_functions: bool,
3736
allow_iifes: bool,
3837
}

‎crates/oxc_linter/src/rules/vitest/prefer_each.rs

+15-16
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
use std::collections::HashSet;
2-
31
use oxc_ast::AstKind;
42
use oxc_diagnostics::OxcDiagnostic;
53
use oxc_macros::declare_oxc_lint;
64
use oxc_semantic::{AstNode, NodeId};
75
use oxc_span::{GetSpan, Span};
6+
use rustc_hash::FxHashSet;
87

98
use crate::{
109
context::LintContext,
@@ -70,15 +69,15 @@ declare_oxc_lint!(
7069

7170
impl Rule for PreferEach {
7271
fn run_once(&self, ctx: &LintContext<'_>) {
73-
let mut skip = HashSet::<NodeId>::new();
72+
let mut skip = FxHashSet::<NodeId>::default();
7473
ctx.nodes().iter().for_each(|node| {
7574
Self::run(node, ctx, &mut skip);
7675
});
7776
}
7877
}
7978

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

8483
let AstKind::CallExpression(call_expr) = kind else { return };
@@ -164,7 +163,7 @@ fn test() {
164163
});"#,
165164
r#"it("only returns numbers that are greater than seven", function () {
166165
const numbers = getNumbers();
167-
166+
168167
for (let i = 0; i < numbers.length; i++) {
169168
expect(numbers[i]).toBeGreaterThan(7);
170169
}
@@ -191,7 +190,7 @@ fn test() {
191190
});
192191
});
193192
}
194-
193+
195194
for (const [input, expected] of data) {
196195
it.skip(`results in ${expected}`, () => {
197196
expect(fn(input)).toBe(expected)
@@ -205,7 +204,7 @@ fn test() {
205204
"it('is true', () => {
206205
expect(true).toBe(false);
207206
});
208-
207+
209208
for (const [input, expected] of data) {
210209
it.skip(`results in ${expected}`, () => {
211210
expect(fn(input)).toBe(expected)
@@ -216,28 +215,28 @@ fn test() {
216215
expect(fn(input)).toBe(expected)
217216
});
218217
}
219-
218+
220219
it('is true', () => {
221220
expect(true).toBe(false);
222221
});",
223222
" it('is true', () => {
224223
expect(true).toBe(false);
225224
});
226-
225+
227226
for (const [input, expected] of data) {
228227
it.skip(`results in ${expected}`, () => {
229228
expect(fn(input)).toBe(expected)
230229
});
231230
}
232-
231+
233232
it('is true', () => {
234233
expect(true).toBe(false);
235234
});",
236235
"for (const [input, expected] of data) {
237236
it(`results in ${expected}`, () => {
238237
expect(fn(input)).toBe(expected)
239238
});
240-
239+
241240
it(`results in ${expected}`, () => {
242241
expect(fn(input)).toBe(expected)
243242
});
@@ -247,15 +246,15 @@ fn test() {
247246
expect(fn(input)).toBe(expected)
248247
});
249248
}
250-
249+
251250
for (const [input, expected] of data) {
252251
it(`results in ${expected}`, () => {
253252
expect(fn(input)).toBe(expected)
254253
});
255254
}",
256255
"for (const [input, expected] of data) {
257256
beforeEach(() => setupSomething(input));
258-
257+
259258
test(`results in ${expected}`, () => {
260259
expect(doSomething()).toBe(expected)
261260
});
@@ -264,7 +263,7 @@ fn test() {
264263
for (const [input, expected] of data) {
265264
it("only returns numbers that are greater than seven", function () {
266265
const numbers = getNumbers(input);
267-
266+
268267
for (let i = 0; i < numbers.length; i++) {
269268
expect(numbers[i]).toBeGreaterThan(7);
270269
}
@@ -274,10 +273,10 @@ fn test() {
274273
r#"
275274
for (const [input, expected] of data) {
276275
beforeEach(() => setupSomething(input));
277-
276+
278277
it("only returns numbers that are greater than seven", function () {
279278
const numbers = getNumbers();
280-
279+
281280
for (let i = 0; i < numbers.length; i++) {
282281
expect(numbers[i]).toBeGreaterThan(7);
283282
}

‎crates/oxc_linter/src/snapshots/prefer_each.snap

+4-4
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ source: crates/oxc_linter/src/tester.rs
2727

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

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

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

8787
eslint-plugin-vitest(prefer-each): Enforce using `each` rather than manual loops
8888
╭─[prefer_each.tsx:7:11]
89-
6
89+
6
9090
7for (const [input, expected] of data) {
9191
· ──────────────────────────────────────
9292
8it(`results in ${expected}`, () => {

‎tasks/ast_tools/src/derives/mod.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,10 @@ macro_rules! define_derive {
7777
}
7878

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

8685
use $crate::derives::DeriveTemplate;
8786

@@ -91,7 +90,7 @@ macro_rules! define_derive {
9190
.into_iter()
9291
.filter(|def| def.generates_derive(trait_name))
9392
.map(|def| (def, self.derive(def, ctx)))
94-
.fold(FxHashMap::<&str, (HashSet<&str>, Vec<TokenStream>)>::default(), |mut acc, (def, stream)| {
93+
.fold(FxHashMap::<&str, (FxHashSet<&str>, Vec<TokenStream>)>::default(), |mut acc, (def, stream)| {
9594
let module_path = def.module_path();
9695
let krate = module_path.split("::").next().unwrap();
9796
if !acc.contains_key(krate) {

‎tasks/ast_tools/src/schema/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use quote::ToTokens;
2+
use rustc_hash::FxHashSet;
23
use serde::Serialize;
34

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

296297
fn parse_generate_derive(attrs: &[syn::Attribute]) -> Vec<String> {
297-
let mut derives = std::collections::HashSet::new();
298+
let mut derives = FxHashSet::default();
298299
for attr in attrs {
299300
if !attr.path().is_ident("generate_derive") {
300301
continue;

‎tasks/coverage/src/driver.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::{collections::HashSet, ops::ControlFlow, path::PathBuf};
1+
use std::{ops::ControlFlow, path::PathBuf};
22

33
use oxc::{
44
allocator::Allocator,
@@ -19,6 +19,7 @@ use oxc::{
1919
transformer::{TransformOptions, TransformerReturn},
2020
CompilerInterface,
2121
};
22+
use rustc_hash::FxHashSet;
2223

2324
use crate::suite::TestResult;
2425

@@ -146,7 +147,7 @@ impl Driver {
146147
}
147148

148149
fn check_comments(&mut self, trivias: &Trivias) -> bool {
149-
let mut uniq: HashSet<Span> = HashSet::new();
150+
let mut uniq: FxHashSet<Span> = FxHashSet::default();
150151
for comment in trivias.comments() {
151152
if !uniq.insert(comment.span) {
152153
self.errors

‎tasks/coverage/src/runtime/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use std::{
2-
collections::HashSet,
32
fs,
43
path::{Path, PathBuf},
54
time::Duration,
@@ -8,6 +7,7 @@ use std::{
87
use oxc::{allocator::Allocator, codegen::CodeGenerator, parser::Parser, span::SourceType};
98
use oxc_tasks_common::agent;
109
use phf::{phf_set, Set};
10+
use rustc_hash::FxHashSet;
1111
use serde_json::json;
1212

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

2121
lazy_static::lazy_static! {
22-
static ref V8_TEST_262_FAILED_TESTS: HashSet<String> = {
23-
let mut set = HashSet::default();
22+
static ref V8_TEST_262_FAILED_TESTS: FxHashSet<String> = {
23+
let mut set = FxHashSet::default();
2424
fs::read_to_string(workspace_root().join(V8_TEST_262_FAILED_TESTS_PATH))
2525
.expect("Failed to read v8_test262.status")
2626
.lines()

‎tasks/prettier_conformance/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,5 @@ oxc_span = { workspace = true }
3131
oxc_tasks_common = { workspace = true }
3232

3333
pico-args = { workspace = true }
34+
rustc-hash = { workspace = true }
3435
walkdir = { workspace = true }

‎tasks/prettier_conformance/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ mod ignore_list;
33
mod spec;
44

55
use std::{
6-
collections::HashSet,
76
fs,
87
path::{Path, PathBuf},
98
};
@@ -13,6 +12,7 @@ use oxc_parser::{ParseOptions, Parser};
1312
use oxc_prettier::{Prettier, PrettierOptions};
1413
use oxc_span::SourceType;
1514
use oxc_tasks_common::project_root;
15+
use rustc_hash::FxHashSet;
1616
use walkdir::WalkDir;
1717

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

116-
let dir_set: HashSet<_> = dirs.iter().cloned().collect();
116+
let dir_set: FxHashSet<_> = dirs.iter().cloned().collect();
117117
dirs = dir_set.into_iter().collect();
118118

119119
dirs.sort_unstable();

0 commit comments

Comments
 (0)
Please sign in to comment.