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

Micro-optimizations #162

Open
wants to merge 3 commits into
base: 1.9.x
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions src/Lexer/Lexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,8 @@ private function generateRegexp(): string

self::TOKEN_FLOAT => '(?:-?[0-9]++\\.[0-9]*+(?:e-?[0-9]++)?)|(?:-?[0-9]*+\\.[0-9]++(?:e-?[0-9]++)?)|(?:-?[0-9]++e-?[0-9]++)',
self::TOKEN_INTEGER => '-?(?:(?:0b[0-1]++)|(?:0o[0-7]++)|(?:0x[0-9a-f]++)|(?:[0-9]++))',
self::TOKEN_SINGLE_QUOTED_STRING => '\'(?:\\\\[^\\r\\n]|[^\'\\r\\n\\\\])*+\'',
self::TOKEN_DOUBLE_QUOTED_STRING => '"(?:\\\\[^\\r\\n]|[^"\\r\\n\\\\])*+"',
self::TOKEN_SINGLE_QUOTED_STRING => '\'(?:\\\\[^\\r\\n]|[^\'\\r\\n\\\\]++)*+\'',
self::TOKEN_DOUBLE_QUOTED_STRING => '"(?:\\\\[^\\r\\n]|[^"\\r\\n\\\\]++)*+"',

self::TOKEN_WILDCARD => '\\*',

Expand Down
14 changes: 13 additions & 1 deletion src/Parser/TokenIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace PHPStan\PhpDocParser\Parser;

use PHPStan\PhpDocParser\Lexer\Lexer;
use function array_flip;
use function array_pop;
use function assert;
use function count;
Expand Down Expand Up @@ -160,8 +161,10 @@ public function getSkippedHorizontalWhiteSpaceIfAny(): string
/** @phpstan-impure */
public function joinUntil(int ...$tokenType): string
{
$tokenTypeMap = array_flip($tokenType);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this faster then array_fill_keys($tokenType, true)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's indistinguishable so changing to array_fill_keys() is fine by me :)


$s = '';
while (!in_array($this->tokens[$this->index][Lexer::TYPE_OFFSET], $tokenType, true)) {
while (!isset($tokenTypeMap[$this->tokens[$this->index][Lexer::TYPE_OFFSET]])) {
$s .= $this->tokens[$this->index++][Lexer::VALUE_OFFSET];
}
return $s;
Expand Down Expand Up @@ -207,6 +210,15 @@ public function rollback(): void
}


/** @internal */
public function copy(): self
{
$copy = new self($this->tokens, $this->index);
$copy->savePoints = $this->savePoints;
return $copy;
}


/**
* @throws ParserException
*/
Expand Down
21 changes: 12 additions & 9 deletions src/Parser/TypeParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,26 +139,29 @@ private function parseAtomic(TokenIterator $tokens): Ast\Type\TypeNode
$tokens->dropSavePoint(); // because of ConstFetchNode
}

$exception = new ParserException(
$tokens->currentTokenValue(),
$tokens->currentTokenType(),
$tokens->currentTokenOffset(),
Lexer::TOKEN_IDENTIFIER
);
$tokensCopy = $tokens->copy();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you not just clone $tokens?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but I know Ondřej dislikes clone

$exception = static function () use ($tokensCopy): ParserException {
return new ParserException(
$tokensCopy->currentTokenValue(),
$tokensCopy->currentTokenType(),
$tokensCopy->currentTokenOffset(),
Lexer::TOKEN_IDENTIFIER
);
};

if ($this->constExprParser === null) {
throw $exception;
throw $exception();
}

try {
$constExpr = $this->constExprParser->parse($tokens, true);
if ($constExpr instanceof Ast\ConstExpr\ConstExprArrayNode) {
throw $exception;
throw $exception();
}

return new Ast\Type\ConstTypeNode($constExpr);
} catch (LogicException $e) {
throw $exception;
throw $exception();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like constExprParser no longer throws LogicException since d8e9fd9.

}
}

Expand Down