java-topology/defects/julia/patch/julia-0002-reinfer-visited-method-array-hashset.md

55 lines
1.6 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# UNDF: UNDF-2026-000000352
## Classification
| Field | Value |
|-------------|-------|
| CWE | CWE-407 Inefficient Algorithmic Complexity |
| Severity | MEDIUM |
| Component | `Compiler/src/reinfer.jl:432` |
| Function | (method order determination — morespecific BFS) |
| Hot path | Method dispatch ordering — called during type inference for every ambiguous method pair |
| Status | PATCHED (unit test PASS) |
## Defect
Method interference graph BFS uses `visited = Method[]` (Array) with `method3 in visited`
for cycle prevention. `in` on a Julia Vector/Array is O(V) linear scan:
```julia
visited = Method[] # Array — O(V) membership
push!(visited, method2)
workqueue = Method[method2]
while !isempty(workqueue)
current = pop!(workqueue)
for k = 1:length(interferences)
method3 = interferences[k]::Method
method3 in visited && continue # O(V) per check — CWE-407
push!(visited, method3)
push!(workqueue, method3)
end
end
```
With V methods reachable in the interference graph, total membership checks: O(V²).
Called during type inference for every ambiguous method pair resolution.
## Fix
```julia
visited = Set{Method}() # O(1) membership
push!(visited, method2)
workqueue = Method[method2]
while !isempty(workqueue)
current = pop!(workqueue)
for k = 1:length(interferences)
method3 = interferences[k]::Method
method3 in visited && continue # O(1) hash lookup
push!(visited, method3)
push!(workqueue, method3)
end
end
```
Speedup: O(V²) → O(V) — ~250× at V=500 (62,500 → 500 checks).