8000 [Performance] Use more performance spl_object_id() by samsonasik · Pull Request #4876 · rectorphp/rector-src · GitHub
[go: up one dir, main page]

Skip to content

[Performance] Use more performance spl_object_id() #4876

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

Merged
merged 12 commits into from
Aug 28, 2023
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
8000 12 changes: 6 additions & 6 deletions packages/BetterPhpDocParser/PhpDocInfo/PhpDocInfoFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
final class PhpDocInfoFactory
{
/**
* @var array<string, PhpDocInfo>
* @var array<int, PhpDocInfo>
*/
private array $phpDocInfosByObjectHash = [];
private array $phpDocInfosByObjectId = [];

public function __construct(
private readonly PhpDocNodeMapper $phpDocNodeMapper,
Expand Down Expand Up @@ -57,10 +57,10 @@ public function createFromNodeOrEmpty(Node $node): PhpDocInfo

public function createFromNode(Node $node): ?PhpDocInfo
{
$objectHash = spl_object_hash($node);
$objectId = spl_object_id($node);

if (isset($this->phpDocInfosByObjectHash[$objectHash])) {
return $this->phpDocInfosByObjectHash[$objectHash];
if (isset($this->phpDocInfosByObjectId[$objectId])) {
return $this->phpDocInfosByObjectId[$objectId];
}

/** @see \Rector\BetterPhpDocParser\PhpDocParser\DoctrineAnnotationDecorator::decorate() */
Expand All @@ -84,7 +84,7 @@ public function createFromNode(Node $node): ?PhpDocInfo
}

$phpDocInfo = $this->createFromPhpDocNode($phpDocNode, $tokenIterator, $node);
$this->phpDocInfosByObjectHash[$objectHash] = $phpDocInfo;
$this->phpDocInfosByObjectId[$objectId] = $phpDocInfo;

return $phpDocInfo;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ public function __construct(

public function resolveTagFullyQualifiedName(string $tag, Node $node): string
{
$uniqueHash = $tag . spl_object_hash($node);
if (isset($this->fullyQualifiedNameByHash[$uniqueHash])) {
return $this->fullyQualifiedNameByHash[$uniqueHash];
$uniqueId = $tag . spl_object_id($node);
if (isset($this->fullyQualifiedNameByHash[$uniqueId])) {
return $this->fullyQualifiedNameByHash[$uniqueId];
}

$tag = ltrim($tag, '@');
Expand All @@ -47,7 +47,7 @@ public function resolveTagFullyQualifiedName(string $tag, Node $node): string
$fullyQualifiedClass = $tag;
}

$this->fullyQualifiedNameByHash[$uniqueHash] = $fullyQualifiedClass;
$this->fullyQualifiedNameByHash[$uniqueId] = $fullyQualifiedClass;

return $fullyQualifiedClass;
}
Expand Down
8 changes: 4 additions & 4 deletions packages/PhpDocParser/NodeVisitor/CallableNodeVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ final class CallableNodeVisitor extends NodeVisitorAbstract
*/
private $callable;

private ?string $nodeHashToRemove = null;
private ?int $nodeIdToRemove = null;

/**
* @param callable(Node $node): (int|Node|null) $callable
Expand All @@ -38,7 +38,7 @@ public function enterNode(Node $node): int|Node|null
$newNode = $callable($node);

if ($newNode === NodeTraverser::REMOVE_NODE) {
$this->nodeHashToRemove = spl_object_hash($originalNode);
$this->nodeIdToRemove = spl_object_id($originalNode);
return $originalNode;
}

Expand All @@ -51,8 +51,8 @@ public function enterNode(Node $node): int|Node|null

public function leaveNode(Node $node): int|Node
{
if ($this->nodeHashToRemove === spl_object_hash($node)) {
$this->nodeHashToRemove = null;
if ($this->nodeIdToRemove === spl_object_id($node)) {
$this->nodeIdToRemove = null;
return NodeTraverser::REMOVE_NODE;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class SkipAnonymousClass

private function processClassNode(Class_ $classNode): Class_
{
$variableInfos = $this->propertiesByClass[spl_object_hash($classNode)] ?? [];
$variableInfos = $this->propertiesByClass[spl_object_id($classNode)] ?? [];
foreach ($variableInfos as $propertyInfo) {
$this->classManipula 8000 tor->addConstructorDependency($classNode, $propertyInfo);
}
Expand Down
10 changes: 5 additions & 5 deletions rules/Naming/Naming/ConflictingNameResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
final class ConflictingNameResolver
{
/**
* @var array<string, string[]>
* @var array<int, string[]>
*/
private array $conflictingVariableNamesByClassMethod = [];

Expand Down Expand Up @@ -64,10 +64,10 @@ private function resolveConflictingVariableNamesForNew(
ClassMethod | Function_ | Closure | ArrowFunction $functionLike
): array {
// cache it!
$classMethodHash = spl_object_hash($functionLike);
$classMethodId = spl_object_id($functionLike);

if (isset($this->conflictingVariableNamesByClassMethod[$classMethodHash])) {
return $this->conflictingVariableNamesByClassMethod[$classMethodHash];
if (isset($this->conflictingVariableNamesByClassMethod[$classMethodId])) {
return $this->conflictingVariableNamesByClassMethod[$classMethodId];
}

$paramNames = $this->functionLikeManipulator->resolveParamNames($functionLike);
Expand All @@ -77,7 +77,7 @@ private function resolveConflictingVariableNamesForNew(
$protectedNames = array_merge($paramNames, $newAssignNames, $nonNewAssignNames);

$protectedNames = $this->arrayFilter->filterWithAtLeastTwoOccurences($protectedNames);
$this->conflictingVariableNamesByClassMethod[$classMethodHash] = $protectedNames;
$this->conflictingVariableNamesByClassMethod[$classMethodId] = $protectedNames;

return $protectedNames;
}
Expand Down
10 changes: 5 additions & 5 deletions rules/Naming/Naming/OverridenExistingNamesResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
final class OverridenExistingNamesResolver
{
/**
* @var array<string, array<int, string>>
* @var array<int, array<int, string>>
*/
private array $overridenExistingVariableNamesByClassMethod = [];

Expand Down Expand Up @@ -65,10 +65,10 @@ public function hasNameInFunctionLikeForParam(
*/
private function resolveOveriddenNamesForNew(ClassMethod | Function_ | Closure $functionLike): array
{
$classMethodHash = spl_object_hash($functionLike);
$classMethodId = spl_object_id($functionLike);

if (isset($this->overridenExistingVariableNamesByClassMethod[$classMethodHash])) {
return $this->overridenExistingVariableNamesByClassMethod[$classMethodHash];
if (isset($this->overridenExistingVariableNamesByClassMethod[$classMethodId])) {
return $this->overridenExistingVariableNamesByClassMethod[$classMethodId];
}

$currentlyUsedNames = [];
Expand All @@ -90,7 +90,7 @@ private function resolveOveriddenNamesForNew(ClassMethod | Function_ | Closure $
$currentlyUsedNames = array_values($currentlyUsedNames);
$currentlyUsedNames = $this->arrayFilter->filterWithAtLeastTwoOccurences($currentlyUsedNames);

$this->overridenExistingVariableNamesByClassMethod[$classMethodHash] = $currentlyUsedNames;
$this->overridenExistingVariableNamesByClassMethod[$classMethodId] = $currentlyUsedNames;

return $currentlyUsedNames;
}
Expand Down
12 changes: 6 additions & 6 deletions src/NodeManipulator/ClassMethodAssignManipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
final class ClassMethodAssignManipulator
{
/**
* @var array<string, string[]>
* @var array<int, string[]>
*/
private array $alreadyAddedClassMethodNames = [];

Expand All @@ -37,8 +37,8 @@ public function addParameterAndAssignToMethod(
$classMethod->params[] = $this->nodeFactory->createParamFromNameAndType($name, $type);
$classMethod->stmts[] = new Expression($assign);

$classMethodHash = spl_object_hash($classMethod);
$this->alreadyAddedClassMethodNames[$classMethodHash][] = $name;
$classMethodId = spl_object_id($classMethod);
$this->alreadyAddedClassMethodNames[$classMethodId][] = $name;
}

private function hasMethodParameter(ClassMethod $classMethod, string $name): bool
Expand All @@ -49,11 +49,11 @@ private function hasMethodParameter(ClassMethod $classMethod, string $name): boo
}
}

$classMethodHash = spl_object_hash($classMethod);
if (! isset($this->alreadyAddedClassMethodNames[$classMethodHash])) {
$classMethodId = spl_object_id($classMethod);
if (! isset($this->alreadyAddedClassMethodNames[$classMethodId])) {
return false;
}

return in_array($name, $this->alreadyAddedClassMethodNames[$classMethodHash], true);
return in_array($name, $this->alreadyAddedClassMethodNames[$classMethodId], true);
}
}
18 changes: 9 additions & 9 deletions src/Rector/AbstractRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,15 @@ abstract class AbstractRector extends NodeVisitorAbstract implements RectorInter
private CurrentFileProvider $currentFileProvider;

/**
* @var array<string, Node[]>
* @var array<int, Node[]>
*/
private array $nodesToReturn = [];

private CreatedByRuleDecorator $createdByRuleDecorator;

private RectorOutput $rectorOutput;

private ?string $toBeRemovedNodeHash = null;
private ?int $toBeRemovedNodeId = null;

public function autowire(
NodeNameResolver $nodeNameResolver,
Expand Down Expand Up @@ -183,7 +183,7 @@ final public function enterNode(Node $node): int|Node|null

// @see NodeTraverser::* codes, e.g. removal of node of stopping the traversing
if ($refactoredNode === NodeTraverser::REMOVE_NODE) {
$this->toBeRemovedNodeHash = spl_object_hash($originalNode);
$this->toBeRemovedNodeId = spl_object_id($originalNode);

// notify this rule changing code
$rectorWithLineChange = new RectorWithLineChange(static::class, $originalNode->getLine());
Expand Down Expand Up @@ -234,14 +234,14 @@ public function leaveNode(Node $node): array|int|Node|null
return null;
}

$objectHash = spl_object_hash($node);
if ($this->toBeRemovedNodeHash === $objectHash) {
$this->toBeRemovedNodeHash = null;
$objectId = spl_object_id($node);
if ($this->toBeRemovedNodeId === $objectId) {
$this->toBeRemovedNodeId = null;

return NodeTraverser::REMOVE_NODE;
}

return $this->nodesToReturn[$objectHash] ?? $node;
return $this->nodesToReturn[$objectId] ?? $node;
}

protected function isName(Node $node, string $name): bool
Expand Down Expand Up @@ -349,10 +349,10 @@ private function postRefactorProcess(
$this->refreshScopeNodes($refactoredNode, $filePath, $currentScope);

// search "infinite recursion" in https://github.com/nikic/PHP-Parser/blob/master/doc/component/Walking_the_AST.markdown
$originalNodeHash = spl_object_hash($originalNode);
$originalNodeId = spl_object_id($originalNode);

// will be replaced in leaveNode() the original node must be passed
$this->nodesToReturn[$originalNodeHash] = $refactoredNode;
$this->nodesToReturn[$originalNodeId] = $refactoredNode;

return $originalNode;
}
Expand Down
0