From afa56dcf12b520606b309d71dca67b372728a819 Mon Sep 17 00:00:00 2001
From: Alexandre Lision <alexandre.lision@gmail.com>
Date: Sun, 8 May 2016 17:30:20 -0400
Subject: [PATCH] images: cache contact photos

Use a QHash to store contact photos.
Is saves around 25% of memory used at startup

Note: if contact photo is updated on the system it will only reflect
in Ring at next startup due to this caching

Tuleap: #663
Change-Id: I10bf90b8425efcdf120b16d088a56be015ffdda3
---
 src/delegates/ImageManipulationDelegate.h  | 1 +
 src/delegates/ImageManipulationDelegate.mm | 9 +++++++++
 2 files changed, 10 insertions(+)

diff --git a/src/delegates/ImageManipulationDelegate.h b/src/delegates/ImageManipulationDelegate.h
index 9605ba39..b99cd6ae 100644
--- a/src/delegates/ImageManipulationDelegate.h
+++ b/src/delegates/ImageManipulationDelegate.h
@@ -63,6 +63,7 @@ namespace Interfaces {
         CGImageRef resizeCGImage(CGImageRef image, const QSize& size);
 
         QHash<QString, QPixmap> m_hDefaultUserPixmap;
+        QHash<QString, QPixmap> m_hContactsPixmap;
 
         /**
          * Return a version of size destSize centered of the bigger photo
diff --git a/src/delegates/ImageManipulationDelegate.mm b/src/delegates/ImageManipulationDelegate.mm
index 7780d947..082c9416 100644
--- a/src/delegates/ImageManipulationDelegate.mm
+++ b/src/delegates/ImageManipulationDelegate.mm
@@ -46,6 +46,13 @@ namespace Interfaces {
     QVariant ImageManipulationDelegate::contactPhoto(Person* c, const QSize& size, bool displayPresence) {
         const int radius = size.height() / 2;
 
+        auto index = QStringLiteral("%1%2%3").arg(size.width())
+                                            .arg(size.height())
+                                            .arg(QString::fromUtf8(c->uid()));
+        if (m_hContactsPixmap.contains(index)) {
+            return m_hContactsPixmap.value(index);
+        }
+
         QPixmap pxm;
         if (c && c->photo().isValid()) {
             QPixmap contactPhoto(qvariant_cast<QPixmap>(c->photo()).scaled(size, Qt::KeepAspectRatioByExpanding,
@@ -87,6 +94,8 @@ namespace Interfaces {
             pxm = drawDefaultUserPixmap(size);
         }
 
+        m_hContactsPixmap.insert(index, pxm);
+
         return pxm;
     }
 
-- 
GitLab