perf: hoist C# type-declaration Set to static readonly and use childForFieldName in Go receiver lookup#419
Open
github-actions[bot] wants to merge 2 commits into
Conversation
…orFieldName in Go receiver lookup
- csharpAnalyzer: move the inline `new Set([...])` in `getEnclosingTypeName` to a
`private static readonly` class field (TYPE_DECLARATION_TYPES). The set was
reallocated on every call to `getEnclosingTypeName`, which is invoked once per
method/accessor/operator in every C# file analysed. A static field is
allocated once at class load time and reused across all calls.
- goAnalyzer: replace the linear `.children.find(c => c.type === ...)` scan in
`findTypeInParameterList` with `child.childForFieldName("type")` — an O(1)
tree-sitter field lookup. This also removes the hard-coded list of receiver
type node kinds (type_identifier, pointer_type, qualified_type), making the
lookup correctly handle any type form the grammar may return (slice, map,
channel, etc.).
Both changes are pure performance micro-optimisations with no semantic effect;
all 153 unit tests pass.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…0627-9934802bccab092c
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #419 +/- ##
==========================================
- Coverage 78.34% 78.33% -0.02%
==========================================
Files 13 13
Lines 4156 4154 -2
Branches 469 466 -3
==========================================
- Hits 3256 3254 -2
Misses 897 897
Partials 3 3 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Contributor
There was a problem hiding this comment.
Pull request overview
This PR applies two small performance-focused refinements to the language analyzers used by the VS Code extension’s cognitive complexity engine, aiming to reduce per-node allocations in the C# analyzer and avoid linear child scans in the Go analyzer.
Changes:
- C#: Hoists the set of type-declaration node kinds into a
private static readonlyclass field to avoid re-allocating it on eachgetEnclosingTypeName()call. - Go: Uses
childForFieldName("type")when extracting receiver types, avoiding a manual.children.find(...)scan and broadening compatibility with more Go type forms.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| src/metricsAnalyzer/languages/csharpAnalyzer.ts | Hoists type-declaration node kind set to a static field for reuse across calls/instances. |
| src/metricsAnalyzer/languages/goAnalyzer.ts | Switches receiver type lookup to childForFieldName("type") instead of scanning children. |
Comment on lines
+281
to
+283
| // Use childForFieldName for O(1) field access rather than a linear | ||
| // scan over the parameter's children. | ||
| const typeNode = child.childForFieldName("type"); |
| */ | ||
| export class CSharpMetricsAnalyzer { | ||
| /** Node types that represent type declarations (class, struct, interface, record, enum). */ | ||
| private static readonly TYPE_DECLARATION_TYPES = new Set([ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
🤖 This PR was created by Repo Assist, an automated AI assistant.
Summary
Two targeted micro-optimisations across
csharpAnalyzer.tsandgoAnalyzer.tsthat eliminate per-call heap allocations and O(n) child scans.
1. C# — hoist
typeDeclarationsSet tostatic readonlygetEnclosingTypeName()is called once per C# method/accessor/operator foundduring analysis. It previously started every call with:
This allocates a new
Setobject (and re-inserts 5 strings into it) on everysingle invocation. Moving it to a
private static readonlyclass field(
TYPE_DECLARATION_TYPES) means the set is allocated once at class load timeand shared across all calls, all analyzer instances, and all files analysed
in a session.
2. Go — replace
.children.find()withchildForFieldName("type")in receiver lookupfindTypeInParameterList()extracted the receiver type from a method'sparameter list by scanning
parameter_declaration.childrenwith a hard-codedlist of accepted node kinds:
Replacing this with
child.childForFieldName("type")gives an O(1) tree-sitterfield lookup instead of an O(n) linear scan, and correctly handles any type form
the grammar may produce (slice types, map types, channel types, etc.) without
needing an explicit allowlist.
Test Status
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