Skip to content

Commit

Permalink
Improved hydration of private properties by ~15%
Browse files Browse the repository at this point in the history
This is because by using a `static function` rather than a `function`, the PHP
engine does not need to instantiate and later GC a `$this` variable for said closure
  • Loading branch information
Ocramius committed Mar 4, 2019
1 parent a33f9c1 commit bc03e8d
Showing 1 changed file with 2 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ private function replaceConstructor(ClassMethod $method) : void
// then be called in order in the hydrate() and extract() methods.
foreach ($this->hiddenPropertyMap as $className => $propertyNames) {
// Hydrate closures
$bodyParts[] = '$this->hydrateCallbacks[] = \\Closure::bind(function ($object, $values) {';
$bodyParts[] = '$this->hydrateCallbacks[] = \\Closure::bind(static function ($object, $values) {';
foreach ($propertyNames as $propertyName) {
$bodyParts[] = " if (isset(\$values['" . $propertyName . "']) || " .
'$object->' . $propertyName . " !== null && \\array_key_exists('" . $propertyName . "', \$values)) {";
Expand All @@ -111,7 +111,7 @@ private function replaceConstructor(ClassMethod $method) : void
$bodyParts[] = '}, null, ' . var_export($className, true) . ');' . "\n";

// Extract closures
$bodyParts[] = '$this->extractCallbacks[] = \\Closure::bind(function ($object, &$values) {';
$bodyParts[] = '$this->extractCallbacks[] = \\Closure::bind(static function ($object, &$values) {';
foreach ($propertyNames as $propertyName) {
$bodyParts[] = " \$values['" . $propertyName . "'] = \$object->" . $propertyName . ';';
}
Expand Down

0 comments on commit bc03e8d

Please sign in to comment.