List Info

Thread: Value-based classes: struct vs shared-data




Value-based classes: struct vs shared-data
user name
2006-09-29 12:31:35
I'm considering changing KFileItem to be a value-based
class, to solve ownership issues that lead to crashes in
kde3.

The first question that came to mind was: should I make it
ref-counted (QSharedData) 
or just a simple struct-like set of members given that those
members use implicitly
shared types anyway (QString etc.)

Obviously, struct-like means cheaper constructor (no new),
but more expensive copy constructor (has to change many
refcounts instead of one).

So the question is how that balance goes - which solution
offers the best compromise.

I wrote the test program below, and I got the following
results:

QDEBUG : KFileItemTest::testPerformance() CTOR: Simple
struct: 20
QDEBUG : KFileItemTest::testPerformance() CTOR: With
QSharedDataPointer: 24
QDEBUG : KFileItemTest::testPerformance() COPY CTOR: Simple
struct (direct copy of the data): 11
QDEBUG : KFileItemTest::testPerformance() COPY CTOR: With
QSharedDataPointer: 1

The copy constructor is -10- times slower with the
individual members have to be
copied, despite the implicit sharing for most of them (and
the others are basic integral types).
The normal constructor is 20% slower. Looks like QSharedData
is the best solution then.

Any mistakes in my test? ;)

====

class TestKFileItemPrivate : public QSharedData
{
public:
  KIO::UDSEntry m_entry;
  KUrl m_url;
  QString m_strName;
  QString m_strText;
  mutable QString m_user, m_group;
  mutable QString m_strLowerCaseName;
  KMimeType::Ptr m_pMimeType;
  mode_t m_fileMode;
  mode_t m_permissions;
  bool m_bMarked:1;
  bool m_bLink:1;
  bool m_bIsLocalUrl:1;
  bool m_bMimeTypeKnown:1;
  enum { Auto, Hidden, Shown } m_hidden:3;
  QString m_guessedMimeType;
  mutable QString m_access;
  QString m_iconName;
  QMap<const void*, void*> m_extra;
  mutable KFileMetaInfo m_metaInfo;
  enum { Modification = 0, Access = 1, Creation = 2,
NumFlags = 3 };
  mutable time_t m_time[3];
  mutable KIO::filesize_t m_size;
};

class TestKFileItem
{
public:
    TestKFileItem() { d = new TestKFileItemPrivate;
d->m_bMarked = true; }

    bool marked() const { return d->m_bMarked; }
private:
    QSharedDataPointer<TestKFileItemPrivate> d;
};

#include <time.h>
#include <sys/time.h>

void KFileItemTest::testPerformance()
{
    static const int count = 10000000;


    // Normal constructor
    {
        time_t start = time(0);
        for (uint i = 0; i < count; ++i) {
            TestKFileItemPrivate item;
            item.m_bMarked = true;
            Q_ASSERT(item.m_bMarked);
        }
        qDebug("CTOR: Simple struct: %ld", time(0)
- start);
    }

    {
        time_t start = time(0);
        for (uint i = 0; i < count; ++i) {
            TestKFileItem item;
            Q_ASSERT(item.marked());
        }
        qDebug("CTOR: With QSharedDataPointer:
%ld", time(0) - start);
    }

    // Copy constructor
    {
        TestKFileItemPrivate item;
        item.m_bMarked = true;

        time_t start = time(0);
        for (uint i = 0; i < count; ++i) {
            TestKFileItemPrivate item2 = item;
            Q_ASSERT(item2.m_bMarked);
        }
        qDebug("COPY CTOR: Simple struct (direct copy
of the data): %ld", time(0) - start);
    }

    {
        TestKFileItem item;
        time_t start = time(0);
        for (uint i = 0; i < count; ++i) {
            TestKFileItem item2 = item;
            Q_ASSERT(item2.marked());
        }
        qDebug("COPY CTOR: With QSharedDataPointer:
%ld", time(0) - start);
    }
}



-- 
David Faure, faurekde.org, sponsored by Trolltech to work on
KDE,
Konqueror (http://www.konqueror.org
), and KOffice (http://www.koffice.org).

_______________________________________________
Kde-optimize mailing list
Kde-optimizekde.org
ht
tps://mail.kde.org/mailman/listinfo/kde-optimize
Value-based classes: struct vs shared-data
user name
2006-09-29 20:54:54
On Friday 29 September 2006 14:31, David Faure wrote:

> Any mistakes in my test? ;)
>

You did not tell what compiler (version) and options you
used.
But I guess it was unoptimized...

/RogerL
_______________________________________________
Kde-optimize mailing list
Kde-optimizekde.org
ht
tps://mail.kde.org/mailman/listinfo/kde-optimize
Value-based classes: struct vs shared-data
user name
2006-09-29 22:39:27
On Friday 29 September 2006 22:54, Roger Larsson wrote:
> On Friday 29 September 2006 14:31, David Faure wrote:
> 
> > Any mistakes in my test? ;)
> >
> 
> You did not tell what compiler (version) and options
you used.
> But I guess it was unoptimized...

gcc-4.0.3, default kde-trunk buildsystem options with
debugfull enabled.

Let's see what happens with -O2 instead of -g3
-fno-inline...

QDEBUG : KFileItemTest::testPerformance() CTOR: Simple
struct: 16
QDEBUG : KFileItemTest::testPerformance() CTOR: With
QSharedDataPointer: 18
QDEBUG : KFileItemTest::testPerformance() COPY CTOR: Simple
struct: 5
QDEBUG : KFileItemTest::testPerformance() COPY CTOR: With
QSharedDataPointer: 1

Hey that's pretty good. The performance hit for the default
ctor is down to 12%
while the performance hit for the copy ctor when not using
qshareddatapointer 
is still very high (around 500%, although with '1' the
number is too small to mean anything).
This seems to confirm that I should go ahead with
QSharedDataPointer.

-- 
David Faure, faurekde.org, sponsored by Trolltech to work on
KDE,
Konqueror (http://www.konqueror.org
), and KOffice (http://www.koffice.org).

_______________________________________________
Kde-optimize mailing list
Kde-optimizekde.org
ht
tps://mail.kde.org/mailman/listinfo/kde-optimize
[1-3]

about | contact  Other archives ( Real Estate discussion Medical topics )