[Development] QML pixmap caching

David Faure david.faure at kdab.com
Mon Jul 23 16:41:59 CEST 2012


QDeclarativePixmapStore (called QQuickPixmapStore in Qt5, but it's the same 
code) seems to throw away cached pixmaps, every 30 seconds, even if the cache 
is far from full.

This creates a performance issue when an image are being loaded during 
animations, and was supposed to be in cache already.

Here's my analysis of qdeclarativepixmapcache.cpp:

The timer (called 30s after a pixmap becomes unused)g calls 
    shrinkCache(_unreferencedCost / CACHE_REMOVAL_FRACTION);
i.e. it wants to remove 1/4 of the unused pixmap data.

And the shrinkCache method, if called with a positive number, will always 
remove at least one element from the cache:

    while ((remove > 0 || [...]) && m_lastUnreferencedPixmap) {
        // remove last unreferenced pixmap
    }


So if you have a 10MB cache and an Image element that alternatives between two 
states by changing its "source" property, each image being 1MB, then it would 
all nicely fit into the cache, but shrinkCache will be called with an argument 
of remove=250KB and will delete the currently-unused image from the cache. 

Of course it's a matter of tradeoffs -- do you want to use more RAM to save on 
IO+CPU, or to minimize RAM usage, at the expense of IO+CPU the next time the 
image is necessary....
It's just that in the use case I'm seeing here, loading images from the SD 
card is too costly, so keeping them in RAM would be preferred.

So maybe this should be configurable?

Or is the only option to create my own QDeclarativeImageProvider which keeps 
everything in memory?

I'll do that for now, but it seems like there should be an easier way to tell 
QML "keep stuff in cache as long as the cache isn't too big" (and the cache 
size could possibly be made configurable, too, like in QPixmapCache).

-- 
David Faure | david.faure at kdab.com | Managing Director KDAB France
KDAB (France) S.A.S., a KDAB Group company
Tel. France +33 (0)4 90 84 08 53, Sweden (HQ) +46-563-540090
KDAB - Qt Experts - Platform-independent software solutions




More information about the Development mailing list