Rex Dieter adf30a
From c733429f4fa9696fb027ddc946e54f6bbb68deaf Mon Sep 17 00:00:00 2001
Rex Dieter adf30a
From: Milian Wolff <mail@milianw.de>
Rex Dieter adf30a
Date: Wed, 10 Dec 2014 21:16:45 +0100
Rex Dieter adf30a
Subject: [PATCH 30/30] Preallocate a capacity of 16 for the returned list.
Rex Dieter adf30a
Rex Dieter adf30a
See also 159abaf2f372eaa633db8f69ff6b1edd459998cc in kdepimlibs on
Rex Dieter adf30a
why. I'll quote it here again:
Rex Dieter adf30a
Rex Dieter adf30a
    In my data, most often the final size of fetchResponse is 16.
Rex Dieter adf30a
    By reserving that data upfront, we can get rid of 3/4 of the
Rex Dieter adf30a
    list allocations, according to the growth-strategy outlined here:
Rex Dieter adf30a
    http://qt-project.org/doc/qt-4.8/containers.html#growth-strategies
Rex Dieter adf30a
    I.e. before, we'd allocate 4, 8, 12, 16. Now we directly allocate
Rex Dieter adf30a
    room for 16.
Rex Dieter adf30a
Rex Dieter adf30a
Thing is, doing this outside of akonadi has no effect, as QList::clear
Rex Dieter adf30a
destroys the internal buffer. With the added benchmark, I now verified
Rex Dieter adf30a
that this patch here does have a positive effect.
Rex Dieter adf30a
---
Rex Dieter adf30a
 libs/imapparser.cpp                |  2 ++
Rex Dieter adf30a
 libs/tests/imapparserbenchmark.cpp | 52 ++++++++++++++++++++++++++------------
Rex Dieter adf30a
 2 files changed, 38 insertions(+), 16 deletions(-)
Rex Dieter adf30a
Rex Dieter adf30a
diff --git a/libs/imapparser.cpp b/libs/imapparser.cpp
Rex Dieter adf30a
index f3301e7..f5a7457 100644
Rex Dieter adf30a
--- a/libs/imapparser.cpp
Rex Dieter adf30a
+++ b/libs/imapparser.cpp
Rex Dieter adf30a
@@ -79,6 +79,8 @@ int parseParenthesizedListHelper( const QByteArray &data, T &result, int start )
Rex Dieter adf30a
     return start;
Rex Dieter adf30a
   }
Rex Dieter adf30a
 
Rex Dieter adf30a
+  result.reserve(16);
Rex Dieter adf30a
+
Rex Dieter adf30a
   int count = 0;
Rex Dieter adf30a
   int sublistBegin = start;
Rex Dieter adf30a
   bool insideQuote = false;
Rex Dieter adf30a
diff --git a/libs/tests/imapparserbenchmark.cpp b/libs/tests/imapparserbenchmark.cpp
Rex Dieter adf30a
index ee861a0..7545238 100644
Rex Dieter adf30a
--- a/libs/tests/imapparserbenchmark.cpp
Rex Dieter adf30a
+++ b/libs/tests/imapparserbenchmark.cpp
Rex Dieter adf30a
@@ -27,6 +27,25 @@ Q_DECLARE_METATYPE( QList<QByteArray> )
Rex Dieter adf30a
 class ImapParserBenchmark : public QObject
Rex Dieter adf30a
 {
Rex Dieter adf30a
   Q_OBJECT
Rex Dieter adf30a
+  private:
Rex Dieter adf30a
+    void geneateParseParenthesizedListData()
Rex Dieter adf30a
+    {
Rex Dieter adf30a
+      QTest::addColumn<QByteArray>( "data" );
Rex Dieter adf30a
+      QTest::newRow( "empty" ) << QByteArray();
Rex Dieter adf30a
+      QTest::newRow( "unnested" ) << QByteArray("(\"Foo Bar\" NIL \"foobar\" \"test.com\")");
Rex Dieter adf30a
+      QTest::newRow( "nested" ) << QByteArray("((\"Foo Bar\" NIL \"foobar\" \"test.com\"))");
Rex Dieter adf30a
+      QTest::newRow( "nested-long" ) << QByteArray("(UID 86 REV 0 MIMETYPE \"message/rfc822\" COLLECTIONID 13 SIZE 6114 FLAGS (\\SEEN)"
Rex Dieter adf30a
+                                                   " ANCESTORS ((13 \"/INBOX\") (12 \"imap://mail@mail.test.com/\") (0 \"\")) PLD:ENVELOPE[1] {396}"
Rex Dieter adf30a
+                                                   " (\"Fri, 04 Jun 2010 09:07:54 +0200\" \"Re: [ADMIN] foobar available again!\""
Rex Dieter adf30a
+                                                   " ((\"Foo Bar\" NIL \"foobar\" \"test.com\"))"
Rex Dieter adf30a
+                                                   " NIL NIL"
Rex Dieter adf30a
+                                                   " ((\"Asdf Bla Blub\" NIL \"asdf.bla.blub\" \"123test.org\"))"
Rex Dieter adf30a
+                                                   " ((NIL NIL \"muh.kuh\" \"lalala.com\") (\"Konqi KDE\" NIL \"konqi\" \"kde.org\") (NIL NIL \"all\" \"test.com\"))"
Rex Dieter adf30a
+                                                   " NIL \"<201006040905.33367.foo.bar@test.com>\" \"<4C08A64A.9020205@123test.org>\""
Rex Dieter adf30a
+                                                   " \"<201006040142.56540.muh.kuh@lalala.com> <201006040704.39648.konqi@kde.org> <201006040905.33367.foo.bar@test.com>\""
Rex Dieter adf30a
+                                                   "))");
Rex Dieter adf30a
+    }
Rex Dieter adf30a
+
Rex Dieter adf30a
   private Q_SLOTS:
Rex Dieter adf30a
     void quote_data()
Rex Dieter adf30a
     {
Rex Dieter adf30a
@@ -68,25 +87,12 @@ class ImapParserBenchmark : public QObject
Rex Dieter adf30a
       }
Rex Dieter adf30a
     }
Rex Dieter adf30a
 
Rex Dieter adf30a
-    void parseParenthesizedList_data()
Rex Dieter adf30a
+    void parseParenthesizedQVarLengthArray_data()
Rex Dieter adf30a
     {
Rex Dieter adf30a
-      QTest::addColumn<QByteArray>( "data" );
Rex Dieter adf30a
-      QTest::newRow( "empty" ) << QByteArray();
Rex Dieter adf30a
-      QTest::newRow( "unnested" ) << QByteArray("(\"Foo Bar\" NIL \"foobar\" \"test.com\")");
Rex Dieter adf30a
-      QTest::newRow( "nested" ) << QByteArray("((\"Foo Bar\" NIL \"foobar\" \"test.com\"))");
Rex Dieter adf30a
-      QTest::newRow( "nested-long" ) << QByteArray("(UID 86 REV 0 MIMETYPE \"message/rfc822\" COLLECTIONID 13 SIZE 6114 FLAGS (\\SEEN)"
Rex Dieter adf30a
-                                                   " ANCESTORS ((13 \"/INBOX\") (12 \"imap://mail@mail.test.com/\") (0 \"\")) PLD:ENVELOPE[1] {396}"
Rex Dieter adf30a
-                                                   " (\"Fri, 04 Jun 2010 09:07:54 +0200\" \"Re: [ADMIN] foobar available again!\""
Rex Dieter adf30a
-                                                   " ((\"Foo Bar\" NIL \"foobar\" \"test.com\"))"
Rex Dieter adf30a
-                                                   " NIL NIL"
Rex Dieter adf30a
-                                                   " ((\"Asdf Bla Blub\" NIL \"asdf.bla.blub\" \"123test.org\"))"
Rex Dieter adf30a
-                                                   " ((NIL NIL \"muh.kuh\" \"lalala.com\") (\"Konqi KDE\" NIL \"konqi\" \"kde.org\") (NIL NIL \"all\" \"test.com\"))"
Rex Dieter adf30a
-                                                   " NIL \"<201006040905.33367.foo.bar@test.com>\" \"<4C08A64A.9020205@123test.org>\""
Rex Dieter adf30a
-                                                   " \"<201006040142.56540.muh.kuh@lalala.com> <201006040704.39648.konqi@kde.org> <201006040905.33367.foo.bar@test.com>\""
Rex Dieter adf30a
-                                                   "))");
Rex Dieter adf30a
+      geneateParseParenthesizedListData();
Rex Dieter adf30a
     }
Rex Dieter adf30a
 
Rex Dieter adf30a
-    void parseParenthesizedList()
Rex Dieter adf30a
+    void parseParenthesizedQVarLengthArray()
Rex Dieter adf30a
     {
Rex Dieter adf30a
       QFETCH( QByteArray, data );
Rex Dieter adf30a
       QVarLengthArray<QByteArray, 16> result;
Rex Dieter adf30a
@@ -95,6 +101,20 @@ class ImapParserBenchmark : public QObject
Rex Dieter adf30a
       }
Rex Dieter adf30a
     }
Rex Dieter adf30a
 
Rex Dieter adf30a
+    void parseParenthesizedQList_data()
Rex Dieter adf30a
+    {
Rex Dieter adf30a
+      geneateParseParenthesizedListData();
Rex Dieter adf30a
+    }
Rex Dieter adf30a
+
Rex Dieter adf30a
+    void parseParenthesizedQList()
Rex Dieter adf30a
+    {
Rex Dieter adf30a
+      QFETCH( QByteArray, data );
Rex Dieter adf30a
+      QList<QByteArray> result;
Rex Dieter adf30a
+      QBENCHMARK {
Rex Dieter adf30a
+        ImapParser::parseParenthesizedList( data, result, 0 );
Rex Dieter adf30a
+      }
Rex Dieter adf30a
+    }
Rex Dieter adf30a
+
Rex Dieter adf30a
     void parseString_data()
Rex Dieter adf30a
     {
Rex Dieter adf30a
       QTest::addColumn<QByteArray>( "data" );
Rex Dieter adf30a
-- 
Rex Dieter adf30a
2.1.0
Rex Dieter adf30a