From fa00afe7d760d91cf9c91f57dd2f77625e758e28 Mon Sep 17 00:00:00 2001 From: Konstantin Ritt Date: Sat, 21 Nov 2015 01:04:39 +0400 Subject: [PATCH] Optimize QTextEngine::findItem() usage cases Since the item positions are guaranteed to grow, we could safely re-use the obtained first item while looking for the last item in the chain. Change-Id: I5e42f5de820c62a51a109a4b227b031c697aa898 Reviewed-by: Lars Knoll --- src/gui/text/qtextengine.cpp | 13 +++++++------ src/gui/text/qtextengine_p.h | 2 +- src/gui/text/qtextlayout.cpp | 4 ++-- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/src/gui/text/qtextengine.cpp b/src/gui/text/qtextengine.cpp index 50a242d81e..31b1109b6a 100644 --- a/src/gui/text/qtextengine.cpp +++ b/src/gui/text/qtextengine.cpp @@ -918,10 +918,11 @@ void QTextEngine::shapeLine(const QScriptLine &line) { QFixed x; bool first = true; - const int end = findItem(line.from + line.length - 1); int item = findItem(line.from); if (item == -1) return; + + const int end = findItem(line.from + line.length - 1, item); for ( ; item <= end; ++item) { QScriptItem &si = layoutData->items[item]; if (si.analysis.flags == QScriptAnalysis::Tab) { @@ -1747,13 +1748,13 @@ bool QTextEngine::isRightToLeft() const } -int QTextEngine::findItem(int strPos) const +int QTextEngine::findItem(int strPos, int firstItem) const { itemize(); - if (strPos < 0 || strPos >= layoutData->string.size()) + if (strPos < 0 || strPos >= layoutData->string.size() || firstItem < 0) return -1; - int left = 1; + int left = firstItem + 1; int right = layoutData->items.size()-1; while(left <= right) { int middle = ((right-left)/2)+left; @@ -2172,7 +2173,7 @@ void QTextEngine::justify(const QScriptLine &line) return; int firstItem = findItem(line.from); - int lastItem = findItem(line.from + line_length - 1); + int lastItem = findItem(line.from + line_length - 1, firstItem); int nItems = (firstItem >= 0 && lastItem >= firstItem)? (lastItem-firstItem+1) : 0; QVarLengthArray justificationPoints; @@ -3529,7 +3530,7 @@ QTextLineItemIterator::QTextLineItemIterator(QTextEngine *_eng, int _lineNum, co lineNum(_lineNum), lineEnd(line.from + line.length), firstItem(eng->findItem(line.from)), - lastItem(eng->findItem(lineEnd - 1)), + lastItem(eng->findItem(lineEnd - 1, firstItem)), nItems((firstItem >= 0 && lastItem >= firstItem)? (lastItem-firstItem+1) : 0), logicalItem(-1), item(-1), diff --git a/src/gui/text/qtextengine_p.h b/src/gui/text/qtextengine_p.h index dbe8e1ee2b..39c228fd52 100644 --- a/src/gui/text/qtextengine_p.h +++ b/src/gui/text/qtextengine_p.h @@ -512,7 +512,7 @@ public: void freeMemory(); - int findItem(int strPos) const; + int findItem(int strPos, int firstItem = 0) const; inline QTextFormatCollection *formatCollection() const { if (block.docHandle()) return block.docHandle()->formatCollection(); diff --git a/src/gui/text/qtextlayout.cpp b/src/gui/text/qtextlayout.cpp index 67af3d316b..5f570fea82 100644 --- a/src/gui/text/qtextlayout.cpp +++ b/src/gui/text/qtextlayout.cpp @@ -2685,7 +2685,7 @@ qreal QTextLine::cursorToX(int *cursorPos, Edge edge) const // add the items left of the cursor int firstItem = eng->findItem(line.from); - int lastItem = eng->findItem(lineEnd - 1); + int lastItem = eng->findItem(lineEnd - 1, itm); int nItems = (firstItem >= 0 && lastItem >= firstItem)? (lastItem-firstItem+1) : 0; QVarLengthArray visualOrder(nItems); @@ -2786,7 +2786,7 @@ int QTextLine::xToCursor(qreal _x, CursorPosition cpos) const return line.from; int firstItem = eng->findItem(line.from); - int lastItem = eng->findItem(line.from + line_length - 1); + int lastItem = eng->findItem(line.from + line_length - 1, firstItem); int nItems = (firstItem >= 0 && lastItem >= firstItem)? (lastItem-firstItem+1) : 0; if (!nItems)