java-topology/defects/cmake/cmake-0002-getdirectories-ticket.md

1.2 KiB
Raw Blame History

cmake-0002 — GetDirectoriesWithBacktraces: O(n²) std::find inside loop

Severity: MEDIUM File: Source/cmComputeLinkInformation.cxx Lines: 465472 CWE: CWE-407 (Algorithmic Complexity — Quadratic)

Description

cmComputeLinkInformation::GetDirectoriesWithBacktraces() iterates over orderedDirectories (size N) and for each entry calls std::find over targetLinkDirectories (size M) to recover the backtrace annotation. Total cost: O(N × M).

In a large project with many link directories and many targets, both N and M grow with the number of libraries, making this O(n²) on the link directory count.

// Source/cmComputeLinkInformation.cxx:465-472
for (std::string const& dir : orderedDirectories) {
    auto result = std::find(targetLinkDirectories.begin(),   // O(M) scan
                            targetLinkDirectories.end(), dir);
    ...
}

Fix

Build a std::unordered_map<string, BT<string>> from targetLinkDirectories before the loop, reducing each lookup to O(1) amortised.

Patch: patch/cmake-0002-getdirectories-unordered-map.patch Unit test: unit/GetDirectoriesAlgorithm.java

Complexity

Time
Before O(N × M)
After O(N + M)