Commit f26e099a authored by Volker Krause's avatar Volker Krause

Introduce SharedValuePool, a little helper class that restores implicit

sharing on objects that are received in great numbers but only with a
small set of possible values. Use it for item mimetypes and flags,
reducing their memory use from O(n) to O(1) for larger folder listings.

svn path=/trunk/KDE/kdepimlibs/; revision=1181945
parent 27acc91c
......@@ -296,9 +296,9 @@ class ItemPrivate : public EntityPrivate
QString mMimeType;
Item::Flags mAddedFlags;
Item::Flags mDeletedFlags;
bool mFlagsOverwritten;
bool mSizeChanged;
bool mClearPayload;
bool mFlagsOverwritten : 1;
bool mSizeChanged : 1;
bool mClearPayload : 1;
};
}
......
......@@ -40,11 +40,17 @@ class Akonadi::ItemFetchJobPrivate : public JobPrivate
{
public:
ItemFetchJobPrivate( ItemFetchJob *parent )
: JobPrivate( parent )
: JobPrivate( parent ),
mValuePool( 0 )
{
mCollection = Collection::root();
}
~ItemFetchJobPrivate()
{
delete mValuePool;
}
void init()
{
Q_Q( ItemFetchJob );
......@@ -78,6 +84,7 @@ class Akonadi::ItemFetchJobPrivate : public JobPrivate
ItemFetchScope mFetchScope;
Item::List mPendingItems; // items pending for emitting itemsReceived()
QTimer* mEmitTimer;
ProtocolHelperValuePool *mValuePool;
};
void ItemFetchJobPrivate::startFetchJob()
......@@ -116,6 +123,7 @@ ItemFetchJob::ItemFetchJob( const Collection &collection, QObject * parent )
d->init();
d->mCollection = collection;
d->mValuePool = new ProtocolHelperValuePool; // only worth it for lots of results
}
ItemFetchJob::ItemFetchJob( const Item & item, QObject * parent)
......@@ -171,7 +179,7 @@ void ItemFetchJob::doHandleResponse( const QByteArray & tag, const QByteArray &
ImapParser::parseParenthesizedList( data, fetchResponse, begin + 6 );
Item item;
ProtocolHelper::parseItemFetchResult( fetchResponse, item );
ProtocolHelper::parseItemFetchResult( fetchResponse, item, d->mValuePool );
if ( !item.isValid() )
return;
......
......@@ -272,7 +272,7 @@ QByteArray ProtocolHelper::itemFetchScopeToByteArray( const ItemFetchScope &fetc
return command;
}
void ProtocolHelper::parseItemFetchResult( const QList<QByteArray> &lineTokens, Item &item )
void ProtocolHelper::parseItemFetchResult( const QList<QByteArray> &lineTokens, Item &item, ProtocolHelperValuePool *valuePool )
{
// create a new item object
Item::Id uid = -1;
......@@ -300,7 +300,10 @@ void ProtocolHelper::parseItemFetchResult( const QList<QByteArray> &lineTokens,
} else if ( key == "COLLECTIONID" ) {
cid = value.toInt();
} else if ( key == "MIMETYPE" ) {
mimeType = QString::fromLatin1( value );
if ( valuePool )
mimeType = valuePool->mimeTypePool.sharedValue( QString::fromLatin1( value ) );
else
mimeType = QString::fromLatin1( value );
}
}
......@@ -329,8 +332,16 @@ void ProtocolHelper::parseItemFetchResult( const QList<QByteArray> &lineTokens,
if ( key == "FLAGS" ) {
QList<QByteArray> flags;
ImapParser::parseParenthesizedList( lineTokens[i + 1], flags );
foreach ( const QByteArray &flag, flags ) {
item.setFlag( flag );
if ( !flags.isEmpty() ) {
Item::Flags convertedFlags;
convertedFlags.reserve( flags.size() );
foreach ( const QByteArray &flag, flags ) {
if ( valuePool )
convertedFlags.insert( valuePool->flagPool.sharedValue( flag ) );
else
convertedFlags.insert( flag );
}
item.setFlags( convertedFlags );
}
} else if ( key == "SIZE" ) {
const quint64 size = lineTokens[i + 1].toLongLong();
......
......@@ -25,6 +25,7 @@
#include <akonadi/collectionutils_p.h>
#include <akonadi/item.h>
#include <akonadi/itemfetchscope.h>
#include <akonadi/sharedvaluepool_p.h>
#include <akonadi/private/imapparser_p.h>
#include <akonadi/private/protocol_p.h>
......@@ -34,6 +35,15 @@
namespace Akonadi {
struct ProtocolHelperValuePool
{
typedef Internal::SharedValuePool<QByteArray, QVector> FlagPool;
typedef Internal::SharedValuePool<QString, QVector> MimeTypePool;
FlagPool flagPool;
MimeTypePool mimeTypePool;
};
/**
@internal
Helper methods for converting between libakonadi objects and their protocol
......@@ -195,7 +205,7 @@ class ProtocolHelper
/**
Parses a single line from an item fetch job result into an Item object.
*/
static void parseItemFetchResult( const QList<QByteArray> &lineTokens, Item &item );
static void parseItemFetchResult( const QList<QByteArray> &lineTokens, Item &item, ProtocolHelperValuePool *valuePool = 0 );
};
}
......
/*
Copyright (c) 2010 Volker Krause <vkrause@kde.org>
This library is free software; you can redistribute it and/or modify it
under the terms of the GNU Library General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at your
option) any later version.
This library is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.
*/
#ifndef AKONADI_SHAREDVALUEPOOL_P_H
#define AKONADI_SHAREDVALUEPOOL_P_H
#include <boost/utility/enable_if.hpp>
#include <algorithm>
namespace Akonadi {
namespace Internal {
template <typename T> class container_traits
{
private:
typedef char sizeOne;
typedef struct { char a[2]; } sizeTwo;
template <typename C> static sizeOne testForKeyType( typename C::key_type const* );
template <typename C> static sizeTwo testForKeyType( ... );
public:
enum { isAssociative = sizeof( container_traits<T>::testForKeyType<T>( 0 ) ) == 1 };
};
/**
* Pool of implicitly shared values, use for optimizing memory use
* when having a large amount of copies from a small set of different values.
*/
template <typename T, template <typename> class Container>
class SharedValuePool
{
public:
/** Returns the shared value equal to @p value .*/
template <typename C>
typename boost::enable_if_c<container_traits<Container<C> >::isAssociative, C>::type sharedValue( const C &value, const int* = 0 )
{
typename Container<T>::const_iterator it = m_pool.constFind( value );
if ( it != m_pool.constEnd() )
return *it;
m_pool.insert( value );
return value;
}
template <typename C>
typename boost::disable_if_c<container_traits<Container<C> >::isAssociative, C>::type sharedValue( const C &value )
{
// for small pool sizes this is actually faster than using lower_bound and a sorted vector
typename Container<T>::const_iterator it = std::find( m_pool.constBegin(), m_pool.constEnd(), value );
if ( it != m_pool.constEnd() )
return *it;
m_pool.push_back( value );
return value;
}
private:
Container<T> m_pool;
};
}
}
#endif
......@@ -144,6 +144,7 @@ add_akonadi_test(collectionutilstest.cpp)
add_akonadi_test(entitydisplayattributetest.cpp)
add_akonadi_test(proxymodelstest.cpp)
add_akonadi_test(actionstatemanagertest.cpp)
add_akonadi_test(sharedvaluepooltest.cpp)
# qtestlib tests that need non-exported stuff from akonadi-kde
kde4_add_unit_test(resourceschedulertest TESTNAME akonadi-resourceschedulertest resourceschedulertest.cpp ../resourcescheduler.cpp)
......
/*
Copyright (c) 2010 Volker Krause <vkrause@kde.org>
This library is free software; you can redistribute it and/or modify it
under the terms of the GNU Library General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at your
option) any later version.
This library is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.
*/
#include "../sharedvaluepool_p.h"
#include <qtest_kde.h>
#include <QVector>
#include <QSet>
#include <set>
#include <vector>
using namespace Akonadi;
class SharedValuePoolTest : public QObject
{
Q_OBJECT
private slots:
void testQVector_data()
{
QTest::addColumn<int>( "size" );
QTest::newRow( "10" ) << 10;
QTest::newRow( "100" ) << 100;
}
void testQVector()
{
QFETCH( int, size );
QVector<QByteArray> data;
Internal::SharedValuePool<QByteArray, QVector> pool;
for ( int i = 0; i < size; ++i ) {
QByteArray b( 10, (char)i );
data.push_back( b );
QCOMPARE( pool.sharedValue( b ), b );
QCOMPARE( pool.sharedValue( b ), b );
}
QBENCHMARK {
foreach ( const QByteArray &b, data )
pool.sharedValue( b );
}
}
void testQSet_data()
{
QTest::addColumn<int>( "size" );
QTest::newRow( "10" ) << 10;
QTest::newRow( "100" ) << 100;
}
void testQSet()
{
QFETCH( int, size );
QVector<QByteArray> data;
Internal::SharedValuePool<QByteArray, QSet> pool;
for ( int i = 0; i < size; ++i ) {
QByteArray b( 10, (char)i );
data.push_back( b );
QCOMPARE( pool.sharedValue( b ), b );
QCOMPARE( pool.sharedValue( b ), b );
}
QBENCHMARK {
foreach ( const QByteArray &b, data )
pool.sharedValue( b );
}
}
};
QTEST_KDEMAIN( SharedValuePoolTest, NoGUI )
#include "sharedvaluepooltest.moc"
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment