Bostjan Skufca
2016-04-09 55d90b2f621a9c144b54b07b4e1dafd0fdad0e85
mailbox/listing: Make server response for large mailbox listing faster when using threaded view

Symptom
=======
When using roundcube with mailboxes with over 60k messages, list
view was way faster than viewing in threaded view.

Mailbox index view timing: ~360 ms
Mailbox threaded view timing: ~800 ms

Resolution
==========
Use native PHP array manipulation functions instead of rolling custom
string data reversal implementation using strpos() and substr() in a
'while' loop.

This optimization is already present in index view handler, but was missing
from threaded view.

Results after optimization
==========================
Both average out around ~360 ms response time.
1 files modified
18 ■■■■ changed files
program/lib/Roundcube/rcube_result_thread.php 18 ●●●● patch | view | raw | blame | history
program/lib/Roundcube/rcube_result_thread.php
@@ -252,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);
    }