From 55d90b2f621a9c144b54b07b4e1dafd0fdad0e85 Mon Sep 17 00:00:00 2001
From: Bostjan Skufca <bostjan@a2o.si>
Date: Sun, 10 Apr 2016 04:50:53 -0400
Subject: [PATCH] mailbox/listing: Make server response for large mailbox listing faster when using threaded view

---
 program/lib/Roundcube/rcube_result_thread.php |   57 +++++++++++++++++++++++++++------------------------------
 1 files changed, 27 insertions(+), 30 deletions(-)

diff --git a/program/lib/Roundcube/rcube_result_thread.php b/program/lib/Roundcube/rcube_result_thread.php
index 7657550..7123f47 100644
--- a/program/lib/Roundcube/rcube_result_thread.php
+++ b/program/lib/Roundcube/rcube_result_thread.php
@@ -26,6 +26,8 @@
  */
 class rcube_result_thread
 {
+    public $incomplete = false;
+
     protected $raw_data;
     protected $mailbox;
     protected $meta = array();
@@ -250,22 +252,10 @@
             return;
         }
 
+        $raw_data_reverse = implode(self::SEPARATOR_ELEMENT, array_reverse(explode(self::SEPARATOR_ELEMENT, $this->raw_data)));
+        $this->raw_data = $raw_data_reverse;
+
         $this->meta['pos'] = array();
-        $datalen = strlen($this->raw_data);
-        $result  = '';
-        $start   = 0;
-
-        while (($pos = @strpos($this->raw_data, self::SEPARATOR_ELEMENT, $start))
-            || ($start < $datalen && ($pos = $datalen))
-        ) {
-            $len   = $pos - $start;
-            $elem  = substr($this->raw_data, $start, $len);
-            $start = $pos + 1;
-
-            $result = $elem . self::SEPARATOR_ELEMENT . $result;
-        }
-
-        $this->raw_data = rtrim($result, self::SEPARATOR_ELEMENT);
     }
 
 
@@ -453,7 +443,7 @@
 
         // when sorting search result it's good to make the index smaller
         if ($index->count() != $this->count_messages()) {
-            $index->intersect($this->get());
+            $index->filter($this->get());
         }
 
         $result  = array_fill_keys($index->get(), null);
@@ -606,33 +596,39 @@
         // arrays handling is much more expensive
         // For the following structure: THREAD (2)(3 6 (4 23)(44 7 96))
         // -- 2
-        //
         // -- 3
         //     \-- 6
         //         |-- 4
         //         |    \-- 23
         //         |
         //         \-- 44
-        //              \-- 7
-        //                   \-- 96
+        //               \-- 7
+        //                    \-- 96
         //
         // The output will be: 2,3^1:6^2:4^3:23^2:44^3:7^4:96
 
         if ($str[$begin] != '(') {
-            $stop = $begin + strspn($str, '1234567890', $begin, $end - $begin);
-            $msg  = substr($str, $begin, $stop - $begin);
-            if (!$msg) {
+            // find next bracket
+            $stop      = $begin + strcspn($str, '()', $begin, $end - $begin);
+            $messages  = explode(' ', trim(substr($str, $begin, $stop - $begin)));
+
+            if (empty($messages)) {
                 return $node;
             }
 
-            $this->meta['messages']++;
-
-            $node .= ($depth ? self::SEPARATOR_ITEM.$depth.self::SEPARATOR_LEVEL : '').$msg;
-
-            if ($stop + 1 < $end) {
-                $node .= $this->parse_thread($str, $stop + 1, $end, $depth + 1);
+            foreach ($messages as $msg) {
+                if ($msg) {
+                    $node .= ($depth ? self::SEPARATOR_ITEM.$depth.self::SEPARATOR_LEVEL : '').$msg;
+                    $this->meta['messages']++;
+                    $depth++;
+                }
             }
-        } else {
+
+            if ($stop < $end) {
+                $node .= $this->parse_thread($str, $stop, $end, $depth);
+            }
+        }
+        else {
             $off = $begin;
             while ($off < $end) {
                 $start = $off;
@@ -649,7 +645,8 @@
                     if ($p1 !== false && $p1 < $p) {
                         $off = $p1 + 1;
                         $n++;
-                    } else {
+                    }
+                    else {
                         $off = $p + 1;
                         $n--;
                     }

--
Gitblit v1.9.1