Find code by asking what it does. A CLI for coding agents that uses Jev to discover relevant files and source context.
TypeScript ai-sdkclaude-codeclicode-searchcodexcoding-agentscontext-retrievaldeveloper-toolsjevsemantic-searchtypescriptvercel-ai-gateway
Find Rust attribute starts without quadratic sibling walks
taeskim0414b8e260e6
diff --git a/packages/core/src/parser-declarations.mjs b/packages/core/src/parser-declarations.mjsindex ff9c423..9010bfd 100644--- a/packages/core/src/parser-declarations.mjs+++ b/packages/core/src/parser-declarations.mjs@@ -43,15 +43,17 @@ export function declarations(root, language) {units.push({ name, range: range(node) });}else {- function startWithAttributes(node) {- let start = node;+ // `siblings` are the parent's named children and `index` is the node's position in them.+ // previousNamedSibling costs time proportional to the node's position, so walking back+ // through a long comment run with it was quadratic: 20,000 comment lines took 19 s.+ function startWithAttributes(siblings, index) {+ let start = index;while (- start.previousNamedSibling &&- (start.previousNamedSibling.type === "attribute_item" ||- comment(start.previousNamedSibling))+ start > 0 &&+ (siblings[start - 1].type === "attribute_item" || comment(siblings[start - 1]))) {- const previous = start.previousNamedSibling;- const before = previous.previousNamedSibling;+ const previous = siblings[start - 1];+ const before = start > 1 ? siblings[start - 2] : undefined;if (comment(previous) &&before &&@@ -60,16 +62,16 @@ export function declarations(root, language) {before.endPosition.row === previous.startPosition.row)break;- start = start.previousNamedSibling;+ start--;}- return range(start).startLine;+ return range(siblings[start]).startLine;}function visit(nodes, prefix = "", headers = []) {const ownedHeaders = [...headers,...nodes.filter((node) => node.type === "inner_attribute_item").map(range),];- for (const node of nodes) {+ for (const [index, node] of nodes.entries()) {if (comment(node) || ["attribute_item", "inner_attribute_item"].includes(node.type))continue;const body =@@ -80,7 +82,7 @@ export function declarations(root, language) {field(node, "type")?.text ||field(node, "macro")?.text ||node.type;- const startLine = startWithAttributes(node);+ const startLine = startWithAttributes(nodes, index);if (body &&body.namedChildren.some(diff --git a/test/parser/languages.test.ts b/test/parser/languages.test.tsindex 96d7365..b8c9fe2 100644--- a/test/parser/languages.test.ts+++ b/test/parser/languages.test.ts@@ -101,3 +101,19 @@ test("Go/Rust syntax and size fallbacks preserve every Unicode source byte", asyassert.ok(result.units.every((unit) => Buffer.byteLength(sourceForUnit(snapshot, unit)) <= 8));}});++test("Rust declarations after long comment runs keep their attributes and parse quickly", async () => {+ const notes = Array.from({ length: 20_000 }, (_, i) => `// note ${i}`).join("\n");+ const source = `${notes}\n#[inline]\n/// Docs.\nfn first() {}\n\nfn second() {} // trailing\n${notes}\nfn third() {}\n`;+ const started = performance.now();+ const result = await inspect(+ { path: "notes.rs", source, contentHash: "fixture" },+ { maxUnitBytes: Buffer.byteLength(source) },+ );+ // Walking back through the run was quadratic: 20,000 comment lines took 19 s.+ assert.ok(performance.now() - started < 5_000, "comment runs were not handled quickly");+ assert.deepEqual(+ result.units.map(({ name, range }) => `${name}@${range.startLine}-${range.endLine}`),+ ["first@1-20003", "second@20005-20005", "third@20006-40006"],+ );+});