Skip to content

Commit

Permalink
Merge pull request #28 from rbayliss/regex_precompile_perf
Browse files Browse the repository at this point in the history
Performance: Precompile the ImmutableComponentTrait::encode() regex
  • Loading branch information
nyamsprod committed Dec 30, 2015
2 parents 3d6f573 + 9501113 commit d7d440b
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 23 deletions.
21 changes: 9 additions & 12 deletions src/Components/HierarchicalPath.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,18 +87,15 @@ public function __construct($path = '')
*/
protected function validate($data)
{
$data = array_values(array_filter(explode(static::$separator, $data), function ($segment) {
return !is_null($segment);
}));

$reserved = implode('', array_map(function ($char) {
return preg_quote($char, '/');
}, static::$characters_set));
$regex = '/(?:[^'.$reserved.']+|%(?![A-Fa-f0-9]{2}))/';

return array_map(function ($segment) use ($regex) {
return preg_replace_callback($regex, [$this, 'decodeSegmentPart'], $segment);
}, $data);
$regex = $this->getReservedRegex();

// Run the regex on the entire string rather than exploding.
// The separator should always be a reserved character.
$data = preg_replace_callback($regex, [$this, 'decodeSegmentPart'], $data);

return array_filter(explode(static::$separator, $data), function ($segment) {
return isset($segment);
});
}

/**
Expand Down
6 changes: 1 addition & 5 deletions src/Components/Path.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,8 @@ protected function validate($path)
{
$this->assertValidComponent($path);

$reserved = implode('', array_map(function ($char) {
return preg_quote($char, '/');
}, static::$characters_set));

return preg_replace_callback(
'/(?:[^'.$reserved.']+|%(?![A-Fa-f0-9]{2}))/',
$this->getReservedRegex(),
[$this, 'decodeSegmentPart'],
$path
);
Expand Down
19 changes: 13 additions & 6 deletions src/Types/ImmutableComponentTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ trait ImmutableComponentTrait
'!', '$', '&', "'", '(', ')', '*', '+', ',', ';', '=', ':',
];

protected static $characters_set_compiled;

/**
* Encoded characters to conform to RFC3986 - http://tools.ietf.org/html/rfc3986#section-2
*
Expand Down Expand Up @@ -83,6 +85,15 @@ abstract public function getUriComponent();
*/
abstract public function __toString();

protected static function getReservedRegex()
{
if (!isset(static::$characters_set_compiled)) {
$reserved = preg_quote(implode('', static::$characters_set), '/');
static::$characters_set_compiled = '/(?:[^'.$reserved.']+|%(?![A-Fa-f0-9]{2}))/S';
}
return static::$characters_set_compiled;
}

/**
* Encoding string according to RFC3986
*
Expand All @@ -92,19 +103,15 @@ abstract public function __toString();
*/
protected static function encode($value)
{
$reservedChars = implode('', array_map(function ($value) {
return preg_quote($value, '/');
}, static::$characters_set));

$str = preg_replace_callback(
'/(?:[^'.$reservedChars.']+|%(?![A-Fa-f0-9]{2}))/',
self::getReservedRegex(),
function (array $matches) {
return rawurlencode($matches[0]);
},
$value
);

return preg_replace_callback(',(?<encode>%[0-9A-F]{2}),i', function (array $matches) {
return preg_replace_callback(',(?<encode>%[0-9a-f]{2}),', function (array $matches) {
return strtoupper($matches['encode']);
}, $str);
}
Expand Down

0 comments on commit d7d440b

Please sign in to comment.