Skip to content

Commit

Permalink
Optimize memoization
Browse files Browse the repository at this point in the history
  • Loading branch information
joshdifabio committed Dec 13, 2018
1 parent 553a27c commit 9a2d01f
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 8 deletions.
5 changes: 1 addition & 4 deletions demo/auto/AutoValue_Address.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,7 @@ public function foo()

public function linesString(): string
{
if (!isset($this->__memoized['linesString'])) {
$this->__memoized['linesString'] = parent::linesString();
}
return $this->__memoized['linesString'];
return $this->__memoized['linesString'] ?? ($this->__memoized['linesString'] = parent::linesString());
}

/**
Expand Down
5 changes: 1 addition & 4 deletions src/Memoize/MemoizeMethodProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,7 @@ function (MethodDefinitionCollection $methodDefinitions, ReflectionMethod $metho
THEPHP;
} else {
$methodBody = <<<THEPHP
if (!isset(\$this->__memoized['$methodName'])) {
\$this->__memoized['$methodName'] = parent::$methodName();
}
return \$this->__memoized['$methodName'];
return \$this->__memoized['$methodName'] ?? (\$this->__memoized['$methodName'] = parent::$methodName());
THEPHP;
}
return $methodDefinitions->withAdditionalMethodDefinition(MethodDefinition::of($method, $methodBody));
Expand Down

0 comments on commit 9a2d01f

Please sign in to comment.