Why do moest DUContext functions return a QVector and most DUChain use QList?
David Nolden
zwabel at googlemail.com
Sun Oct 25 22:09:07 UTC 2009
Am Sonntag 25 Oktober 2009 23:00:46 schrieb Milian Wolff:
> Hi there, especially David once again :)
>
> Could you elaborate why most of the DUContext methods return a QVector of
>
> pointers, and not a QList? To quote the documentation:
> > For most purposes, QList is the right class to use. Operations like
> > prepend() and insert() are usually faster than with QVector because of
> > the way QList stores its items in memory (see Algorithmic Complexity for
> > details), and its index-based API is more convenient than QLinkedList's
> > iterator-based API. It also expands to less code in your executable.
>
> And (emphasise added)
>
> > If you want the items to occupy adjacent memory positions, or if your
> > items are **larger than a pointer** and you want to avoid the overhead of
> > allocating them on the heap individually at insertion time, then use
> > QVector.
>
> There are lots of methods that return lists of pointers in a QVector. If I
> read the docs, I'd say those should be QLists? Of course things like
> importedParentContexts should stay the same.
A QVector is always faster and takes less memory than a QList, and since the
duchain handles very light-weight objects, that does count there.
In some places also QVector is used mainly because in the background the data
is saved in a QVector, so that the function can return a "const QVector<...>&"
instead of copying the object over.
QList is a "one fits all" container that is good for most usecases, but not
when maximum performance is required. Ideally the whole duchain would not use
any of those high-level container classes at all.
Greetings, DAvid
More information about the KDevelop-devel
mailing list