From 0102f34f1ebe91aaff6fb2edc83a7ebf7ffb4d1e Mon Sep 17 00:00:00 2001 From: Milian Wolff Date: Mon, 3 Sep 2012 18:06:17 +0200 Subject: Optimize QTextLayout/QTextEngine usage outside of QTextDocument. When QTextLayout is used in a QTextDocument, many code paths use special caches and thus greatly outperform the raw QTextLayout version that operates directly on a QString. This patch brings some of these optimizations also to the raw version. We now also use a QFormatCollection in such cases and enable the functionality of QTextEngine::indexAdditionalFormats() and QTextEngine::resolveAdditionalFormats(). Thanks to that, we can greatly speed up QTextEngine::format(), which now uses an amort O(1) hash table lookup instead of a O(N) linear search. The added benchmark shows a gain in the order of one magnitude: ./tst_bench_QText formattedLayout:long-many before applying the patch: 378.19 msecs per iteration (total: 37,820, iterations: 100) after applying the patch: 25.80 msecs per iteration (total: 2,580, iterations: 100) Note: This change is source-incompatible for applications using the private QTextEngine API. Task-number: QTBUG-8389 Change-Id: Ifcf7a8902a394428979ea06a6d955f886ee739c7 Reviewed-by: Konstantin Ritt Reviewed-by: Olivier Goffart Reviewed-by: Lars Knoll --- tests/benchmarks/gui/text/qtext/main.cpp | 43 ++++++++++++++++++++++++++------ 1 file changed, 35 insertions(+), 8 deletions(-) (limited to 'tests/benchmarks/gui') diff --git a/tests/benchmarks/gui/text/qtext/main.cpp b/tests/benchmarks/gui/text/qtext/main.cpp index 5a4745fbc7..8188a01b3e 100644 --- a/tests/benchmarks/gui/text/qtext/main.cpp +++ b/tests/benchmarks/gui/text/qtext/main.cpp @@ -51,6 +51,7 @@ #include Q_DECLARE_METATYPE(QTextDocument*) +Q_DECLARE_METATYPE(QList) class tst_QText: public QObject { @@ -80,6 +81,7 @@ private slots: void layout_data(); void layout(); + void formattedLayout_data(); void formattedLayout(); void paintLayoutToPixmap(); void paintLayoutToPixmap_painterFill(); @@ -328,28 +330,53 @@ void tst_QText::layout() } }*/ -void tst_QText::formattedLayout() +void tst_QText::formattedLayout_data() { - //set up formatting - QList ranges; + QTest::addColumn("text"); + QTest::addColumn >("ranges"); + + QTextCharFormat format; + format.setForeground(QColor("steelblue")); + { - QTextCharFormat format; - format.setForeground(QColor("steelblue")); + QList ranges; QTextLayout::FormatRange formatRange; formatRange.format = format; formatRange.start = 0; formatRange.length = 50; - ranges.append(formatRange); + + QTest::newRow("short-single") << m_shortLorem << ranges; + } + { + QList ranges; + + QString text = m_lorem.repeated(100); + const int width = 1; + for (int i = 0; i < text.size(); i += width) { + QTextLayout::FormatRange formatRange; + formatRange.format.setForeground(QBrush(QColor(i % 255, 255, 255))); + formatRange.start = i; + formatRange.length = width; + ranges.append(formatRange); + } + + QTest::newRow("long-many") << m_shortLorem << ranges; } +} - QTextLayout layout(m_shortLorem); +void tst_QText::formattedLayout() +{ + QFETCH(QString, text); + QFETCH(QList, ranges); + + QTextLayout layout(text); layout.setAdditionalFormats(ranges); setupTextLayout(&layout); QBENCHMARK { - QTextLayout layout(m_shortLorem); + QTextLayout layout(text); layout.setAdditionalFormats(ranges); setupTextLayout(&layout); } -- cgit v1.2.3