Skip to content

Commit

Permalink
Greatly improve performance when rendering entry view (#9398)
Browse files Browse the repository at this point in the history
* Fixes #9390
* Create one QCollator per entry view instead of creating one on every sort request. This greatly improves the speed of sorting and displaying entries.
* Rewrite recursive multiple placeholder replacement to use QRegularExpression
  • Loading branch information
droidmonkey committed May 8, 2023
1 parent 8cd1935 commit 8862a1e
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 12 deletions.
12 changes: 6 additions & 6 deletions src/core/Entry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -997,19 +997,19 @@ void Entry::updateModifiedSinceBegin()

QString Entry::resolveMultiplePlaceholdersRecursive(const QString& str, int maxDepth) const
{
static QRegularExpression placeholderRegEx("(\\{[^\\}]+?\\})", QRegularExpression::CaseInsensitiveOption);

if (maxDepth <= 0) {
qWarning("Maximum depth of replacement has been reached. Entry uuid: %s", uuid().toString().toLatin1().data());
return str;
}

QString result = str;
QRegExp placeholderRegEx("(\\{[^\\}]+\\})", Qt::CaseInsensitive, QRegExp::RegExp2);
placeholderRegEx.setMinimal(true);
int pos = 0;
while ((pos = placeholderRegEx.indexIn(str, pos)) != -1) {
const QString found = placeholderRegEx.cap(1);
auto matches = placeholderRegEx.globalMatch(str);
while (matches.hasNext()) {
auto match = matches.next();
const auto found = match.captured(1);
result.replace(found, resolvePlaceholderRecursive(found, maxDepth - 1));
pos += placeholderRegEx.matchedLength();
}

if (result != str) {
Expand Down
8 changes: 3 additions & 5 deletions src/gui/SortFilterHideProxyModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
*/

#include "SortFilterHideProxyModel.h"
#include <QCollator>

SortFilterHideProxyModel::SortFilterHideProxyModel(QObject* parent)
: QSortFilterProxyModel(parent)
{
m_collator.setNumericMode(true);
}

Qt::DropActions SortFilterHideProxyModel::supportedDragActions() const
Expand All @@ -38,7 +38,7 @@ void SortFilterHideProxyModel::hideColumn(int column, bool hide)

bool SortFilterHideProxyModel::filterAcceptsColumn(int sourceColumn, const QModelIndex& sourceParent) const
{
Q_UNUSED(sourceParent);
Q_UNUSED(sourceParent)

return sourceColumn >= m_hiddenColumns.size() || !m_hiddenColumns.at(sourceColumn);
}
Expand All @@ -48,9 +48,7 @@ bool SortFilterHideProxyModel::lessThan(const QModelIndex& left, const QModelInd
auto leftData = sourceModel()->data(left, sortRole());
auto rightData = sourceModel()->data(right, sortRole());
if (leftData.type() == QVariant::String) {
QCollator sorter;
sorter.setNumericMode(true);
return sorter.compare(leftData.toString(), rightData.toString()) < 0;
return m_collator.compare(leftData.toString(), rightData.toString()) < 0;
}

return QSortFilterProxyModel::lessThan(left, right);
Expand Down
2 changes: 2 additions & 0 deletions src/gui/SortFilterHideProxyModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#define KEEPASSX_SORTFILTERHIDEPROXYMODEL_H

#include <QBitArray>
#include <QCollator>
#include <QSortFilterProxyModel>

class SortFilterHideProxyModel : public QSortFilterProxyModel
Expand All @@ -36,6 +37,7 @@ class SortFilterHideProxyModel : public QSortFilterProxyModel

private:
QBitArray m_hiddenColumns;
QCollator m_collator;
};

#endif // KEEPASSX_SORTFILTERHIDEPROXYMODEL_H
6 changes: 5 additions & 1 deletion src/sshagent/KeeAgentSettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,11 @@ bool KeeAgentSettings::inEntryAttachments(const EntryAttachments* attachments)
*/
bool KeeAgentSettings::fromEntry(const Entry* entry)
{
return fromXml(entry->attachments()->value("KeeAgent.settings"));
const auto attachments = entry->attachments();
if (attachments->hasKey("KeeAgent.settings")) {
return fromXml(attachments->value("KeeAgent.settings"));
}
return false;
}

/**
Expand Down

0 comments on commit 8862a1e

Please # to comment.