perf: replace multi-comparison type chains with static readonly Sets#434
Conversation
Replace long or-chain type checks in six analyzers with static readonly Sets, consistent with the existing TYPE_DECLARATION_TYPES pattern in CSharpMetricsAnalyzer. The affected methods — isFunctionDeclaration, isMethodDeclaration, isNestedFunction, and increasesNesting — are called on every node during AST traversal, so converting from O(n) sequential string comparisons to O(1) Set.has() lookups reduces per-node overhead on every analysed file. Changes by file: - csharpAnalyzer.ts : FUNCTION_DECLARATION_TYPES (7 entries), NESTING_TYPES (9 entries) - javaAnalyzer.ts : METHOD_DECLARATION_TYPES (2 entries), NESTING_TYPES (8 entries) - goAnalyzer.ts : NESTING_TYPES (6 entries) - jsLikeAnalyzer.ts : FUNCTION_NODE_TYPES (4 entries), NESTED_FUNCTION_TYPES (3 entries), NESTING_TYPES (7 entries) - pythonAnalyzer.ts : NESTING_TYPES (9 entries, replaces switch/case) - rustAnalyzer.ts : NESTING_TYPES (6 entries) No logic changes; all 159 unit tests continue to pass. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
|
@copilot resolve the merge conflicts in this pull request |
…ith Set lookups Co-authored-by: askpt <2493377+askpt@users.noreply.github.com>
Merge conflicts resolved in Resolution: kept the Set-based approach and added the new generator types to both |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #434 +/- ##
==========================================
+ Coverage 78.62% 78.76% +0.14%
==========================================
Files 13 13
Lines 4211 4239 +28
Branches 474 418 -56
==========================================
+ Hits 3311 3339 +28
Misses 898 898
Partials 2 2 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Pull request overview
This PR optimizes the cognitive complexity “hot path” in the language analyzers by replacing repeated string OR-comparisons (and one switch) with static readonly Set membership checks, aligning with the existing TYPE_DECLARATION_TYPES pattern in the C# analyzer.
Changes:
- Introduced
static readonlyReadonlySet<string>constants for frequently-checked node-type groups across analyzers. - Updated
increasesNestingand function/method identification helpers to useSet.has(node.type)instead of comparison chains. - Kept behavior consistent with existing logic while improving per-node traversal overhead.
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| src/metricsAnalyzer/languages/csharpAnalyzer.ts | Adds FUNCTION_DECLARATION_TYPES/NESTING_TYPES sets and switches checks to Set.has() |
| src/metricsAnalyzer/languages/javaAnalyzer.ts | Adds method/nesting type sets and replaces OR-chains with Set.has() |
| src/metricsAnalyzer/languages/goAnalyzer.ts | Adds NESTING_TYPES and updates nesting check to Set.has() |
| src/metricsAnalyzer/languages/jsLikeAnalyzer.ts | Adds function/nested-function/nesting sets and replaces OR-chains with Set.has() |
| src/metricsAnalyzer/languages/pythonAnalyzer.ts | Adds NESTING_TYPES and replaces switch-based nesting logic with Set.has() |
| src/metricsAnalyzer/languages/rustAnalyzer.ts | Adds NESTING_TYPES and updates nesting check to Set.has() |
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
🤖 This is an automated pull request from Repo Assist.
Summary
Replaces multi-step OR-chain type checks in six language analyzers with
static readonly Setlookups, improving per-node performance on the analysis hot path.This pattern was already established by
TYPE_DECLARATION_TYPESinCSharpMetricsAnalyzer— this PR extends it consistently to all analyzers.Changes
csharpAnalyzer.tsFUNCTION_DECLARATION_TYPES(7),NESTING_TYPES(9)isFunctionDeclaration,increasesNestingjavaAnalyzer.tsMETHOD_DECLARATION_TYPES(2),NESTING_TYPES(8)isMethodDeclaration,increasesNestinggoAnalyzer.tsNESTING_TYPES(6)increasesNestingjsLikeAnalyzer.tsFUNCTION_NODE_TYPES(4),NESTED_FUNCTION_TYPES(3),NESTING_TYPES(7)isFunctionNode,isNestedFunction,increasesNestingpythonAnalyzer.tsNESTING_TYPES(9)increasesNesting(replaces switch/case)rustAnalyzer.tsNESTING_TYPES(6)increasesNestingNo logic changes; all existing tests continue to pass.
Rationale
isFunctionDeclaration,isMethodDeclaration,isNestedFunction, andincreasesNestingare called on every AST node during traversal. Converting sequential OR-chain string comparisons toSet.has()reduces per-node overhead on every analysed file — the benefit scales with file size and the number of node types in each chain (up to 9 comparisons replaced by a single hash lookup).Test Status
No new tests needed — no logic changes.
Warning
Firewall blocked 1 domain
The following domain was blocked by the firewall during workflow execution:
releaseassets.githubusercontent.comSee Network Configuration for more information.
Add this agentic workflows to your repo
To install this agentic workflow, run