[Development] OK to add zero/bulk copy to QVector?

Konstantin Ritt ritt.ks at gmail.com
Mon Dec 30 01:45:48 CET 2013


There is a plan to add  "static QVector<T> QVector::fromRawData(const T
*data, int size)". Though, I'm not sure if we can do this until 6.0.

Regards,
Konstantin


2013/12/30 Jiergir Ogoerg <f35f22fan at gmail.com>

> QVector is created before I can load the array on the heap, so I can't
> init it to the desired
> item count.
> Particularly I'm talking about QVector<QComposeTableElement> m_composeTable
> from
>
> qt5/qtbase/src/plugins/platforminputcontexts/compose/generator/qtablegenerator.h
>
> And because of that I have to (can) resize() it, and resizing sucks
> because it not just creates
> more space for the internal buffer but also initializes each item to
> the default value.
>
> The other option to resize() is reserve, but it doesn't also increase
> the item count of the vector so
> I still have to go over each element and push_back().
>
> Just setting the whole thing with one qvector->setBuffer(array) would
> be quicker and faster.
>
> The example:
> The actual source code is attached (as zip file) in the file
> qtablegenerator.cpp, at static function loadCache(..),
> the quickest approach I could figure out was to use reserve() and then
> push_back() on each item,
> which still does lots of copies:
>
> QComposeTableElement *elem;
>     vec->reserve(kElemCount);
>
>     for(int i=0; i<kElemCount; i++) {
>         elem = (QComposeTableElement*) (buf + (i * kElemSize));
>         vec->push_back(*elem);
>     }
>
>
>
>
> On Sun, Dec 29, 2013 at 11:44 PM, Yves Bailly <yves.bailly at laposte.net>
> wrote:
> > On 29/12/2013 20:10, Jiergir Ogoerg wrote:
> >> Hi,
> >> there are 2 simple options to speed up setting/adding elements to a
> >> vector when a heap buffer is filled in in advance:
> >> 1) Bulk copy.
> >> Assign a bunch of elements at once (from another vector or array),
> >> like std::vector::assign() does.
> >> A big extra copy happens, but avoids multiple calls to push_back().
> >
> > What about this, re-using your example:
> > my_struct *arr = new my_struct[count];
> > QVector<my_struct> v(count);
> > memcpy(v.data(), arr, sizeof(my_struct)*count);
> >
> > Assuming "my_struct" doesn't contain self-referencing references or
> > virtual table.
> >
> > Regards,
> >
> > --
> > (o< | Yves Bailly                          | -o)
> > //\ | Linux Dijon  : http://www.coagul.org | //\
> > \_/ |                                      | \_/`
> > _______________________________________________
> > Development mailing list
> > Development at qt-project.org
> > http://lists.qt-project.org/mailman/listinfo/development
>
> _______________________________________________
> Development mailing list
> Development at qt-project.org
> http://lists.qt-project.org/mailman/listinfo/development
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/development/attachments/20131230/1dcf3292/attachment.html>


More information about the Development mailing list