Skip to content

Commit a167aa2

Browse files
committed
Optimize attribue checks in the lexer
1 parent 993f299 commit a167aa2

File tree

1 file changed

+25
-14
lines changed

1 file changed

+25
-14
lines changed

lib/PhpParser/Lexer.php

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,13 @@ class Lexer
1616
protected $tokenMap;
1717
protected $dropTokens;
1818

19-
protected $usedAttributes;
19+
private $attributeStartLineUsed;
20+
private $attributeEndLineUsed;
21+
private $attributeStartTokenPosUsed;
22+
private $attributeEndTokenPosUsed;
23+
private $attributeStartFilePosUsed;
24+
private $attributeEndFilePosUsed;
25+
private $attributeCommentsUsed;
2026

2127
/**
2228
* Creates a Lexer.
@@ -37,12 +43,17 @@ public function __construct(array $options = []) {
3743
[\T_WHITESPACE, \T_OPEN_TAG, \T_COMMENT, \T_DOC_COMMENT], 1
3844
);
3945

40-
// the usedAttributes member is a map of the used attribute names to a dummy
41-
// value (here "true")
42-
$options += [
43-
'usedAttributes' => ['comments', 'startLine', 'endLine'],
44-
];
45-
$this->usedAttributes = array_fill_keys($options['usedAttributes'], true);
46+
$defaultAttributes = ['comments', 'startLine', 'endLine'];
47+
$usedAttributes = array_fill_keys($options['usedAttributes'] ?? $defaultAttributes, true);
48+
49+
// Create individual boolean properties to make these checks faster.
50+
$this->attributeStartLineUsed = isset($usedAttributes['startLine']);
51+
$this->attributeEndLineUsed = isset($usedAttributes['endLine']);
52+
$this->attributeStartTokenPosUsed = isset($usedAttributes['startTokenPos']);
53+
$this->attributeEndTokenPosUsed = isset($usedAttributes['endTokenPos']);
54+
$this->attributeStartFilePosUsed = isset($usedAttributes['startFilePos']);
55+
$this->attributeEndFilePosUsed = isset($usedAttributes['endFilePos']);
56+
$this->attributeCommentsUsed = isset($usedAttributes['comments']);
4657
}
4758

4859
/**
@@ -230,13 +241,13 @@ public function getNextToken(&$value = null, &$startAttributes = null, &$endAttr
230241
$token = "\0";
231242
}
232243

233-
if (isset($this->usedAttributes['startLine'])) {
244+
if ($this->attributeStartLineUsed) {
234245
$startAttributes['startLine'] = $this->line;
235246
}
236-
if (isset($this->usedAttributes['startTokenPos'])) {
247+
if ($this->attributeStartTokenPosUsed) {
237248
$startAttributes['startTokenPos'] = $this->pos;
238249
}
239-
if (isset($this->usedAttributes['startFilePos'])) {
250+
if ($this->attributeStartFilePosUsed) {
240251
$startAttributes['startFilePos'] = $this->filePos;
241252
}
242253

@@ -263,7 +274,7 @@ public function getNextToken(&$value = null, &$startAttributes = null, &$endAttr
263274
$this->filePos += \strlen($value);
264275
} else {
265276
if (\T_COMMENT === $token[0] || \T_DOC_COMMENT === $token[0]) {
266-
if (isset($this->usedAttributes['comments'])) {
277+
if ($this->attributeCommentsUsed) {
267278
$comment = \T_DOC_COMMENT === $token[0]
268279
? new Comment\Doc($token[1], $this->line, $this->filePos, $this->pos)
269280
: new Comment($token[1], $this->line, $this->filePos, $this->pos);
@@ -276,13 +287,13 @@ public function getNextToken(&$value = null, &$startAttributes = null, &$endAttr
276287
continue;
277288
}
278289

279-
if (isset($this->usedAttributes['endLine'])) {
290+
if ($this->attributeEndLineUsed) {
280291
$endAttributes['endLine'] = $this->line;
281292
}
282-
if (isset($this->usedAttributes['endTokenPos'])) {
293+
if ($this->attributeEndTokenPosUsed) {
283294
$endAttributes['endTokenPos'] = $this->pos;
284295
}
285-
if (isset($this->usedAttributes['endFilePos'])) {
296+
if ($this->attributeEndFilePosUsed) {
286297
$endAttributes['endFilePos'] = $this->filePos - 1;
287298
}
288299

0 commit comments

Comments
 (0)