Use QPixmapCache for KStandardItemListWidget::pixmapForIcon

Review Request #107039 - Created Oct. 25, 2012 and submitted

Emmanuel Pescosta
Use QPixmapCache for KStandardItemListWidget::pixmapForIcon(const QString& name, int size) -> Avoid KIcon loading and rescaleing
According to valgrind/callgrind lesser cpu time and according to valgrind/massif lesser memory consume


  • 1
  • 0
  • 0
  • 1
Description From Last Updated
Just to be sure that we don't get accidental key collisons between "icon1" with size 25 and "icon12" with size ... Frank Reininghaus Frank Reininghaus
Frank Reininghaus
Commit Hook
Emmanuel Pescosta
Review request changed

Status: Closed (submitted)

Christoph Feck
Sorry, QPixmapCache is obsolete, please port to KImageCache.
  1. Before you ask, here is what the API docs say:
    KPixmapCache is susceptible to various non-trivial locking bugs and inefficiencies, and is supported for backward compatibility only (since it exposes a QDataStream API for subclasses). Users should port to KImageCache for a very close work-alike, or KSharedDataCache if they need more control.
  2. In fairness, QPixmapCache is nowhere near the same as KPixmapCache. QPixmapCache is a thread-shared cache (i.e. different processes will have different QPixmapCaches). There are plausible scenarios where you might not need either the additional features or different semantics of KImageCache.
    However, given that QPixmapCache has no means for namespacing it should not be your first choice for library code (e.g. if the library adds a pixmap called "icon_back", what happens if an application using that library also tries to add or use a pixmap called "icon_back"). If used in library code the key should be modified to include a namespace element e.g. key = QLatin1String("org.kde.KSILW.") + key. But doing this will make the cache itself slower as it has to fight with many different elements with a long common prefix (using a common suffix is probably OK but introduces a minor risk of collision again).
  3. AHHHH... sorry, indeed this is about QPixmapCache, not KPixmapCache. So just forget my comment.
  4. Thanks for your comments, Christoph and Michael!
    Hm, it was my understanding that KSharedDataCache stores the cache contents on disk, whereas QPixmapCache does in in memory. Therefore, I thought that using KSharedDataCache might not bring the same performance increase as QPixmapCache for our limited use case. On the other hand, it might enable us to share the pixmaps between different Dolphin processes. It seems that we need to do some careful profiling to see what works out better. I think we're quite safe with QPixmapCache at the moment though because it is only used in this single place in Dolphin.
    Michael, I'm not quite sure why adding the namespace element at the beginning or at the end of the key would make a difference. It was my understanding that the string is passed to a hash function, which reads the entire string anyway.
  5. KSharedDataCache uses a disk file but doesn't actually ever force the OS to commit to disk until the KSharedDataCache is later destroyed. The actual cache updates are done all in memory, which the OS may page to disk at its leisure. However because it is process-shared and not just thread-shared it is probably still not as efficient as what you could get with Qt or STL maps or hashes. The balance really just depends on whether you expect many different processes to do the same intensive cacheable work or not. If processes will normally do work that does not help other processes (or if the work doesn't take so long that it would be nice to persist across multiple process executions) then KSharedDataCache is probably not useful.
    As far as the [namespace,key] question, it's true that using a hash table instead of a map would tend to make the point moot. I think I've heard of hash table implementations where long common prefixes actually increase the risk of hash collisions though, but I'm not up to researching that right now. I do think I was incorrect on using a suffix introducing a collision risk, I think that's related to crypto hash signature functions instead on second thought.
  6. Just a little note that everyone seems to forget..
    KImageCache = file cache (as is mentioned in here) and also pixmap caching  IF a QPixmap is inserted! Then it's using QCache. However, it defaults to just 16 KiB (why so small?) so you can use it just fine, just increase the size.