From 4ca807271bcd2db0c1209284ecf2bd009f58bf53 Mon Sep 17 00:00:00 2001 From: Gergo Magyar Date: Mon, 26 Oct 2026 12:12:40 +0000 Subject: [PATCH] perf(ingestion): guard callable scaling and reuse export checks --- .../bench/method-declaration-facts/README.md | 32 +- .../method-declaration-facts/baseline.json | 407 ++++++++++++++++-- .../method-declaration-facts/measure.mjs | 196 +++++++-- .../method-declaration-facts/scaling.mjs | 28 ++ .../src/core/ingestion/export-detection.ts | 30 +- gitnexus/test/integration/parsing.test.ts | 48 ++- .../unit/method-declaration-scaling.test.ts | 70 +++ 7 files changed, 716 insertions(+), 95 deletions(-) create mode 100644 gitnexus/bench/method-declaration-facts/scaling.mjs create mode 100644 gitnexus/test/unit/method-declaration-scaling.test.ts diff --git a/gitnexus/bench/method-declaration-facts/README.md b/gitnexus/bench/method-declaration-facts/README.md index 986fa2647..16bf844ea 100644 --- a/gitnexus/bench/method-declaration-facts/README.md +++ b/gitnexus/bench/method-declaration-facts/README.md @@ -10,12 +10,40 @@ node --import tsx bench/method-declaration-facts/measure.mjs --check --output me `--check` compares the current build with committed `baseline.json`; no second checkout is required. It checks all eight providers' semantic fingerprints and -shared parameter/type-array counts, cold/warm/changed graph and cache counts on -fixed declaration and provider corpora, and tree collection. Three fresh-process +shared parameter/type-array counts, cold/warm/changed graph and cache counts at +each workload size, and tree collection. Three fresh-process samples report median wall/CPU time, endpoint heap, peak RSS, and retained heap, with percentage differences from the recorded measurements. Timing and heap/RSS are machine-dependent references, not absolute CI failure thresholds. +Like the C# pipeline benchmark, this check increases input size and compares cost +growth with input growth. It uses the stricter **1.5** normalized growth budget +already used by the Python scope benchmark: + +```text +normalized growth = (large cost / small cost) / (large input / small input) +``` + +| Workload | Sizes | Gated costs | +| --- | --- | --- | +| Java + TypeScript, spread across files | 8 / 32 / 128 files per language, 40 methods per class | Cold/warm/changed wall time, CPU, endpoint heap, sampled peak RSS and process maximum RSS | +| Java + TypeScript, concentrated in one class per language | 320 / 1,280 / 5,120 methods per class | Same full-indexing measurements | +| Retained declaration metadata, separately for Java and TypeScript | 2,500 / 10,000 / 40,000 declarations | Extraction time, extraction retained heap, emitted retained heap | + +Both indexing shapes contain 640 / 2,560 / 10,240 generated methods, plus fixed +call and edit probes. The harness verifies the emitted method and parsed/cache-hit +file counts, so silently dropping work cannot improve a scaling result. Size order +alternates between repetitions. Every adjacent pair **and the full size span** must +stay within the budget, using per-size medians from the same run. Missing, zero, +negative or non-finite measurements fail. `--record` also enforces these guards. +Unit tests demonstrate that quadratic time or memory growth fails the check. + +The artifact includes raw samples, median costs and normalized ratios. These +bounded workloads guard against scaling regressions; they do not prove linear +complexity for every repository. Fixed startup costs can mask growth at small +sizes. Released heap deltas remain diagnostic because they can be zero or negative +after collection. + CI uploads raw samples and comparisons as `method-declaration-facts-benchmark`, using the same five-day retention as the other benchmark artifacts. The separate all-language [scope-resolution benchmark](../scope-resolution/README.md) remains diff --git a/gitnexus/bench/method-declaration-facts/baseline.json b/gitnexus/bench/method-declaration-facts/baseline.json index 0d3597eea..5f6e0f671 100644 --- a/gitnexus/bench/method-declaration-facts/baseline.json +++ b/gitnexus/bench/method-declaration-facts/baseline.json @@ -1,5 +1,5 @@ { - "_note": "Reviewed semantic and allocation baseline. --check compares fingerprints, cache counts and shared-array counts; timing and heap/RSS medians are references, not machine-independent thresholds. CI never records a baseline.", + "_note": "Reviewed semantic and allocation baseline. --check compares fingerprints, cache counts and shared-array counts and gates same-run normalized time/memory growth at 1.5. Absolute timing and heap/RSS medians are references. CI never records a baseline.", "environment": { "node": "v22.23.3", "platform": "linux", @@ -58,6 +58,44 @@ } ], "analysis": { + "declarations-8": { + "manifest": { + "files": 17, + "manifest": "353a6948d690eaba364c8d31df4937d1b969e855ccb7bed1ded1c95b6a528847" + }, + "runs": [ + { + "scenario": "cold", + "facts": { + "nodes": 691, + "edges": 690, + "hash": "c2918b76eea971e5d925afd2e912c53ce372da6abfd38ba4adcccade22c07ef9" + }, + "reparsed": 17, + "hits": 0 + }, + { + "scenario": "warm", + "facts": { + "nodes": 691, + "edges": 690, + "hash": "c2918b76eea971e5d925afd2e912c53ce372da6abfd38ba4adcccade22c07ef9" + }, + "reparsed": 0, + "hits": 17 + }, + { + "scenario": "changed", + "facts": { + "nodes": 691, + "edges": 690, + "hash": "e220495c5c40fba7820c452b44c6ced2302981e60a7428eb4bb73ec628d7daa8" + }, + "reparsed": 1, + "hits": 16 + } + ] + }, "declarations": { "manifest": { "files": 65, @@ -96,6 +134,158 @@ } ] }, + "declarations-128": { + "manifest": { + "files": 257, + "manifest": "b9625a8c2fe98276a16847c4d77f9e4c46cd696a61d555440d7840faf0107780" + }, + "runs": [ + { + "scenario": "cold", + "facts": { + "nodes": 11011, + "edges": 11010, + "hash": "11fbfb9fdf42fb9ac78c11e359fb9d7a6aea448887a65650b9d54e039f3e73a2" + }, + "reparsed": 257, + "hits": 0 + }, + { + "scenario": "warm", + "facts": { + "nodes": 11011, + "edges": 11010, + "hash": "11fbfb9fdf42fb9ac78c11e359fb9d7a6aea448887a65650b9d54e039f3e73a2" + }, + "reparsed": 0, + "hits": 257 + }, + { + "scenario": "changed", + "facts": { + "nodes": 11011, + "edges": 11010, + "hash": "88a8baaa628aa037c02c83b6d912ef9ca206fd63c035a284dc55a9c3f1ed4903" + }, + "reparsed": 1, + "hits": 256 + } + ] + }, + "concentrated-320": { + "manifest": { + "files": 3, + "manifest": "b2bcb40fa14120ac4ada96e78569ffa2413250ea3009dbf39b7d4d4c1bb533da" + }, + "runs": [ + { + "scenario": "cold", + "facts": { + "nodes": 649, + "edges": 648, + "hash": "4606ff07912fef5a01f2b057e6b9d9bc5df3cc3a40295fd558fca8d45df9527e" + }, + "reparsed": 3, + "hits": 0 + }, + { + "scenario": "warm", + "facts": { + "nodes": 649, + "edges": 648, + "hash": "4606ff07912fef5a01f2b057e6b9d9bc5df3cc3a40295fd558fca8d45df9527e" + }, + "reparsed": 0, + "hits": 3 + }, + { + "scenario": "changed", + "facts": { + "nodes": 649, + "edges": 648, + "hash": "562dc4337b5840e0a64c1b3f4d4f90cc1f2fd75a49ccb23a50b7c6adcbf3fd07" + }, + "reparsed": 1, + "hits": 2 + } + ] + }, + "concentrated-1280": { + "manifest": { + "files": 3, + "manifest": "59d63e1f0a4b92388b4b9c9008e90338d50839ebc7c8779c86169f1adac57a06" + }, + "runs": [ + { + "scenario": "cold", + "facts": { + "nodes": 2569, + "edges": 2568, + "hash": "ad17f7f24a6948d140ccfdca0230f9562a3e44717a5931a29ae62af504899041" + }, + "reparsed": 3, + "hits": 0 + }, + { + "scenario": "warm", + "facts": { + "nodes": 2569, + "edges": 2568, + "hash": "ad17f7f24a6948d140ccfdca0230f9562a3e44717a5931a29ae62af504899041" + }, + "reparsed": 0, + "hits": 3 + }, + { + "scenario": "changed", + "facts": { + "nodes": 2569, + "edges": 2568, + "hash": "90625066c2e3ef78881ef820b573f5a1201cb5b50b96a3a5d9bb4a6572dbeb5a" + }, + "reparsed": 1, + "hits": 2 + } + ] + }, + "concentrated-5120": { + "manifest": { + "files": 3, + "manifest": "8b2fb11e9b2a4ba1c124c1efdb22e55db91b8595e1a519f55da75e94af0a5ff1" + }, + "runs": [ + { + "scenario": "cold", + "facts": { + "nodes": 10249, + "edges": 10248, + "hash": "f1f7ef339c2ba2f15f1c38fc9c7caffc0d5d92a222059cc21352a17fb30a2879" + }, + "reparsed": 3, + "hits": 0 + }, + { + "scenario": "warm", + "facts": { + "nodes": 10249, + "edges": 10248, + "hash": "f1f7ef339c2ba2f15f1c38fc9c7caffc0d5d92a222059cc21352a17fb30a2879" + }, + "reparsed": 0, + "hits": 3 + }, + { + "scenario": "changed", + "facts": { + "nodes": 10249, + "edges": 10248, + "hash": "4b521b01aef30d967ea1ce113f8021a1f2c30319ef9102f303986c77390b4c27" + }, + "reparsed": 1, + "hits": 2 + } + ] + }, "providers": { "manifest": { "files": 9, @@ -136,69 +326,216 @@ } }, "memory": { + "java-2500": { + "digest": "5d72a942d521e860c36563aceb57cc32d17857273a18d0d428b4266bd617e06f", + "declarations": 2500 + }, "java": { "digest": "44e5fc1e221e02a9644075284d18b3936d3afc36b1ff696341f7ab5a4a5b15b0", "declarations": 10000 }, + "java-40000": { + "digest": "bb0f0b9a3281108e1aa910478e8f9171f0e375e96bf527e7020f46bb5e04cba3", + "declarations": 40000 + }, + "typescript-2500": { + "digest": "14e9b13d31e8874849cfd64f2367716b09ed9d6577912f75318f358bdc8c741d", + "declarations": 2500 + }, "typescript": { "digest": "9e1ae4a2165a63a3f89aa97d474e799ffc18d09cb47578b5a112a7ed75c54e40", "declarations": 10000 + }, + "typescript-40000": { + "digest": "632331d244854ab84e4974e4ea7ef669460e3acc5e97e933cb822ca0e39844c2", + "declarations": 40000 } }, "collectedTrees": 100 }, "measured": { + "declarations-8/cold": { + "wallMs": 4598.0965830000005, + "cpuMs": 5028.881, + "heapUsedMiB": 69.43971252441406, + "sampledPeakRssMiB": 552.4375, + "cumulativeMaxRssMiB": 552.4375 + }, + "declarations-8/warm": { + "wallMs": 1469.5753610000002, + "cpuMs": 1310.647, + "heapUsedMiB": 100.86992645263672, + "sampledPeakRssMiB": 639.1484375, + "cumulativeMaxRssMiB": 639.1484375 + }, + "declarations-8/changed": { + "wallMs": 2224.902616000001, + "cpuMs": 2158.139, + "heapUsedMiB": 70.97301483154297, + "sampledPeakRssMiB": 691.4453125, + "cumulativeMaxRssMiB": 691.4453125 + }, "declarations/cold": { - "wallMs": 9491.255859, - "cpuMs": 12413.021, - "heapUsedMiB": 100.68425750732422, - "sampledPeakRssMiB": 562.76171875, - "cumulativeMaxRssMiB": 568.734375 + "wallMs": 6287.4714890000005, + "cpuMs": 7338.656, + "heapUsedMiB": 102.16807556152344, + "sampledPeakRssMiB": 568.0625, + "cumulativeMaxRssMiB": 574.22265625 }, "declarations/warm": { - "wallMs": 2825.0413579999986, - "cpuMs": 3197.422, - "heapUsedMiB": 106.1380615234375, - "sampledPeakRssMiB": 670.35546875, - "cumulativeMaxRssMiB": 676.64453125 + "wallMs": 1898.1291850000007, + "cpuMs": 2115.823, + "heapUsedMiB": 107.16117858886719, + "sampledPeakRssMiB": 672.890625, + "cumulativeMaxRssMiB": 679.06640625 }, "declarations/changed": { - "wallMs": 3547.4335059999994, - "cpuMs": 3785.397, - "heapUsedMiB": 100.31853485107422, - "sampledPeakRssMiB": 738.78125, - "cumulativeMaxRssMiB": 744.953125 + "wallMs": 2573.0706090000003, + "cpuMs": 2741.011, + "heapUsedMiB": 101.39332580566406, + "sampledPeakRssMiB": 741.0625, + "cumulativeMaxRssMiB": 747.2890625 + }, + "declarations-128/cold": { + "wallMs": 11950.934368, + "cpuMs": 15088.994, + "heapUsedMiB": 96.21513366699219, + "sampledPeakRssMiB": 642.6796875, + "cumulativeMaxRssMiB": 642.8671875 + }, + "declarations-128/warm": { + "wallMs": 3071.7164410000005, + "cpuMs": 3799.132, + "heapUsedMiB": 151.96864318847656, + "sampledPeakRssMiB": 752.5546875, + "cumulativeMaxRssMiB": 758.421875 + }, + "declarations-128/changed": { + "wallMs": 3785.085203999999, + "cpuMs": 4623.536, + "heapUsedMiB": 165.27832794189453, + "sampledPeakRssMiB": 825.6484375, + "cumulativeMaxRssMiB": 832.015625 + }, + "concentrated-320/cold": { + "wallMs": 4694.445621, + "cpuMs": 4989.052, + "heapUsedMiB": 67.74372100830078, + "sampledPeakRssMiB": 543.921875, + "cumulativeMaxRssMiB": 543.921875 + }, + "concentrated-320/warm": { + "wallMs": 1478.7918250000002, + "cpuMs": 1216.909, + "heapUsedMiB": 98.2383804321289, + "sampledPeakRssMiB": 634.32421875, + "cumulativeMaxRssMiB": 634.32421875 + }, + "concentrated-320/changed": { + "wallMs": 2217.1457689999997, + "cpuMs": 2067.268, + "heapUsedMiB": 69.39957427978516, + "sampledPeakRssMiB": 703.64453125, + "cumulativeMaxRssMiB": 703.64453125 + }, + "concentrated-1280/cold": { + "wallMs": 8088.226249, + "cpuMs": 9146.62, + "heapUsedMiB": 110.2262191772461, + "sampledPeakRssMiB": 601.5078125, + "cumulativeMaxRssMiB": 602.08203125 + }, + "concentrated-1280/warm": { + "wallMs": 1780.5142610000003, + "cpuMs": 1871.551, + "heapUsedMiB": 116.76529693603516, + "sampledPeakRssMiB": 714.1328125, + "cumulativeMaxRssMiB": 714.75390625 + }, + "concentrated-1280/changed": { + "wallMs": 2454.9453699999995, + "cpuMs": 2596.301, + "heapUsedMiB": 117.36157989501953, + "sampledPeakRssMiB": 772.80078125, + "cumulativeMaxRssMiB": 772.80078125 + }, + "concentrated-5120/cold": { + "wallMs": 19038.470589, + "cpuMs": 21988.894, + "heapUsedMiB": 87.05318450927734, + "sampledPeakRssMiB": 806.7421875, + "cumulativeMaxRssMiB": 807.34765625 + }, + "concentrated-5120/warm": { + "wallMs": 2599.4626009999993, + "cpuMs": 3164.342, + "heapUsedMiB": 196.42921447753906, + "sampledPeakRssMiB": 931.328125, + "cumulativeMaxRssMiB": 931.328125 + }, + "concentrated-5120/changed": { + "wallMs": 3300.2307550000005, + "cpuMs": 3837.783, + "heapUsedMiB": 198.03103637695312, + "sampledPeakRssMiB": 1009.9921875, + "cumulativeMaxRssMiB": 1009.9921875 }, "providers/cold": { - "wallMs": 6830.893146, - "cpuMs": 7177.582, - "heapUsedMiB": 64.3858413696289, - "sampledPeakRssMiB": 596.99609375, - "cumulativeMaxRssMiB": 602.86328125 + "wallMs": 5234.342168, + "cpuMs": 5416.471, + "heapUsedMiB": 64.36933135986328, + "sampledPeakRssMiB": 581.26953125, + "cumulativeMaxRssMiB": 587.6796875 }, "providers/warm": { - "wallMs": 2304.49326, - "cpuMs": 2281.247, - "heapUsedMiB": 80.3407211303711, - "sampledPeakRssMiB": 693.53515625, - "cumulativeMaxRssMiB": 699.69140625 + "wallMs": 1649.076336, + "cpuMs": 1537.857, + "heapUsedMiB": 80.01580810546875, + "sampledPeakRssMiB": 682.64453125, + "cumulativeMaxRssMiB": 688.83984375 }, "providers/changed": { - "wallMs": 3418.0368609999987, - "cpuMs": 3310.594, - "heapUsedMiB": 64.62885284423828, - "sampledPeakRssMiB": 748.1953125, - "cumulativeMaxRssMiB": 754.38671875 + "wallMs": 2417.3744080000006, + "cpuMs": 2417.786, + "heapUsedMiB": 65.51468658447266, + "sampledPeakRssMiB": 764.375, + "cumulativeMaxRssMiB": 770.55859375 + }, + "java-2500/retained": { + "extractionMs": 97.82188899999998, + "extractionRetainedBytes": 3176480, + "emittedRetainedBytes": 1596952, + "releasedDeltaBytes": 332792 }, "java/retained": { - "extractionRetainedBytes": 12057128, + "extractionMs": 378.996999, + "extractionRetainedBytes": 12057136, "emittedRetainedBytes": 5701312, "releasedDeltaBytes": 660560 }, + "java-40000/retained": { + "extractionMs": 1499.86337, + "extractionRetainedBytes": 47273448, + "emittedRetainedBytes": 21694640, + "releasedDeltaBytes": 1447192 + }, + "typescript-2500/retained": { + "extractionMs": 116.37295999999999, + "extractionRetainedBytes": 3198712, + "emittedRetainedBytes": 1619784, + "releasedDeltaBytes": 355624 + }, "typescript/retained": { - "extractionRetainedBytes": 12017312, - "emittedRetainedBytes": 5662200, - "releasedDeltaBytes": 621448 + "extractionMs": 472.583266, + "extractionRetainedBytes": 12017024, + "emittedRetainedBytes": 5662008, + "releasedDeltaBytes": 621256 + }, + "typescript-40000/retained": { + "extractionMs": 1857.2782389999998, + "extractionRetainedBytes": 47241792, + "emittedRetainedBytes": 21663784, + "releasedDeltaBytes": 1416336 } } } diff --git a/gitnexus/bench/method-declaration-facts/measure.mjs b/gitnexus/bench/method-declaration-facts/measure.mjs index 396b83f08..5db69134e 100644 --- a/gitnexus/bench/method-declaration-facts/measure.mjs +++ b/gitnexus/bench/method-declaration-facts/measure.mjs @@ -9,6 +9,7 @@ import path from 'node:path'; import { performance, PerformanceObserver } from 'node:perf_hooks'; import { createRequire } from 'node:module'; import { fileURLToPath, pathToFileURL } from 'node:url'; +import { evaluateScaling } from './scaling.mjs'; const here = fileURLToPath(import.meta.url); const candidate = path.resolve(path.dirname(here), '../..'); @@ -193,6 +194,7 @@ async function retainedTrees() { const Parser = createRequire(path.join(root, 'package.json'))('tree-sitter'); const { getLanguageGrammar } = await load('core/tree-sitter/parser-loader.js'); const { javaMethodConfig } = await load('core/ingestion/method-extractors/configs/jvm.js'); + const { javaExportChecker } = await load('core/ingestion/export-detection.js'); const refs = (() => { const parser = new Parser(); parser.setLanguage(getLanguageGrammar('java')); @@ -201,6 +203,7 @@ async function retainedTrees() { const node = tree.rootNode.descendantsOfType('method_declaration')[0]; javaMethodConfig.extractParameters(node); javaMethodConfig.extractParameters(node); + assert.equal(javaExportChecker(node.childForFieldName('name'), 'f'), false); return new WeakRef(tree); }); })(); @@ -224,13 +227,17 @@ async function makeCorpus(repo, corpus) { await writeFile(path.join(repo, file), source + '\n'); } } else if (corpus === 'declarations') { - for (let file = 0; file < 32; file++) { + const files = Number(option('--files', '32')); + const methods = Number(option('--methods', '40')); + assert(Number.isSafeInteger(files) && files > 0); + assert(Number.isSafeInteger(methods) && methods > 0); + for (let file = 0; file < files; file++) { const ts = Array.from( - { length: 40 }, + { length: methods }, (_, i) => `m${i}(a: number, b: string, c: Array, d = 0): number { return a + d; }`, ).join('\n'); const java = Array.from( - { length: 40 }, + { length: methods }, (_, i) => `int m${i}(int a, String b, List c, int d) { return a + d; }`, ).join('\n'); await writeFile( @@ -378,6 +385,16 @@ async function measureAnalysis(corpus) { assert(pipeline.usedWorkerPool, 'cache misses must use real parser workers'); if (scenario === 'warm') assert.equal(pipeline.reparsedFileCount, 0); if (scenario === 'changed') assert(pipeline.reparsedFileCount > 0); + if (corpus === 'declarations') { + const files = Number(option('--files', '32')); + const methods = Number(option('--methods', '40')); + let declarations = 0; + pipeline.graph.forEachNode((node) => { + if (node.label === 'Method' && /^m\d+$/.test(node.properties.name)) declarations++; + }); + assert.equal(declarations, 2 * files * methods, 'Scaling workload lost declarations'); + assert.equal(pipeline.reparsedFileCount + pipeline.parseCacheHitFileCount, 2 * files + 1); + } if (corpus === 'providers') { if (scenario === 'cold') { assert.equal(pipeline.reparsedFileCount, Object.keys(providerFixtures).length + 1); @@ -421,7 +438,7 @@ async function measureAnalysis(corpus) { } } -async function child(revision, mode, corpus) { +async function child(revision, mode, corpus, extraArgs = []) { const cache = await mkdtemp(path.join(os.tmpdir(), 'gitnexus-callable-identity-')); try { const memory = mode === 'memory'; @@ -435,8 +452,19 @@ async function child(revision, mode, corpus) { revision, '--language', corpus, + ...extraArgs, ] - : ['--expose-gc', here, '--child', mode, '--root', revision, '--corpus', corpus ?? ''], + : [ + '--expose-gc', + here, + '--child', + mode, + '--root', + revision, + '--corpus', + corpus ?? '', + ...extraArgs, + ], { encoding: 'utf8', timeout: 600_000, @@ -487,9 +515,17 @@ async function checkBenchmark() { trials: {}, memory: {}, measured: {}, + scaling: {}, + }; + const median = (rows, key) => { + assert( + rows.every( + (row) => Number.isFinite(row[key]) && (key === 'releasedDeltaBytes' || row[key] > 0), + ), + `Invalid ${key} sample`, + ); + return rows.map((row) => row[key]).sort((a, b) => a - b)[Math.floor(rows.length / 2)]; }; - const median = (rows, key) => - [...rows].map((row) => row[key]).sort((a, b) => a - b)[Math.floor(rows.length / 2)]; const summarize = (rows, keys) => Object.fromEntries(keys.map((key) => [key, median(rows, key)])); try { report.counts = await child(root, 'counts'); @@ -515,13 +551,44 @@ async function checkBenchmark() { memory: {}, collectedTrees: report.lifetime.collected, }; - for (const corpus of ['declarations', 'providers']) { - const trials = []; - report.trials[corpus] = trials; - for (let trial = 0; trial < samples; trial++) { - console.log(`${corpus} ${trial + 1}/${samples}`); - trials.push(await child(root, 'analysis', corpus)); + const workloads = [ + ...[8, 32, 128].map((files) => ({ + name: files === 32 ? 'declarations' : `declarations-${files}`, + shape: 'spread', + files, + methods: 40, + corpus: 'declarations', + })), + ...[320, 1280, 5120].map((methods) => ({ + name: `concentrated-${methods}`, + shape: 'concentrated', + files: 1, + methods, + corpus: 'declarations', + })), + { name: 'providers', corpus: 'providers' }, + ]; + report.workloads = workloads; + for (const { name } of workloads) report.trials[name] = []; + for (let trial = 0; trial < samples; trial++) { + for (const { name, corpus, files, methods } of trial % 2 + ? [...workloads].reverse() + : workloads) { + console.log(`${name} ${trial + 1}/${samples}`); + const extraArgs = + corpus === 'declarations' ? ['--files', String(files), '--methods', String(methods)] : []; + report.trials[name].push(await child(root, 'analysis', corpus, extraArgs)); } + } + const analysisMetrics = [ + 'wallMs', + 'cpuMs', + 'heapUsedMiB', + 'sampledPeakRssMiB', + 'cumulativeMaxRssMiB', + ]; + for (const { name } of workloads) { + const trials = report.trials[name]; const facts = (trial) => ({ manifest: trial.manifest, runs: trial.runs.map(({ scenario, facts, reparsed, hits }) => ({ @@ -531,47 +598,90 @@ async function checkBenchmark() { hits, })), }); - correctness.analysis[corpus] = facts(trials[0]); + correctness.analysis[name] = facts(trials[0]); for (const trial of trials) { assert.deepEqual( facts(trial), - correctness.analysis[corpus], - `${corpus}: nondeterministic graph or cache replay`, + correctness.analysis[name], + `${name}: nondeterministic graph or cache replay`, ); assert.equal(trial.runs[2].reparsed, 1); } for (const scenario of ['cold', 'warm', 'changed']) { - report.measured[`${corpus}/${scenario}`] = summarize( + report.measured[`${name}/${scenario}`] = summarize( trials.map((trial) => trial.runs.find((run) => run.scenario === scenario)), - ['wallMs', 'cpuMs', 'heapUsedMiB', 'sampledPeakRssMiB', 'cumulativeMaxRssMiB'], + analysisMetrics, ); } } + for (const shape of ['spread', 'concentrated']) { + for (const scenario of ['cold', 'warm', 'changed']) { + report.scaling[`${shape}/${scenario}`] = evaluateScaling( + workloads + .filter((row) => row.shape === shape) + .map(({ name, files, methods }) => ({ + size: 2 * files * methods, + ...report.measured[`${name}/${scenario}`], + })), + analysisMetrics, + ); + } + } + const memorySizes = [2500, 10000, 40000]; + const memoryMetrics = ['extractionMs', 'extractionRetainedBytes', 'emittedRetainedBytes']; for (const language of ['java', 'typescript']) { - const trials = []; - report.memory[language] = trials; - for (let trial = 0; trial < samples; trial++) - trials.push(await child(root, 'memory', language)); - assert( - trials.every((trial) => trial.treeCollected), - `${language}: heap probe retained trees`, + const nameFor = (size) => (size === 10000 ? language : `${language}-${size}`); + for (const size of memorySizes) report.memory[nameFor(size)] = []; + for (let trial = 0; trial < samples; trial++) { + for (const size of trial % 2 ? [...memorySizes].reverse() : memorySizes) { + console.log(`${language} retained ${size} ${trial + 1}/${samples}`); + report.memory[nameFor(size)].push( + await child(root, 'memory', language, ['--declarations', String(size)]), + ); + } + } + for (const size of memorySizes) { + const name = nameFor(size); + const trials = report.memory[name]; + assert( + trials.every((trial) => trial.treeCollected), + `${language}: heap probe retained trees`, + ); + assert.equal( + new Set(trials.map((trial) => trial.digest)).size, + 1, + `${language}: nondeterministic metadata`, + ); + assert( + trials.every((trial) => trial.declarations === size), + `${name}: wrong declaration count`, + ); + correctness.memory[name] = { + digest: trials[0].digest, + declarations: trials[0].declarations, + }; + report.measured[`${name}/retained`] = summarize(trials, [ + ...memoryMetrics, + 'releasedDeltaBytes', + ]); + } + report.scaling[`${language}/retained`] = evaluateScaling( + memorySizes.map((size) => ({ size, ...report.measured[`${nameFor(size)}/retained`] })), + memoryMetrics, ); - assert.equal( - new Set(trials.map((trial) => trial.digest)).size, - 1, - `${language}: nondeterministic metadata`, - ); - correctness.memory[language] = { - digest: trials[0].digest, - declarations: trials[0].declarations, - }; - report.measured[`${language}/retained`] = summarize(trials, [ - 'extractionRetainedBytes', - 'emittedRetainedBytes', - 'releasedDeltaBytes', - ]); } report.correctness = correctness; + console.log(JSON.stringify({ scaling: report.scaling }, null, 2)); + assert( + Object.values(report.scaling).every((row) => row.passed), + 'Callable time or memory growth exceeds the 1.5 normalized scaling budget', + ); + if (baseline) + assert.deepEqual( + correctness, + baseline.correctness, + 'callable declaration facts or allocation counts differ from baseline.json', + ); report.baselineMeasured = baseline?.measured; report.changesPercent = baseline ? Object.fromEntries( @@ -589,19 +699,13 @@ async function checkBenchmark() { console.log( JSON.stringify({ measured: report.measured, changesPercent: report.changesPercent }, null, 2), ); - if (baseline) - assert.deepEqual( - correctness, - baseline.correctness, - 'callable declaration facts or allocation counts differ from baseline.json', - ); if (record) await writeFile( baselinePath, JSON.stringify( { _note: - 'Reviewed semantic and allocation baseline. --check compares fingerprints, cache counts and shared-array counts; timing and heap/RSS medians are references, not machine-independent thresholds. CI never records a baseline.', + 'Reviewed semantic and allocation baseline. --check compares fingerprints, cache counts and shared-array counts and gates same-run normalized time/memory growth at 1.5. Absolute timing and heap/RSS medians are references. CI never records a baseline.', environment: report.environment, correctness, measured: report.measured, diff --git a/gitnexus/bench/method-declaration-facts/scaling.mjs b/gitnexus/bench/method-declaration-facts/scaling.mjs new file mode 100644 index 000000000..6afbf77fc --- /dev/null +++ b/gitnexus/bench/method-declaration-facts/scaling.mjs @@ -0,0 +1,28 @@ +import assert from 'node:assert/strict'; + +export function evaluateScaling(rows, metrics) { + const budget = 1.5; + assert(rows.length >= 2 && metrics.length > 0, 'Scaling needs sizes and metrics'); + for (const [index, row] of rows.entries()) { + assert(Number.isFinite(row.size) && row.size > 0, 'Invalid workload size'); + if (index) assert(row.size > rows[index - 1].size, 'Sizes must increase'); + for (const metric of metrics) + assert(Number.isFinite(row[metric]) && row[metric] > 0, `Invalid ${metric}`); + } + const pairs = rows.slice(1).map((row, index) => [rows[index], row]); + if (rows.length > 2) pairs.push([rows[0], rows.at(-1)]); + const comparisons = pairs.map(([small, large]) => { + const inputRatio = large.size / small.size; + const ratios = Object.fromEntries( + metrics.map((metric) => [metric, large[metric] / small[metric] / inputRatio]), + ); + return { + from: small.size, + to: large.size, + inputRatio, + ratios, + passed: Object.values(ratios).every((ratio) => ratio <= budget), + }; + }); + return { budget, comparisons, passed: comparisons.every((row) => row.passed) }; +} diff --git a/gitnexus/src/core/ingestion/export-detection.ts b/gitnexus/src/core/ingestion/export-detection.ts index 143358c6f..c436e5d23 100644 --- a/gitnexus/src/core/ingestion/export-detection.ts +++ b/gitnexus/src/core/ingestion/export-detection.ts @@ -41,25 +41,33 @@ export const tsExportChecker: ExportChecker = (node, _name) => { /** Python: public if no leading underscore (convention). */ export const pythonExportChecker: ExportChecker = (_node, name) => !name.startsWith('_'); +// Parser trees are immutable during extraction; weak keys do not retain discarded trees. +const javaPublicModifierCache = new WeakMap(); + /** Java: check for 'public' modifier — modifiers are siblings of the name node, not parents. */ export const javaExportChecker: ExportChecker = (node, _name) => { let current: SyntaxNode | null = node; while (current) { - if (current.parent) { - const parent = current.parent; - for (let i = 0; i < parent.childCount; i++) { - const child = parent.child(i); - if (child?.type === 'modifiers' && child.text?.includes('public')) { - return true; + const parent: SyntaxNode | null = current.parent; + if (parent) { + let isPublic = javaPublicModifierCache.get(parent); + if (isPublic === undefined) { + isPublic = false; + for (let i = 0; i < parent.childCount; i++) { + const child = parent.child(i); + if (child?.type === 'modifiers' && child.text?.includes('public')) { + isPublic = true; + break; + } } - } - if (parent.type === 'method_declaration' || parent.type === 'constructor_declaration') { - if (parent.text?.trimStart().startsWith('public')) { - return true; + if (parent.type === 'method_declaration' || parent.type === 'constructor_declaration') { + isPublic ||= parent.text?.trimStart().startsWith('public') ?? false; } + javaPublicModifierCache.set(parent, isPublic); } + if (isPublic) return true; } - current = current.parent; + current = parent; } return false; }; diff --git a/gitnexus/test/integration/parsing.test.ts b/gitnexus/test/integration/parsing.test.ts index f7295b759..c1d55a345 100644 --- a/gitnexus/test/integration/parsing.test.ts +++ b/gitnexus/test/integration/parsing.test.ts @@ -6,7 +6,7 @@ * PHP export detection (#20), symbol ID with startLine (#19), * definition node range (#22). */ -import { describe, it, expect } from 'vitest'; +import { describe, it, expect, assert } from 'vitest'; import fs from 'fs/promises'; import path from 'path'; import { loadParser, loadLanguage } from '../../src/core/tree-sitter/parser-loader.js'; @@ -93,6 +93,52 @@ describe('parsing', () => { }); }); + describe('java', () => { + it('keeps sibling visibility independent when a parent check is reused', async () => { + const parser = await loadParser(); + await loadLanguage(SupportedLanguages.Java); + const tree = parser.parse('class C { private void a() {} public void b() {} void c() {} }'); + const methods = tree.rootNode.descendantsOfType('method_declaration'); + for (const order of [methods, [...methods].reverse()]) { + expect( + order.map((method) => { + const name = method.childForFieldName('name'); + assert(name); + return isNodeExported(name, name.text, 'java'); + }), + ).toEqual([false, true, false]); + } + }); + + it('preserves public-ancestor behavior across repeated checks and separate trees', async () => { + const parser = await loadParser(); + await loadLanguage(SupportedLanguages.Java); + for (const modifier of ['', 'public ', '']) { + const tree = parser.parse( + `${modifier}class C { private void a() {} class Inner { void b() {} } }`, + ); + const methods = tree.rootNode.descendantsOfType('method_declaration'); + expect(methods).toHaveLength(2); + for (const method of methods) { + const name = method.childForFieldName('name'); + assert(name); + expect(isNodeExported(name, name.text, 'java')).toBe(modifier === 'public '); + expect(isNodeExported(name, name.text, 'java')).toBe(modifier === 'public '); + } + } + }); + + it.each(['method_declaration', 'constructor_declaration'])( + 'preserves the %s text fallback', + (type) => { + const name = mockNode('identifier', 'C'); + mockNode(type, ' public C() {}', undefined, [name]); + expect(isNodeExported(name, 'C', 'java')).toBe(true); + expect(isNodeExported(name, 'C', 'java')).toBe(true); + }, + ); + }); + // Go describe('go', () => { it('uppercase first letter is exported', () => { diff --git a/gitnexus/test/unit/method-declaration-scaling.test.ts b/gitnexus/test/unit/method-declaration-scaling.test.ts new file mode 100644 index 000000000..0989cd3d2 --- /dev/null +++ b/gitnexus/test/unit/method-declaration-scaling.test.ts @@ -0,0 +1,70 @@ +import { describe, expect, it } from 'vitest'; + +const { evaluateScaling } = await import( + new URL('../../bench/method-declaration-facts/scaling.mjs', import.meta.url).href +); + +const sizes = [640, 2560, 10240]; +const series = (cost: (size: number) => number) => + sizes.map((size) => ({ size, time: cost(size), heap: size * 1000 })); + +describe('callable benchmark scaling guard', () => { + it('accepts linear growth with fixed startup overhead', () => { + expect( + evaluateScaling( + series((n) => 500 + n), + ['time', 'heap'], + ).passed, + ).toBe(true); + }); + + it('rejects quadratic time despite linear memory', () => { + expect( + evaluateScaling( + series((n) => n * n), + ['time', 'heap'], + ).passed, + ).toBe(false); + }); + + it('rejects quadratic memory despite linear time', () => { + const rows = series((n) => n).map((row) => ({ ...row, heap: row.size ** 2 })); + expect(evaluateScaling(rows, ['time', 'heap']).passed).toBe(false); + }); + + it('checks the whole size range as well as adjacent sizes', () => { + // Each 4x step grows cost by 5.28x, but the 16x span grows it by 27.86x. + expect( + evaluateScaling( + series((n) => n ** 1.2), + ['time'], + ).passed, + ).toBe(false); + }); + + it('rejects a regression confined to the largest workload', () => { + const rows = series((n) => n); + rows[2].time *= 7; + expect(evaluateScaling(rows, ['time']).passed).toBe(false); + }); + + it.each([0, -1, NaN, Infinity, undefined])('rejects an invalid reading: %s', (time) => { + const rows = series((n) => n); + expect(() => evaluateScaling([{ ...rows[0], time }, ...rows.slice(1)], ['time'])).toThrow(); + }); + + it('rejects missing sizes or metrics', () => { + expect(() => evaluateScaling(series((n) => n).slice(0, 1), ['time'])).toThrow(); + expect(() => + evaluateScaling( + series((n) => n), + [], + ), + ).toThrow(); + }); + + it.each([0, -1, NaN, Infinity, 640])('rejects invalid or repeated sizes: %s', (size) => { + const rows = series((n) => n); + expect(() => evaluateScaling([rows[0], { ...rows[1], size }, rows[2]], ['time'])).toThrow(); + }); +});