Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Performance: Precompile the ImmutableComponentTrait::encode() regex #28

Merged
merged 1 commit into from
Dec 30, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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