R – Performance issue with QGraphicsScene::createItemGroup


I'm using the Qt graphics API to display layers in some GIS software.

Each layer is a group, containing graphic primitives. I have a
performance issue when loading fairly large data sets, for example
this is what happens when making a group composed of ~96k circular
paths (points from a shapefile):

callgrind image http://f.imagehost.org/0750/profile-createItemGroup.png

The complete callgrind dump is here.

The QGraphicsScene::createItemGroup() call takes about 150 seconds to
complete on my 2.4GHz core2, and it seems all this time is used in
QGraphicsItemPrivate::updateEffectiveOpacity(), which itself consumes
37% of its time calling QGraphicsItem::flags() 4 billion times (the
data comes from a test script with no GUI, just a scene, not even tied
to a view).

All the rest is pretty much instantaneous (creating the items,
reading the file, etc…). I tried to disable the scene's index before
creating the group and obtained similar results.

What could I do to improve performances in this case ? If I can't is there a way to create groups faster ?

Best Solution

After studying the source code a little bit, I found out that the updateEffectiveOpacity has O(n²) with regard to the children of the item's parent item (search for the method qt_allChildrenCombineOpacity). This is probably also the reason that method disappeared in Qt 4.6 and apparently been replaced by something else. Anyway, you should try out setting the ItemDoesntPropagateOpacityToChildren flag on the group item (i.e. you'll have to create it yourself), at least while adding all the items.