From e8b0f17eabb0c5335344bcb02077ae2695585af4 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Mon, 27 Jul 2026 07:50:57 +0000 Subject: [PATCH] perf: avoid substring allocation in getBinaryOperator for C# && and || In tree-sitter-c-sharp, anonymous operator tokens such as && and || use their literal text as the node type. Reading node.type directly avoids a sourceText.substring() heap allocation on every binary logical expression. The previously matched 'binary_operator' case was dead code (no such token type is emitted by the grammar). Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- src/metricsAnalyzer/languages/csharpAnalyzer.ts | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/metricsAnalyzer/languages/csharpAnalyzer.ts b/src/metricsAnalyzer/languages/csharpAnalyzer.ts index a2a2fe0..a825f4d 100644 --- a/src/metricsAnalyzer/languages/csharpAnalyzer.ts +++ b/src/metricsAnalyzer/languages/csharpAnalyzer.ts @@ -756,15 +756,14 @@ export class CSharpMetricsAnalyzer { * @returns The operator string or null if not found */ private getBinaryOperator(node: Parser.SyntaxNode): string | null { - // binary_expression structure: [left, operator, right] — operator always at index 1 + // binary_expression structure: [left, operator, right] — operator always at index 1. + // In tree-sitter-c-sharp, anonymous operator tokens use their literal text as their node + // type (e.g. type === "&&"), so reading `.type` avoids a sourceText.substring allocation. const operatorNode = node.child(1); if (!operatorNode) { return null; } const type = operatorNode.type; - if (type === "&&" || type === "||" || type === "binary_operator") { - return this.sourceText.substring( - operatorNode.startIndex, - operatorNode.endIndex - ); + if (type === "&&" || type === "||") { + return type; } return null; }