Both were flagged in Wave 17 survey as borderline real defects deferred for follow-up because the fix needed careful design beyond a single-line set hoist. Both shipped now with full bench + ticket + intel. UNDF-1310 symfony-0001 (HIGH) - PropertyAccessor::writeCollection. Doctrine entity collection diff: in_array($item, $collection, true) per item in $previousValue, then in_array($item, $previousValue, true) per item in $collection. O(P*C). Fix: dual lookup (SplObjectStorage for objects + serialize-keyed array for scalars, in_array fallback for resources). Bench: 5.2x at P=C=100, 88x at P=C=2000. UNDF-1311 pyright-0001 (HIGH) - CallHierarchyProvider outgoing/incoming call dedup. _outgoingCalls.find / _incomingCalls.find with composite key (uri, range) walks the list per call expression. O(C^2). Fix: parallel Map<string, entry> keyed by composite serialized form (uri|start.line|start.char|end.line|end.char). Bench: 2.7x at C=100, 22x at C=2000. Total session flagships: 11 (was 9) — 7 CWE-407 + 3 MOAD-0003 + 1 MOAD-0004. Wave 17 borderline backlog now empty.
4 KiB
pyright-0001: CallHierarchyProvider — O(C²) outgoing/incoming dedup
Target: microsoft/pyright
Severity: HIGH
CWE: CWE-407 (Inefficient Algorithmic Complexity)
MOAD: MOAD-0001 (A Sedimentary Defect)
File: packages/pyright-internal/src/languageService/callHierarchyProvider.ts:394-396, 608-610
Language: TypeScript
Status: open
Description
CallHierarchyProvider has TWO parallel patterns that linear-scan the recorded-calls list to dedup by composite key (uri, range):
// _outgoingCalls: line 394-396
let outgoingCall = this._outgoingCalls.find(
(outgoing) => outgoing.to.uri === callDest.uri &&
rangesAreEqual(outgoing.to.range, callDest.range)
);
// _incomingCalls: line 608-610 (same shape, different visitor class)
let incomingCall = this._incomingCalls.find(
(incoming) => incoming.from.uri === callSource.uri &&
rangesAreEqual(incoming.from.range, callSource.range)
);
Per discovered call expression, Array.find walks the list from index 0. For C call expressions in a function, total cost is O(C²).
Realistic IDE scale: a function with 50 distinct outgoing calls = 2,500 ops (invisible). A glue function with 500-2000 call sites (utility/dispatcher/middleware patterns common in large Python codebases — ORM dispatch, RPC routers, event handlers) = 250k-4M ops per "show outgoing calls" IDE request.
Severity Note
IDE responsiveness defect. Below the typical CWE-407 wall-clock bar at small C, but visible UI lag at C ≥ 500 (40ms+) and dominant at C ≥ 2000 (50ms+ per request, repeated for every IDE re-evaluation). Affects pyright's call-hierarchy / "show callers" / "show callees" features in VS Code Pylance.
Root Cause
// callHierarchyProvider.ts:287
private _outgoingCalls: CallHierarchyOutgoingCall[] = [];
// line 394-396 — linear scan per call expression
let outgoingCall = this._outgoingCalls.find(
(outgoing) => outgoing.to.uri === callDest.uri && rangesAreEqual(outgoing.to.range, callDest.range)
);
Array.find is O(N); per-call cost grows with the number of already-recorded calls. Across C iterations: O(C²).
Fix
Maintain a parallel Map<string, entry> keyed by composite (uri | start.line | start.character | end.line | end.character). Lookup via map.get(key) is O(1). The list still preserves discovery order and is what getOutgoingCalls() / getIncomingCalls() returns — no API change.
private _outgoingCalls: CallHierarchyOutgoingCall[] = [];
private _outgoingCallsByKey: Map<string, CallHierarchyOutgoingCall> = new Map();
private static _callKey(uri: string, range: Range): string {
return `${uri}|${range.start.line}|${range.start.character}|`
+ `${range.end.line}|${range.end.character}`;
}
// per-call dedup:
const dedupKey = CallFinder._callKey(callDest.uri, callDest.range);
let outgoingCall = this._outgoingCallsByKey.get(dedupKey);
if (!outgoingCall) {
outgoingCall = { to: callDest, fromRanges: [] };
this._outgoingCalls.push(outgoingCall);
this._outgoingCallsByKey.set(dedupKey, outgoingCall);
}
Same shape applies to _incomingCalls in CallVisitor (line 608-610).
Bench (defects/pyright/bench/results.txt)
=== pyright-0001: CallHierarchyProvider O(C^2) -> O(C) ===
scale defective fixed speedup
-------------------------------------------------------
C= 100 0.34ms 0.13ms 2.7x
C= 500 8.33ms 1.07ms 7.8x
C= 1000 18.28ms 1.13ms 16.2x
C= 2000 46.21ms 2.10ms 22.1x
C= 5000 138.21ms 6.88ms 20.1x
Discovery context
Documented in Wave 17 survey as a borderline real defect — flagged because the fix is mechanical but needs composite-key serialization design (Range is a struct, not directly map-key-able). This patch is the composite-key-Map fix Wave 17 deferred for follow-up.
Complexity Gate
- C=2000 call expressions: fixed must complete in <3ms
- k-scaling 5×: time ratio must be <17.5×