1 From 89357c7b0fc5e76091510af504058c036fa1b2f9 Mon Sep 17 00:00:00 2001
2 From: Milian Wolff <mail@milianw.de>
3 Date: Wed, 10 Dec 2014 21:04:24 +0100
4 Subject: [PATCH 29/30] Reduce the amount of allocations by preallocating a
7 Sadly, QByteArray cannot be cleared without destroying the buffer.
8 But we can guesstimate the target size of the buffer and thus
9 reduce the amount of allocations.
11 This also adds a benchmark for ImapParser::parseString.
13 libs/imapparser.cpp | 1 +
14 libs/tests/imapparserbenchmark.cpp | 19 +++++++++++++++++++
15 2 files changed, 20 insertions(+)
17 diff --git a/libs/imapparser.cpp b/libs/imapparser.cpp
18 index 6f9f592..f3301e7 100644
19 --- a/libs/imapparser.cpp
20 +++ b/libs/imapparser.cpp
21 @@ -186,6 +186,7 @@ int ImapParser::parseQuotedString( const QByteArray &data, QByteArray &result, i
23 if ( data[begin] == '"' ) {
25 + result.reserve(qMin(32, data.size() - begin));
26 for ( int i = begin; i < data.length(); ++i ) {
27 const char ch = data.at( i );
29 diff --git a/libs/tests/imapparserbenchmark.cpp b/libs/tests/imapparserbenchmark.cpp
30 index fd4335c..ee861a0 100644
31 --- a/libs/tests/imapparserbenchmark.cpp
32 +++ b/libs/tests/imapparserbenchmark.cpp
33 @@ -95,6 +95,25 @@ class ImapParserBenchmark : public QObject
37 + void parseString_data()
39 + QTest::addColumn<QByteArray>( "data" );
40 + QTest::newRow("plain") << QByteArray("fooobarasdf something more lalala");
41 + QTest::newRow("quoted") << QByteArray("\"fooobarasdf\" something more lalala");
46 + QFETCH(QByteArray, data);
50 + sum += ImapParser::parseString( data, result );
52 + QVERIFY(!result.isEmpty());
58 QByteArray data( "123456" );