java-topology/defects/krita-0002/TICKET.md
russell@unturf.com 7ecd95c4d6 kdenlive+audacity: 5-MOAD rescan; kdenlive-0010 CWE-407 checkConsistency QList::contains O(P*K^2) 500x at K=1000; audacity CLEAN rescan confirmed
kdenlive-0010: KeyframeModelList::checkConsistency() in
src/assets/keyframes/model/keyframemodellist.cpp calls QList<GenTime>::contains()
inside nested loops — O(P*K^2) at clip load for multi-parameter keyframe effects.
Fix: std::set<GenTime> using operator< for O(log K) insert/lookup in both phases.
Speedup: 50x at K=100, 250x at K=500, 500x at K=1000 (P=3 params). 12/12 PASS.

Audacity: full 5-MOAD rescan on fresh clone confirms prior scan results.
No new defects. MOADs 0002/0003/0004/0005 CLEAN.
2026-04-03 14:50:10 -04:00

1.6 KiB

krita-0002 — DlgCreateBundle putResourcesInTheBundle O(N²) QStack::contains dedup

Summary

DlgCreateBundle::putResourcesInTheBundle uses a QStack<int> (allResourcesIds) as a work-queue for bundle export. While processing each resource it discovers linked resources and checks allResourcesIds.contains(resource->resourceId()) before appending. QStack inherits QList, so contains is an O(N) linear scan. With K total resources (selected + linked), each append check costs O(K), giving O(K²) total work for deduplication.

Location

plugins/extensions/resourcemanager/dlg_create_bundle.cpp

  • putResourcesInTheBundle (~line 187): QStack<int> allResourcesIds
  • Line ~242: if (!allResourcesIds.contains(resource->resourceId()))

Severity

LOW-MEDIUM. Triggered once per bundle export. Bundles can contain thousands of resources with linked dependencies (brush tips, patterns, gradients). At K=1000 the O(K²) dedup cost becomes noticeable on export.

Complexity

Before After
O(K²) QStack::contains per linked resource O(K) QSet::contains per linked resource

Fix

Maintain a parallel QSet<int> seenIds for O(1) dedup while keeping the QStack as work-queue for ordering.

QStack<int> allResourcesIds;
QSet<int> seenIds;
Q_FOREACH(int id, selectedResourcesIds) {
    allResourcesIds << id;
    seenIds.insert(id);
}
// ...
if (!seenIds.contains(resource->resourceId())) {
    seenIds.insert(resource->resourceId());
    allResourcesIds.append(resource->resourceId());
}

References

  • CWE-407: Inefficient Algorithmic Complexity
  • MOAD-0001: The Sedimentary Defect