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