From dbec361e6b20bb2978a02ca3e2c25b066fe854c6 Mon Sep 17 00:00:00 2001 From: can1357 Date: Tue, 10 Feb 2026 16:42:49 +0100 Subject: [PATCH] perf(coding-agent): optimized hash computation and simplified conditional logic across tools - Optimized line hash computation by precomputing hex lookup table and using bitwise operations instead of string formatting. - Simplified conditional logic in read tool for hash and line number configuration. - Refactored react-edit-benchmark model/provider parsing to use concise ternary operator. --- packages/coding-agent/src/patch/hashline.ts | 4 +++- packages/coding-agent/src/tools/read.ts | 4 ++-- packages/react-edit-benchmark/index.ts | 18 ++++++------------ 3 files changed, 11 insertions(+), 15 deletions(-) diff --git a/packages/coding-agent/src/patch/hashline.ts b/packages/coding-agent/src/patch/hashline.ts index 450facd51..54af96c95 100644 --- a/packages/coding-agent/src/patch/hashline.ts +++ b/packages/coding-agent/src/patch/hashline.ts @@ -14,6 +14,8 @@ import type { HashlineEdit, HashMismatch } from "./types"; +const HEX3 = Array.from({ length: 0xfff + 1 }, (_, i) => i.toString(16).padStart(3, "0")); + /** * Compute the 4-character hex hash of a single line. * @@ -23,7 +25,7 @@ import type { HashlineEdit, HashMismatch } from "./types"; * The line input should not include a trailing newline. */ export function computeLineHash(idx: number, line: string): string { - return Bun.hash.xxHash64(line, BigInt(idx)).toString(16).padStart(16, "0").slice(0, 4); + return HEX3[Number(Bun.hash.xxHash64(line, BigInt(idx))) & 0xfff]; } /** diff --git a/packages/coding-agent/src/tools/read.ts b/packages/coding-agent/src/tools/read.ts index 676fbcc9f..ec25762b9 100644 --- a/packages/coding-agent/src/tools/read.ts +++ b/packages/coding-agent/src/tools/read.ts @@ -760,8 +760,8 @@ export class ReadTool implements AgentTool { maxBytes: DEFAULT_MAX_BYTES, }; - const shouldAddHashLines = hashes ?? this.#defaultHashLines; - const shouldAddLineNumbers = shouldAddHashLines ? false : (lines ?? this.#defaultLineNumbers); + const shouldAddHashLines = hashes || this.#defaultHashLines; + const shouldAddLineNumbers = lines || this.#defaultLineNumbers; const prependLineNumbers = (text: string, startNum: number): string => { const textLines = text.split("\n"); const lastLineNum = startNum + textLines.length - 1; diff --git a/packages/react-edit-benchmark/index.ts b/packages/react-edit-benchmark/index.ts index e3b702646..4cb96289a 100644 --- a/packages/react-edit-benchmark/index.ts +++ b/packages/react-edit-benchmark/index.ts @@ -157,18 +157,12 @@ async function main(): Promise { allowPositionals: true, }); - // Auto-detect provider from model string: "provider/model" -> provider="provider", model="model" - let provider = values.provider; - let model = values.model!; - if (!provider) { - const slashIndex = model.indexOf("/"); - if (slashIndex !== -1) { - provider = model.slice(0, slashIndex); - model = model.slice(slashIndex + 1); - } else { - provider = "anthropic"; - } - } + // Extract provider for display/config purposes only. + // The full model string (e.g. "openrouter/google/gemini-2.5-flash-lite") is passed + // as --model to the CLI, which handles resolution via parseModelPattern. + const model = values.model!; + const slashIndex = model.indexOf("/"); + const provider = values.provider ?? (slashIndex !== -1 ? model.slice(0, slashIndex) : "anthropic"); if (values.help) { printUsage();