refactor(noUnusedPrivateClassMembers): avoid some allocations#7424
refactor(noUnusedPrivateClassMembers): avoid some allocations#7424
Conversation
|
WalkthroughRefactors the no-unused-private-class-members rule to replace FxHashSet-based aggregation with iterator- and Vec-based flows. get_all_declared_private_members and get_constructor_params now return iterators; traverse_members_usage accepts and returns Vec. run collects private members into a Vec, handles the empty case with Box::default(), and returns results via into_boxed_slice(). Private member discovery chains class members and constructor params using iterators. FxHashSet imports and collections are removed. Public function signatures are updated accordingly. Suggested labels
Suggested reviewers
✨ Finishing Touches
🧪 Generate unit tests
Comment |
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (3)
crates/biome_js_analyze/src/lint/correctness/no_unused_private_class_members.rs (3)
123-147: Pre-size results and prefer an explicit empty boxMinor allocation/readability nits: allocate
resultswith the exact capacity afterunused_membersis known, and useBox::new([])for clarity overBox::default().- let private_members: Vec<AnyMember> = get_all_declared_private_members(node).collect(); - if private_members.is_empty() { - Box::default() - } else { - let mut results = Vec::new(); - let unused_members = traverse_members_usage(node.syntax(), private_members); + let private_members: Vec<AnyMember> = get_all_declared_private_members(node).collect(); + if private_members.is_empty() { + Box::new([]) + } else { + let unused_members = traverse_members_usage(node.syntax(), private_members); + let mut results = Vec::with_capacity(unused_members.len());
248-303: Vec-based flow: confirm uniqueness and update the stale commentSwitching from a set to a Vec is fine here, but please confirm we never feed duplicates; a quick debug-only assert guards against accidental regressions. Also, the doc still says “hashmap”.
fn traverse_members_usage( syntax: &JsSyntaxNode, mut private_members: Vec<AnyMember>, ) -> Vec<AnyMember> { + #[cfg(debug_assertions)] + { + use std::collections::HashSet; + let mut seen = HashSet::new(); + assert!( + private_members + .iter() + .all(|m| seen.insert(m.syntax().text_range())), + "duplicate private members passed to traverse_members_usage" + ); + }Doc tweak (outside the selected range, near Lines 245–246):
-/// if the member usage is found, we remove it from the hashmap +/// if the member usage is found, we remove it from the working list
352-375: Slightly flatter iterator chain for readabilityEquivalent, but a tad easier to scan by grouping
map(...).into_iter().flatten()at the end.-fn get_constructor_params( - class_declaration: &JsClassDeclaration, -) -> impl Iterator<Item = AnyMember> { - class_declaration - .members() - .iter() - .find_map(|member| match member { - AnyJsClassMember::JsConstructorClassMember(member) => Some(member), - _ => None, - }) - .and_then(|constructor_member| constructor_member.parameters().ok()) - .into_iter() - .flat_map(|constructor_params| { - constructor_params - .parameters() - .iter() - .filter_map(|param| match param.ok()? { - biome_js_syntax::AnyJsConstructorParameter::TsPropertyParameter( - ts_property, - ) => Some(ts_property.into()), - _ => None, - }) - }) -} +fn get_constructor_params( + class_declaration: &JsClassDeclaration, +) -> impl Iterator<Item = AnyMember> { + class_declaration + .members() + .iter() + .find_map(|member| match member { + AnyJsClassMember::JsConstructorClassMember(member) => Some(member), + _ => None, + }) + .and_then(|c| c.parameters().ok()) + .map(|params| { + params + .parameters() + .iter() + .filter_map(|param| match param.ok()? { + biome_js_syntax::AnyJsConstructorParameter::TsPropertyParameter(p) => { + Some(p.into()) + } + _ => None, + }) + }) + .into_iter() + .flatten() +}
📜 Review details
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
crates/biome_js_analyze/src/lint/correctness/no_unused_private_class_members.rs(5 hunks)
🧰 Additional context used
📓 Path-based instructions (3)
**/*.{rs,toml}
📄 CodeRabbit inference engine (CONTRIBUTING.md)
Format Rust and TOML files before committing (use
just f/just format).
Files:
crates/biome_js_analyze/src/lint/correctness/no_unused_private_class_members.rs
crates/biome_*_{syntax,parser,formatter,analyze,factory,semantic}/**
📄 CodeRabbit inference engine (CLAUDE.md)
Maintain the per-language crate structure: biome_{lang}_{syntax,parser,formatter,analyze,factory,semantic}
Files:
crates/biome_js_analyze/src/lint/correctness/no_unused_private_class_members.rs
crates/biome_*/**
📄 CodeRabbit inference engine (CLAUDE.md)
Place core crates under /crates/biome_*/
Files:
crates/biome_js_analyze/src/lint/correctness/no_unused_private_class_members.rs
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (24)
- GitHub Check: Bench (biome_graphql_formatter)
- GitHub Check: Documentation
- GitHub Check: Bench (biome_package)
- GitHub Check: Bench (biome_html_formatter)
- GitHub Check: Bench (biome_configuration)
- GitHub Check: Lint project (depot-windows-2022)
- GitHub Check: Bench (biome_module_graph)
- GitHub Check: Test Node.js API
- GitHub Check: Bench (biome_html_parser)
- GitHub Check: Check Dependencies
- GitHub Check: Bench (biome_graphql_parser)
- GitHub Check: Bench (biome_json_parser)
- GitHub Check: Lint project (depot-ubuntu-24.04-arm-16)
- GitHub Check: Bench (biome_css_analyze)
- GitHub Check: Bench (biome_json_formatter)
- GitHub Check: Bench (biome_json_analyze)
- GitHub Check: Bench (biome_js_parser)
- GitHub Check: Bench (biome_css_parser)
- GitHub Check: Bench (biome_js_analyze)
- GitHub Check: Bench (biome_css_formatter)
- GitHub Check: Test (depot-windows-2022-16)
- GitHub Check: Bench (biome_js_formatter)
- GitHub Check: Test (depot-ubuntu-24.04-arm-16)
- GitHub Check: autofix
🔇 Additional comments (1)
crates/biome_js_analyze/src/lint/correctness/no_unused_private_class_members.rs (1)
343-350: Iterator-based discovery LGTMChaining members with constructor params and filtering late is clean and allocation-free up to collection. Nice touch.
CodSpeed Performance ReportMerging #7424 will not alter performanceComparing Summary
|
Summary
This PR removes some allocations by using iterators.
I also replaced the
FxHashSetwith aVecbecause it is not possible to collect twice the same node.Test Plan
Everything should be green.
Docs
No behavioral changes/