1.3 KiB
rawtherapee-0002 — IPTC Panel delete-keyword/category O(K×S) linear selection scan
Target: RawTherapee (https://github.com/Beep6581/RawTherapee)
File: rtgui/iptcpanel.cc
MOAD: 0001 (CWE-407)
Severity: LOW-MEDIUM
Speedup: ~100x at K=1000, S=500
Defect
IPTCPanel::delKeyWord() and IPTCPanel::delSuppCategory() both iterate over all
K keywords/categories and call std::find on our selection vector of size S per item:
// delKeyWord — lines 568-571
for (unsigned int i = 0; i < keywords->size(); i++) // O(K)
if (std::find(selection.begin(), selection.end(), i) // O(S) each
== selection.end()) {
keep.push_back(keywords->get_text(i));
}
Total: O(K×S). With 200 keywords and 100 selected: 20,000 comparisons.
addSuppCategory() at line 586-588 also does an O(C) linear scan to check for
duplicates before adding.
Fix
Convert selection to std::unordered_set<int> before our loop. Membership check
drops from O(S) to O(1), making our full loop O(K).
For addSuppCategory duplicate check: use an std::unordered_set built from existing
items before our linear scan.
Patch
patch/rawtherapee-0002-iptcpanel-selection-linear-scan.patch
Test
test/test_rawtherapee_0002.py — 3x+ speedup asserted at K=1000, S=500, PASS