Skip to content

Commit

Permalink
Optimizations for email parsing
Browse files Browse the repository at this point in the history
2x increase in performance per xdebug
  • Loading branch information
slusarz committed Jan 30, 2014
1 parent b475a43 commit 0064a57
Showing 1 changed file with 26 additions and 19 deletions.
45 changes: 26 additions & 19 deletions framework/Mail/lib/Horde/Mail/Rfc822.php
Expand Up @@ -146,7 +146,7 @@ public function parseAddressList($address, array $params = array())
}

if (empty($params['limit'])) {
$params['limit'] = null;
$params['limit'] = -1;
}

$this->_params = array_merge(array(
Expand Down Expand Up @@ -245,8 +245,7 @@ protected function _parseAddressList()
{
$limit = $this->_params['limit'];

while (($this->_curr() !== false) &&
(is_null($limit) || ($limit > 0))) {
while (($this->_curr() !== false) && ($limit-- !== 0)) {
try {
$this->_parseAddress();
} catch (Horde_Mail_Exception $e) {
Expand All @@ -271,7 +270,6 @@ protected function _parseAddressList()
}
break;
}
$limit--;
}
}

Expand Down Expand Up @@ -527,9 +525,9 @@ protected function _rfc822ParsePhrase(&$phrase)
}

$chr = $this->_curr();
if (!$this->_rfc822IsAtext($chr) &&
($chr != '"') &&
($chr != '.')) {
if (($chr != '"') &&
($chr != '.') &&
!$this->_rfc822IsAtext($chr)) {
break;
}

Expand Down Expand Up @@ -558,7 +556,7 @@ protected function _rfc822ParseQuotedString(&$str)

case "\n":
/* Folding whitespace, remove the (CR)LF. */
if ($str[strlen($str) - 1] == "\r") {
if (substr($str, -1) == "\r") {
$str = substr($str, 0, -1);
}
continue;
Expand Down Expand Up @@ -597,8 +595,14 @@ protected function _rfc822ParseDotAtom(&$str, $validate = null)
throw new Horde_Mail_Exception('Error when parsing dot-atom.');
}

$is_validate = $this->_params['validate'];

while (($chr = $this->_curr()) !== false) {
if ($this->_rfc822IsAtext($chr, $validate)) {
/* $this->_rfc822IsAtext($chr, $validate);
* Optimization: Function would be called excessively in this
* loop, so eliminate function call overhead. */
if (($is_validate && !strcspn($chr, self::ATEXT)) ||
(!$is_validate && strcspn($chr, $validate))) {
$str .= $chr;
++$this->_ptr;
} else {
Expand All @@ -607,7 +611,7 @@ protected function _rfc822ParseDotAtom(&$str, $validate = null)
if ($this->_curr() != '.') {
return;
}
$str .= '.';
$str .= $chr;

$this->_rfc822SkipLwsp(true);
}
Expand All @@ -626,10 +630,17 @@ protected function _rfc822ParseDotAtom(&$str, $validate = null)
*/
protected function _rfc822ParseAtomOrDot(&$str)
{
$validate = $this->_params['validate'];

while (($chr = $this->_curr()) !== false) {
if (($chr != '.') && !$this->_rfc822IsAtext($chr, ',<:')) {
if (($chr != '.') &&
/* !$this->_rfc822IsAtext($chr, ',<:');
* Optimization: Function would be called excessively in this
* loop, so eliminate function call overhead. */
!(($validate && !strcspn($chr, self::ATEXT)) ||
(!$validate && strcspn($chr, ',<:')))) {
$this->_rfc822SkipLwsp();
if (!$this->_params['validate']) {
if (!$validate) {
$str = trim($str);
}
return;
Expand Down Expand Up @@ -780,13 +791,9 @@ protected function _rfc822SkipComment()
*/
protected function _rfc822IsAtext($chr, $validate = null)
{
if (is_null($chr)) {
return false;
}

return ($this->_params['validate'] || is_null($validate))
? !strcspn($chr, self::ATEXT)
: strcspn($chr, $validate);
return (!$this->_params['validate'] && !is_null($validate))
? strcspn($chr, $validate)
: !strcspn($chr, self::ATEXT);
}

/* Helper methods. */
Expand Down

0 comments on commit 0064a57

Please sign in to comment.