+
+ const int itemCount = m_itemData.count();
+ int itemsInHash = m_items.count();
+
+ int index = m_items.value(urlToFind, -1);
+ while (index < 0 && itemsInHash < itemCount) {
+ // Not all URLs are stored yet in m_items. We grow m_items until either
+ // urlToFind is found, or all URLs have been stored in m_items.
+ // Note that we do not add the URLs to m_items one by one, but in
+ // larger blocks. After each block, we check if urlToFind is in
+ // m_items. We could in principle compare urlToFind with each URL while
+ // we are going through m_itemData, but comparing two QUrls will,
+ // unlike calling qHash for the URLs, trigger a parsing of the URLs
+ // which costs both CPU cycles and memory.
+ const int blockSize = 1000;
+ const int currentBlockEnd = qMin(itemsInHash + blockSize, itemCount);
+ for (int i = itemsInHash; i < currentBlockEnd; ++i) {
+ const KUrl nextUrl = m_itemData.at(i)->item.url();
+ m_items.insert(nextUrl, i);
+ }
+
+ itemsInHash = currentBlockEnd;
+ index = m_items.value(urlToFind, -1);
+ }
+
+ Q_ASSERT(index >= 0 || m_items.count() == m_itemData.count());
+
+ return index;