Skip to content

Commit

Permalink
Memoizing DocBlock#requirements() so that repeated calls do not lea…
Browse files Browse the repository at this point in the history
…d to overhead

Parsing requirements on docblocks seems to be repeated multiple times,
so we need to prevent this from being performed more than necessary.
The regular expressions around this are quite complex, and therefore there
is a performance impact (~2% total runtime on an IO-free test suite).
  • Loading branch information
Ocramius authored and sebastianbergmann committed Sep 7, 2019
1 parent 2abea0e commit fe308f3
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions src/Annotation/DocBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
* @internal This class is part of PHPUnit internals, an not intended
* for downstream usage
*
* @psalm-immutable
* @psalm-mutation-free
*/
final class DocBlock
{
Expand Down Expand Up @@ -52,6 +52,20 @@ final class DocBlock
/** @var array<string, array<int, string>> pre-parsed annotations indexed by name and occurrence index */
private $symbolAnnotations;

/**
* @var array<string, mixed>|null
*
* @psalm-var null|(array{
* __OFFSET: array<string, int>&array{__FILE: string},
* setting?: array<string, string>,
* extension_versions?: array<string, array{version: string, operator: string}>
* }&array<
* string,
* string|array{version: string, operator: string}|array{constraint: string}|array<int|string, string>
* >)
*/
private $parsedRequirements;

/** @var int */
private $startLine;

Expand Down Expand Up @@ -137,9 +151,15 @@ private function __construct(
* string,
* string|array{version: string, operator: string}|array{constraint: string}|array<int|string, string>
* >
*
* @throws Warning if the requirements version constraint is not well-formed
*/
public function requirements(): array
{
if ($this->parsedRequirements !== null) {
return $this->parsedRequirements;
}

$offset = $this->startLine;
$requires = [];
$recordedSettings = [];
Expand Down Expand Up @@ -212,7 +232,7 @@ public function requirements(): array
$offset++;
}

return \array_merge(
return $this->parsedRequirements = \array_merge(
$requires,
['__OFFSET' => $recordedOffsets],
\array_filter([
Expand Down

0 comments on commit fe308f3

Please sign in to comment.