From b1e35a685c7d02242ad171953614434c7474d051 Mon Sep 17 00:00:00 2001
From: Aleksander Machniak <alec@alec.pl>
Date: Tue, 12 May 2015 14:33:02 -0400
Subject: [PATCH] Performance: Improve changes detection, so we update index only if needed

---
 program/lib/Roundcube/rcube_cache_shared.php |   19 ++++++++++++-------
 program/lib/Roundcube/rcube_cache.php        |   19 ++++++++++++-------
 2 files changed, 24 insertions(+), 14 deletions(-)

diff --git a/program/lib/Roundcube/rcube_cache.php b/program/lib/Roundcube/rcube_cache.php
index dffb600..41f7b33 100644
--- a/program/lib/Roundcube/rcube_cache.php
+++ b/program/lib/Roundcube/rcube_cache.php
@@ -43,6 +43,7 @@
     private $packed;
     private $index;
     private $debug;
+    private $index_changed = false;
     private $cache         = array();
     private $cache_changes = array();
     private $cache_sums    = array();
@@ -118,7 +119,6 @@
     function set($key, $data)
     {
         $this->cache[$key]         = $data;
-        $this->cache_changed       = true;
         $this->cache_changes[$key] = true;
     }
 
@@ -167,7 +167,6 @@
         // Remove all keys
         if ($key === null) {
             $this->cache         = array();
-            $this->cache_changed = false;
             $this->cache_changes = array();
             $this->cache_sums    = array();
         }
@@ -227,10 +226,6 @@
      */
     function close()
     {
-        if (!$this->cache_changed) {
-            return;
-        }
-
         foreach ($this->cache as $key => $data) {
             // The key has been used
             if ($this->cache_changes[$key]) {
@@ -244,7 +239,9 @@
             }
         }
 
-        $this->write_index();
+        if ($this->index_changed) {
+            $this->write_index();
+        }
     }
 
 
@@ -486,6 +483,10 @@
             }
         }
 
+        if ($result) {
+            $this->index_changed = true;
+        }
+
         return $result;
     }
 
@@ -513,6 +514,10 @@
             }
         }
 
+        if ($result) {
+            $this->index_changed = true;
+        }
+
         if ($index) {
             if (($idx = array_search($key, $this->index)) !== false) {
                 unset($this->index[$idx]);
diff --git a/program/lib/Roundcube/rcube_cache_shared.php b/program/lib/Roundcube/rcube_cache_shared.php
index 701f8d7..a85912b 100644
--- a/program/lib/Roundcube/rcube_cache_shared.php
+++ b/program/lib/Roundcube/rcube_cache_shared.php
@@ -42,6 +42,7 @@
     private $index;
     private $table;
     private $debug;
+    private $index_changed = false;
     private $cache         = array();
     private $cache_changes = array();
     private $cache_sums    = array();
@@ -115,7 +116,6 @@
     function set($key, $data)
     {
         $this->cache[$key]         = $data;
-        $this->cache_changed       = true;
         $this->cache_changes[$key] = true;
     }
 
@@ -164,7 +164,6 @@
         // Remove all keys
         if ($key === null) {
             $this->cache         = array();
-            $this->cache_changed = false;
             $this->cache_changes = array();
             $this->cache_sums    = array();
         }
@@ -222,10 +221,6 @@
      */
     function close()
     {
-        if (!$this->cache_changed) {
-            return;
-        }
-
         foreach ($this->cache as $key => $data) {
             // The key has been used
             if ($this->cache_changes[$key]) {
@@ -239,7 +234,9 @@
             }
         }
 
-        $this->write_index();
+        if ($this->index_changed) {
+            $this->write_index();
+        }
     }
 
 
@@ -474,6 +471,10 @@
             }
         }
 
+        if ($result) {
+            $this->index_changed = true;
+        }
+
         return $result;
     }
 
@@ -501,6 +502,10 @@
             }
         }
 
+        if ($result) {
+            $this->index_changed = true;
+        }
+
         if ($index) {
             if (($idx = array_search($key, $this->index)) !== false) {
                 unset($this->index[$idx]);

--
Gitblit v1.9.1