diff options
author | Christian Kamm <christian.d.kamm@nokia.com> | 2010-07-06 10:20:24 (GMT) |
---|---|---|
committer | Christian Kamm <christian.d.kamm@nokia.com> | 2010-07-06 10:20:24 (GMT) |
commit | f62f6effab8d1551d8e5e5843dc478addee96de1 (patch) | |
tree | 1d47256a1ad69d967b2c4227dfdd15207d195cf0 | |
parent | 7e35c6ff442e237ff9a1bec9ea1cdfb597d9ceae (diff) | |
download | Qt-f62f6effab8d1551d8e5e5843dc478addee96de1.zip Qt-f62f6effab8d1551d8e5e5843dc478addee96de1.tar.gz Qt-f62f6effab8d1551d8e5e5843dc478addee96de1.tar.bz2 |
Fix performance of QTextDocumentPrivate::adjustDocumentChangesAndCursors
As the changedCursors list grew large, the function used to spend an
extraordinate amount of time in QList::contains.
The fix removes the changedCursors list outright and replaces it with a
flag in QTextCursorPrivate.
Done-with: mae
-rw-r--r-- | src/gui/text/qtextcursor.cpp | 3 | ||||
-rw-r--r-- | src/gui/text/qtextcursor_p.h | 1 | ||||
-rw-r--r-- | src/gui/text/qtextdocument_p.cpp | 34 | ||||
-rw-r--r-- | src/gui/text/qtextdocument_p.h | 5 |
4 files changed, 22 insertions, 21 deletions
diff --git a/src/gui/text/qtextcursor.cpp b/src/gui/text/qtextcursor.cpp index a9caa6b..63aa946 100644 --- a/src/gui/text/qtextcursor.cpp +++ b/src/gui/text/qtextcursor.cpp @@ -64,7 +64,8 @@ enum { QTextCursorPrivate::QTextCursorPrivate(QTextDocumentPrivate *p) : priv(p), x(0), position(0), anchor(0), adjusted_anchor(0), - currentCharFormat(-1), visualNavigation(false), keepPositionOnInsert(false) + currentCharFormat(-1), visualNavigation(false), keepPositionOnInsert(false), + changed(false) { priv->addCursor(this); } diff --git a/src/gui/text/qtextcursor_p.h b/src/gui/text/qtextcursor_p.h index 4e36b95..4b3262f 100644 --- a/src/gui/text/qtextcursor_p.h +++ b/src/gui/text/qtextcursor_p.h @@ -114,6 +114,7 @@ public: int currentCharFormat; uint visualNavigation : 1; uint keepPositionOnInsert : 1; + uint changed : 1; }; QT_END_NAMESPACE diff --git a/src/gui/text/qtextdocument_p.cpp b/src/gui/text/qtextdocument_p.cpp index a55e5f3..9849317 100644 --- a/src/gui/text/qtextdocument_p.cpp +++ b/src/gui/text/qtextdocument_p.cpp @@ -235,17 +235,17 @@ void QTextDocumentPrivate::init() void QTextDocumentPrivate::clear() { Q_Q(QTextDocument); - for (int i = 0; i < cursors.count(); ++i) { - cursors.at(i)->setPosition(0); - cursors.at(i)->currentCharFormat = -1; - cursors.at(i)->anchor = 0; - cursors.at(i)->adjusted_anchor = 0; + + foreach (QTextCursorPrivate *curs, cursors) { + curs->setPosition(0); + curs->currentCharFormat = -1; + curs->anchor = 0; + curs->adjusted_anchor = 0; } QList<QTextCursorPrivate *>oldCursors = cursors; QT_TRY{ cursors.clear(); - changedCursors.clear(); QMap<int, QTextObject *>::Iterator objectIt = objects.begin(); while (objectIt != objects.end()) { @@ -288,8 +288,8 @@ void QTextDocumentPrivate::clear() QTextDocumentPrivate::~QTextDocumentPrivate() { - for (int i = 0; i < cursors.count(); ++i) - cursors.at(i)->priv = 0; + foreach (QTextCursorPrivate *curs, cursors) + curs->priv = 0; cursors.clear(); undoState = 0; undoEnabled = true; @@ -1225,9 +1225,11 @@ void QTextDocumentPrivate::finishEdit() } } - while (!changedCursors.isEmpty()) { - QTextCursorPrivate *curs = changedCursors.takeFirst(); - emit q->cursorPositionChanged(QTextCursor(curs)); + foreach (QTextCursorPrivate *curs, cursors) { + if (curs->changed) { + curs->changed = false; + emit q->cursorPositionChanged(QTextCursor(curs)); + } } contentsChanged(); @@ -1273,11 +1275,9 @@ void QTextDocumentPrivate::adjustDocumentChangesAndCursors(int from, int addedOr if (!editBlock) ++revision; - for (int i = 0; i < cursors.size(); ++i) { - QTextCursorPrivate *curs = cursors.at(i); + foreach (QTextCursorPrivate *curs, cursors) { if (curs->adjustPosition(from, addedOrRemoved, op) == QTextCursorPrivate::CursorMoved) { - if (!changedCursors.contains(curs)) - changedCursors.append(curs); + curs->changed = true; } } @@ -1700,8 +1700,8 @@ bool QTextDocumentPrivate::ensureMaximumBlockCount() void QTextDocumentPrivate::aboutToRemoveCell(int from, int to) { Q_ASSERT(from <= to); - for (int i = 0; i < cursors.size(); ++i) - cursors.at(i)->aboutToRemoveCell(from, to); + foreach (QTextCursorPrivate *curs, cursors) + curs->aboutToRemoveCell(from, to); } QT_END_NAMESPACE diff --git a/src/gui/text/qtextdocument_p.h b/src/gui/text/qtextdocument_p.h index 06e0753..dcac99d 100644 --- a/src/gui/text/qtextdocument_p.h +++ b/src/gui/text/qtextdocument_p.h @@ -277,7 +277,7 @@ public: void documentChange(int from, int length); inline void addCursor(QTextCursorPrivate *c) { cursors.append(c); } - inline void removeCursor(QTextCursorPrivate *c) { cursors.removeAll(c); changedCursors.removeAll(c); } + inline void removeCursor(QTextCursorPrivate *c) { cursors.removeAll(c); } QTextFrame *frameAt(int pos) const; QTextFrame *rootFrame() const; @@ -329,8 +329,7 @@ private: BlockMap blocks; int initialBlockCharFormatIndex; - QList<QTextCursorPrivate*> cursors; - QList<QTextCursorPrivate*> changedCursors; + QList<QTextCursorPrivate *> cursors; QMap<int, QTextObject *> objects; QMap<QUrl, QVariant> resources; QMap<QUrl, QVariant> cachedResources; |