Skip to content

Commit

Permalink
[mms] Optimizations to IMAP tokenizer.
Browse files Browse the repository at this point in the history
Not much more than removing the overhead of function calls that are
(potentially) called 1,000s of times.

Specifically, in the testLargeEnvelopeData test, these changes save on
something like 700,000+ individual method calls, a ~30% improvement
(albeit in an extreme use-case).
  • Loading branch information
slusarz committed Feb 11, 2014
1 parent 0b267d8 commit 297c1ab
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 57 deletions.
108 changes: 53 additions & 55 deletions framework/Imap_Client/lib/Horde/Imap/Client/Tokenize.php
Expand Up @@ -197,45 +197,11 @@ public function key()
*/
public function next()
{
if ((($this->_current = $this->_parseStream()) === false) &&
$this->eos) {
$this->_key = $this->_level = false;
} else {
++$this->_key;
}

return $this->_current;
}

/**
*/
public function rewind()
{
$this->_stream->rewind();
$this->_current = false;
$this->_key = -1;
$this->_level = 0;
}

/**
*/
public function valid()
{
return ($this->_level !== false);
}

/**
* Returns the next token and increments the internal stream pointer.
*
* @see next()
*/
protected function _parseStream()
{
$in_quote = false;
$check_len = true;
$in_quote = $text = false;
/* Directly access stream here to drastically reduce the number of
* getChar() calls we would have to make. */
$stream = $this->_stream->stream;
$text = '';

while (($c = fgetc($stream)) !== false) {
switch ($c) {
Expand All @@ -247,10 +213,10 @@ protected function _parseStream()

case '"':
if ($in_quote) {
return $text;
} else {
$in_quote = true;
$check_len = false;
break 2;
}
$in_quote = true;
break;

default:
Expand All @@ -262,29 +228,34 @@ protected function _parseStream()
switch ($c) {
case '(':
++$this->_level;
return true;
$check_len = false;
$text = true;
break 3;

case ')':
if (strlen($text)) {
if ($text === false) {
--$this->_level;
$check_len = $text = false;
} else {
$this->_stream->seek(-1);
break 3;
}
--$this->_level;
return false;
break 3;

case '~':
// Ignore binary string identifier. PHP strings are
// binary-safe.
break;

case '{':
return $this->_stream->substring(
$text = $this->_stream->substring(
0,
intval($this->_stream->getToChar('}'))
);
$check_len = false;
break 3;

case ' ':
if (strlen($text)) {
if ($text !== false) {
break 3;
}
break;
Expand All @@ -297,19 +268,46 @@ protected function _parseStream()
}
}

switch (strlen($text)) {
case 0:
return false;
if ($check_len) {
switch (strlen($text)) {
case 0:
$text = false;
break;

case 3:
if (strcasecmp($text, 'NIL') === 0) {
return null;
case 3:
if (($text === 'NIL') || (strcasecmp($text, 'NIL') === 0)) {
$text = null;
}
break;
}
// Fall-through
}

default:
return $text;
if (($text === false) && feof($stream)) {
$this->_key = $this->_level = false;
} else {
++$this->_key;
}

$this->_current = $text;

return $text;
}

/**
*/
public function rewind()
{
$this->_stream->rewind();
$this->_current = false;
$this->_key = -1;
$this->_level = 0;
}

/**
*/
public function valid()
{
return ($this->_level !== false);
}

}
4 changes: 2 additions & 2 deletions framework/Imap_Client/package.xml
Expand Up @@ -21,7 +21,7 @@
</stability>
<license uri="http://www.horde.org/licenses/lgpl21">LGPL-2.1</license>
<notes>
*
* [mms] Optimizations to IMAP tokenizer.
</notes>
<contents>
<dir baseinstalldir="/" name="/">
Expand Down Expand Up @@ -2165,7 +2165,7 @@
<date>2014-02-11</date>
<license uri="http://www.horde.org/licenses/lgpl21">LGPL-2.1</license>
<notes>
*
* [mms] Optimizations to IMAP tokenizer.
</notes>
</release>
</changelog>
Expand Down

0 comments on commit 297c1ab

Please sign in to comment.