[Search] Stop allocating per card in card-name completion (#7604)

* [Search] Stop allocating per card in card-name completion

CardSearchModel::updateSearchResults() runs on every textEdited()
signal, walks the whole card database and runs Levenshtein against every
matching name. Three costs dominated that path:

1. Every candidate built a QStringList of its searchable names, then
   lowercased each name into another QString, only to break out of the
   loop after the first hit.
2. The sort comparator called sortableName() for each comparison, which
   re-fetched the name (QMap lookup plus QString construction) O(n log n)
   times.
3. levenshteinDistance() allocated a full len1 x len2 vector of vectors.

The search model now lowercases the one name it tests directly, stores
the tie-breaking name length once per result, and the comparator only
compares integers. levenshteinDistance() uses the standard two-row
formulation: same result, O(len2) memory instead of O(len1*len2), with
early exits for empty and equal strings.

FilterString query closures get the same treatment:
- Set queries iterate the set map directly instead of materializing
  keys()/values() lists for every card.
- Rarity queries iterate printings in place instead of copying them into
  a QList<PrintingInfo> first.
- Power/toughness split the string with indexOf and compare QStringViews
  instead of allocating a QStringList.
- Field queries do one hash lookup instead of contains() followed by
  value().
- Color queries bind the colors string by reference instead of copying.

Verified with filter_string_test (42 tests) and a full client build.

* [Search] Address review comments

- Use std::any_of/std::none_of over the set map's key/value iterators
  instead of hand-rolled loops.
- Extract the name matching into matchingLowercaseName() returning
  std::optional<QString>.
- Reuse the matched name's length for the distance tie-break instead of
  looking the name up again, removing one getLocalizedName() call per card.
- Take the color strings by value; the getters already return by value.

---------

Co-authored-by: Lukas Brübach <Bruebach.Lukas@bdosecurity.de>
This commit is contained in:
BruebachL 2026-10-23 11:23:04 +02:00 • committed by GitHub
parent 4980f88a7a
commit f4b4365464
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 113 additions and 76 deletions

View file

@ -9,6 +9,7 @@
#include <QtGlobal>
#include <algorithm>
#include <libcockatrice/utility/levenshtein.h>
#include <utility>
CardSearchModel::CardSearchModel(CardDatabaseDisplayModel *sourceModel, QObject *parent)
: QAbstractListModel(parent), sourceModel(sourceModel)
@ -69,31 +70,28 @@ void CardSearchModel::updateSearchResults(const QString &query)
continue;
}
// The completer suggestions match against the same languages the card
// search uses, so typing a localized name finds the card. In Both mode
// either language can match.
for (const QString &matchName : searchableNames(card)) {
const QString lowerName = matchName.toLower();
if (!lowerName.contains(lowerQuery)) {
continue;
}
const std::optional<QString> lowerName = matchingLowercaseName(card, lowerQuery);
if (!lowerName) {
continue;
}
const int distance = levenshteinDistance(lowerQuery, lowerName);
const int distance = levenshteinDistance(lowerQuery, *lowerName);
// Length of the matched name, used to break distance ties. Stored here
// so the comparator does not look the name up again for every comparison.
const int nameLength = static_cast<int>(lowerName->size());
if (lowerName.startsWith(lowerQuery)) {
prefixMatches.append({card, distance});
} else {
containsMatches.append({card, distance});
}
break;
if (lowerName->startsWith(lowerQuery)) {
prefixMatches.append({card, distance, nameLength});
} else {
containsMatches.append({card, distance, nameLength});
}
}
auto sortByDistanceThenLength = [this](const SearchResult &a, const SearchResult &b) {
auto sortByDistanceThenLength = [](const SearchResult &a, const SearchResult &b) {
if (a.distance != b.distance) {
return a.distance < b.distance;
}
return sortableName(a.card).size() < sortableName(b.card).size();
return a.nameLength < b.nameLength;
};
std::sort(prefixMatches.begin(), prefixMatches.end(), sortByDistanceThenLength);
@ -112,24 +110,28 @@ void CardSearchModel::updateSearchResults(const QString &query)
endResetModel();
}
QStringList CardSearchModel::searchableNames(const CardInfoPtr &card) const
std::optional<QString> CardSearchModel::matchingLowercaseName(const CardInfoPtr &card, const QString &lowerQuery) const
{
if (!searchLanguage.isEnglishOnly() && searchLanguage.mode == SearchLanguageMode::Selected) {
QString localizedName = card->getLocalizedName(searchLanguage.language).toLower();
if (localizedName.contains(lowerQuery)) {
return std::optional<QString>(std::move(localizedName));
}
return std::nullopt;
}
QString englishName = card->getName().toLower();
if (englishName.contains(lowerQuery)) {
return std::optional<QString>(std::move(englishName));
}
if (searchLanguage.isEnglishOnly()) {
return {card->getName()};
return std::nullopt;
}
const QString localizedName = card->getLocalizedName(searchLanguage.language);
if (searchLanguage.mode == SearchLanguageMode::Selected) {
return {localizedName};
QString localizedName = card->getLocalizedName(searchLanguage.language).toLower();
if (localizedName.contains(lowerQuery)) {
return std::optional<QString>(std::move(localizedName));
}
return {card->getName(), localizedName};
}
QString CardSearchModel::sortableName(const CardInfoPtr &card) const
{
if (searchLanguage.isEnglishOnly()) {
return card->getName();
}
return card->getLocalizedName(searchLanguage.language);
return std::nullopt;
}