bug #16294 [PropertyAccess] Major performance improvement (dunglas)
This PR was squashed before being merged into the 2.3 branch (closes #16294).
Discussion
----------
[PropertyAccess] Major performance improvement
| Q | A
| ------------- | ---
| Bug fix? | yes
| New feature? | no
| BC breaks? | no
| Deprecations? | no
| Tests pass? | yes
| Fixed tickets | #16179
| License | MIT
| Doc PR | n/a
This PR improves performance of the PropertyAccess component of ~70%.
The two main changes are:
* caching the `PropertyPath` initialization
* caching the guessed access strategy
This is especially important for the `ObjectNormalizer` (Symfony Serializer) and the JSON-LD normalizer ([API Platform](https://api-platform.com)) because they use the `PropertyAccessor` class in large loops (ex: normalization of a list of entities).
Here is the Blackfire comparison: https://blackfire.io/profiles/compare/c42fd275-2b0c-4ce5-8bf3-84762054d31e/graph
The code of the benchmark I've used (with Symfony 2.3 as dependency):
```php
<?php
require 'vendor/autoload.php';
class Foo
{
private $baz;
public $bar;
public function getBaz()
{
return $this->baz;
}
public function setBaz($baz)
{
$this->baz = $baz;
}
}
use Symfony\Component\PropertyAccess\PropertyAccess;
$accessor = PropertyAccess::createPropertyAccessor();
$start = microtime(true);
for ($i = 0; $i < 10000; ++$i) {
$foo = new Foo();
$accessor->setValue($foo, 'bar', 'Lorem');
$accessor->setValue($foo, 'baz', 'Ipsum');
$accessor->getValue($foo, 'bar');
$accessor->getValue($foo, 'baz');
}
echo 'Time: '.(microtime(true) - $start).PHP_EOL;
```
This PR also adds an optional support for Doctrine cache to keep access information across requests and improve the overall application performance (even outside of loops).
Commits
-------
284dc75
[PropertyAccess] Major performance improvement
This commit is contained in:
commit
3b2d0100ac
@ -23,8 +23,21 @@ class PropertyAccessor implements PropertyAccessorInterface
|
|||||||
{
|
{
|
||||||
const VALUE = 0;
|
const VALUE = 0;
|
||||||
const IS_REF = 1;
|
const IS_REF = 1;
|
||||||
|
const ACCESS_HAS_PROPERTY = 0;
|
||||||
|
const ACCESS_TYPE = 1;
|
||||||
|
const ACCESS_NAME = 2;
|
||||||
|
const ACCESS_REF = 3;
|
||||||
|
const ACCESS_ADDER = 4;
|
||||||
|
const ACCESS_REMOVER = 5;
|
||||||
|
const ACCESS_TYPE_METHOD = 0;
|
||||||
|
const ACCESS_TYPE_PROPERTY = 1;
|
||||||
|
const ACCESS_TYPE_MAGIC = 2;
|
||||||
|
const ACCESS_TYPE_ADDER_AND_REMOVER = 3;
|
||||||
|
const ACCESS_TYPE_NOT_FOUND = 4;
|
||||||
|
|
||||||
private $magicCall;
|
private $magicCall;
|
||||||
|
private $readPropertyCache = array();
|
||||||
|
private $writePropertyCache = array();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Should not be used by application code. Use
|
* Should not be used by application code. Use
|
||||||
@ -202,48 +215,31 @@ class PropertyAccessor implements PropertyAccessorInterface
|
|||||||
throw new NoSuchPropertyException(sprintf('Cannot read property "%s" from an array. Maybe you should write the property path as "[%s]" instead?', $property, $property));
|
throw new NoSuchPropertyException(sprintf('Cannot read property "%s" from an array. Maybe you should write the property path as "[%s]" instead?', $property, $property));
|
||||||
}
|
}
|
||||||
|
|
||||||
$camelProp = $this->camelize($property);
|
$access = $this->getReadAccessInfo($object, $property);
|
||||||
$reflClass = new \ReflectionClass($object);
|
|
||||||
$getter = 'get'.$camelProp;
|
|
||||||
$isser = 'is'.$camelProp;
|
|
||||||
$hasser = 'has'.$camelProp;
|
|
||||||
$classHasProperty = $reflClass->hasProperty($property);
|
|
||||||
|
|
||||||
if ($reflClass->hasMethod($getter) && $reflClass->getMethod($getter)->isPublic()) {
|
if (self::ACCESS_TYPE_METHOD === $access[self::ACCESS_TYPE]) {
|
||||||
$result[self::VALUE] = $object->$getter();
|
$result[self::VALUE] = $object->{$access[self::ACCESS_NAME]}();
|
||||||
} elseif ($reflClass->hasMethod($isser) && $reflClass->getMethod($isser)->isPublic()) {
|
} elseif (self::ACCESS_TYPE_PROPERTY === $access[self::ACCESS_TYPE]) {
|
||||||
$result[self::VALUE] = $object->$isser();
|
if ($access[self::ACCESS_REF]) {
|
||||||
} elseif ($reflClass->hasMethod($hasser) && $reflClass->getMethod($hasser)->isPublic()) {
|
$result[self::VALUE] = &$object->{$access[self::ACCESS_NAME]};
|
||||||
$result[self::VALUE] = $object->$hasser();
|
|
||||||
} elseif ($reflClass->hasMethod('__get') && $reflClass->getMethod('__get')->isPublic()) {
|
|
||||||
$result[self::VALUE] = $object->$property;
|
|
||||||
} elseif ($classHasProperty && $reflClass->getProperty($property)->isPublic()) {
|
|
||||||
$result[self::VALUE] = &$object->$property;
|
|
||||||
$result[self::IS_REF] = true;
|
$result[self::IS_REF] = true;
|
||||||
} elseif (!$classHasProperty && property_exists($object, $property)) {
|
} else {
|
||||||
|
$result[self::VALUE] = $object->{$access[self::ACCESS_NAME]};
|
||||||
|
}
|
||||||
|
} elseif (!$access[self::ACCESS_HAS_PROPERTY] && property_exists($object, $property)) {
|
||||||
// Needed to support \stdClass instances. We need to explicitly
|
// Needed to support \stdClass instances. We need to explicitly
|
||||||
// exclude $classHasProperty, otherwise if in the previous clause
|
// exclude $classHasProperty, otherwise if in the previous clause
|
||||||
// a *protected* property was found on the class, property_exists()
|
// a *protected* property was found on the class, property_exists()
|
||||||
// returns true, consequently the following line will result in a
|
// returns true, consequently the following line will result in a
|
||||||
// fatal error.
|
// fatal error.
|
||||||
|
|
||||||
$result[self::VALUE] = &$object->$property;
|
$result[self::VALUE] = &$object->$property;
|
||||||
$result[self::IS_REF] = true;
|
$result[self::IS_REF] = true;
|
||||||
} elseif ($this->magicCall && $reflClass->hasMethod('__call') && $reflClass->getMethod('__call')->isPublic()) {
|
} elseif (self::ACCESS_TYPE_MAGIC === $access[self::ACCESS_TYPE]) {
|
||||||
// we call the getter and hope the __call do the job
|
// we call the getter and hope the __call do the job
|
||||||
$result[self::VALUE] = $object->$getter();
|
$result[self::VALUE] = $object->{$access[self::ACCESS_NAME]}();
|
||||||
} else {
|
} else {
|
||||||
$methods = array($getter, $isser, $hasser, '__get');
|
throw new NoSuchPropertyException($access[self::ACCESS_NAME]);
|
||||||
if ($this->magicCall) {
|
|
||||||
$methods[] = '__call';
|
|
||||||
}
|
|
||||||
|
|
||||||
throw new NoSuchPropertyException(sprintf(
|
|
||||||
'Neither the property "%s" nor one of the methods "%s()" '.
|
|
||||||
'exist and have public access in class "%s".',
|
|
||||||
$property,
|
|
||||||
implode('()", "', $methods),
|
|
||||||
$reflClass->name
|
|
||||||
));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Objects are always passed around by reference
|
// Objects are always passed around by reference
|
||||||
@ -254,6 +250,77 @@ class PropertyAccessor implements PropertyAccessorInterface
|
|||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Guesses how to read the property value.
|
||||||
|
*
|
||||||
|
* @param string $object
|
||||||
|
* @param string $property
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
private function getReadAccessInfo($object, $property)
|
||||||
|
{
|
||||||
|
$key = get_class($object).'::'.$property;
|
||||||
|
|
||||||
|
if (isset($this->readPropertyCache[$key])) {
|
||||||
|
$access = $this->readPropertyCache[$key];
|
||||||
|
} else {
|
||||||
|
$access = array();
|
||||||
|
|
||||||
|
$reflClass = new \ReflectionClass($object);
|
||||||
|
$access[self::ACCESS_HAS_PROPERTY] = $reflClass->hasProperty($property);
|
||||||
|
$camelProp = $this->camelize($property);
|
||||||
|
$getter = 'get'.$camelProp;
|
||||||
|
$isser = 'is'.$camelProp;
|
||||||
|
$hasser = 'has'.$camelProp;
|
||||||
|
$classHasProperty = $reflClass->hasProperty($property);
|
||||||
|
|
||||||
|
if ($reflClass->hasMethod($getter) && $reflClass->getMethod($getter)->isPublic()) {
|
||||||
|
$access[self::ACCESS_TYPE] = self::ACCESS_TYPE_METHOD;
|
||||||
|
$access[self::ACCESS_NAME] = $getter;
|
||||||
|
} elseif ($reflClass->hasMethod($isser) && $reflClass->getMethod($isser)->isPublic()) {
|
||||||
|
$access[self::ACCESS_TYPE] = self::ACCESS_TYPE_METHOD;
|
||||||
|
$access[self::ACCESS_NAME] = $isser;
|
||||||
|
} elseif ($reflClass->hasMethod($hasser) && $reflClass->getMethod($hasser)->isPublic()) {
|
||||||
|
$access[self::ACCESS_TYPE] = self::ACCESS_TYPE_METHOD;
|
||||||
|
$access[self::ACCESS_NAME] = $hasser;
|
||||||
|
} elseif ($reflClass->hasMethod('__get') && $reflClass->getMethod('__get')->isPublic()) {
|
||||||
|
$access[self::ACCESS_TYPE] = self::ACCESS_TYPE_PROPERTY;
|
||||||
|
$access[self::ACCESS_NAME] = $property;
|
||||||
|
$access[self::ACCESS_REF] = false;
|
||||||
|
} elseif ($classHasProperty && $reflClass->getProperty($property)->isPublic()) {
|
||||||
|
$access[self::ACCESS_TYPE] = self::ACCESS_TYPE_PROPERTY;
|
||||||
|
$access[self::ACCESS_NAME] = $property;
|
||||||
|
$access[self::ACCESS_REF] = true;
|
||||||
|
|
||||||
|
$result[self::VALUE] = &$object->$property;
|
||||||
|
$result[self::IS_REF] = true;
|
||||||
|
} elseif ($this->magicCall && $reflClass->hasMethod('__call') && $reflClass->getMethod('__call')->isPublic()) {
|
||||||
|
// we call the getter and hope the __call do the job
|
||||||
|
$access[self::ACCESS_TYPE] = self::ACCESS_TYPE_MAGIC;
|
||||||
|
$access[self::ACCESS_NAME] = $getter;
|
||||||
|
} else {
|
||||||
|
$methods = array($getter, $isser, $hasser, '__get');
|
||||||
|
if ($this->magicCall) {
|
||||||
|
$methods[] = '__call';
|
||||||
|
}
|
||||||
|
|
||||||
|
$access[self::ACCESS_TYPE] = self::ACCESS_TYPE_NOT_FOUND;
|
||||||
|
$access[self::ACCESS_NAME] = sprintf(
|
||||||
|
'Neither the property "%s" nor one of the methods "%s()" '.
|
||||||
|
'exist and have public access in class "%s".',
|
||||||
|
$property,
|
||||||
|
implode('()", "', $methods),
|
||||||
|
$reflClass->name
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->readPropertyCache[$key] = $access;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $access;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the value of the property at the given index in the path.
|
* Sets the value of the property at the given index in the path.
|
||||||
*
|
*
|
||||||
@ -285,22 +352,17 @@ class PropertyAccessor implements PropertyAccessorInterface
|
|||||||
*/
|
*/
|
||||||
private function writeProperty(&$object, $property, $singular, $value)
|
private function writeProperty(&$object, $property, $singular, $value)
|
||||||
{
|
{
|
||||||
$guessedAdders = '';
|
|
||||||
|
|
||||||
if (!is_object($object)) {
|
if (!is_object($object)) {
|
||||||
throw new NoSuchPropertyException(sprintf('Cannot write property "%s" to an array. Maybe you should write the property path as "[%s]" instead?', $property, $property));
|
throw new NoSuchPropertyException(sprintf('Cannot write property "%s" to an array. Maybe you should write the property path as "[%s]" instead?', $property, $property));
|
||||||
}
|
}
|
||||||
|
|
||||||
$reflClass = new \ReflectionClass($object);
|
$access = $this->getWriteAccessInfo($object, $property, $singular, $value);
|
||||||
$plural = $this->camelize($property);
|
|
||||||
|
|
||||||
// Any of the two methods is required, but not yet known
|
if (self::ACCESS_TYPE_METHOD === $access[self::ACCESS_TYPE]) {
|
||||||
$singulars = null !== $singular ? array($singular) : (array) StringUtil::singularify($plural);
|
$object->{$access[self::ACCESS_NAME]}($value);
|
||||||
|
} elseif (self::ACCESS_TYPE_PROPERTY === $access[self::ACCESS_TYPE]) {
|
||||||
if (is_array($value) || $value instanceof \Traversable) {
|
$object->{$access[self::ACCESS_NAME]} = $value;
|
||||||
$methods = $this->findAdderAndRemover($reflClass, $singulars);
|
} elseif (self::ACCESS_TYPE_ADDER_AND_REMOVER === $access[self::ACCESS_TYPE]) {
|
||||||
|
|
||||||
if (null !== $methods) {
|
|
||||||
// At this point the add and remove methods have been found
|
// At this point the add and remove methods have been found
|
||||||
// Use iterator_to_array() instead of clone in order to prevent side effects
|
// Use iterator_to_array() instead of clone in order to prevent side effects
|
||||||
// see https://github.com/symfony/symfony/issues/4670
|
// see https://github.com/symfony/symfony/issues/4670
|
||||||
@ -329,54 +391,106 @@ class PropertyAccessor implements PropertyAccessorInterface
|
|||||||
}
|
}
|
||||||
|
|
||||||
foreach ($itemToRemove as $item) {
|
foreach ($itemToRemove as $item) {
|
||||||
call_user_func(array($object, $methods[1]), $item);
|
call_user_func(array($object, $access[self::ACCESS_REMOVER]), $item);
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach ($itemsToAdd as $item) {
|
foreach ($itemsToAdd as $item) {
|
||||||
call_user_func(array($object, $methods[0]), $item);
|
call_user_func(array($object, $access[self::ACCESS_ADDER]), $item);
|
||||||
}
|
}
|
||||||
|
} elseif (!$access[self::ACCESS_HAS_PROPERTY] && property_exists($object, $property)) {
|
||||||
return;
|
|
||||||
} else {
|
|
||||||
// It is sufficient to include only the adders in the error
|
|
||||||
// message. If the user implements the adder but not the remover,
|
|
||||||
// an exception will be thrown in findAdderAndRemover() that
|
|
||||||
// the remover has to be implemented as well.
|
|
||||||
$guessedAdders = '"add'.implode('()", "add', $singulars).'()", ';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$setter = 'set'.$this->camelize($property);
|
|
||||||
$classHasProperty = $reflClass->hasProperty($property);
|
|
||||||
|
|
||||||
if ($reflClass->hasMethod($setter) && $reflClass->getMethod($setter)->isPublic()) {
|
|
||||||
$object->$setter($value);
|
|
||||||
} elseif ($reflClass->hasMethod('__set') && $reflClass->getMethod('__set')->isPublic()) {
|
|
||||||
$object->$property = $value;
|
|
||||||
} elseif ($classHasProperty && $reflClass->getProperty($property)->isPublic()) {
|
|
||||||
$object->$property = $value;
|
|
||||||
} elseif (!$classHasProperty && property_exists($object, $property)) {
|
|
||||||
// Needed to support \stdClass instances. We need to explicitly
|
// Needed to support \stdClass instances. We need to explicitly
|
||||||
// exclude $classHasProperty, otherwise if in the previous clause
|
// exclude $classHasProperty, otherwise if in the previous clause
|
||||||
// a *protected* property was found on the class, property_exists()
|
// a *protected* property was found on the class, property_exists()
|
||||||
// returns true, consequently the following line will result in a
|
// returns true, consequently the following line will result in a
|
||||||
// fatal error.
|
// fatal error.
|
||||||
$object->$property = $value;
|
|
||||||
|
$object->{$access[self::ACCESS_NAME]} = $value;
|
||||||
|
} elseif (self::ACCESS_TYPE_MAGIC === $access[self::ACCESS_TYPE]) {
|
||||||
|
$object->{$access[self::ACCESS_NAME]}($value);
|
||||||
|
} else {
|
||||||
|
throw new NoSuchPropertyException($access[self::ACCESS_NAME]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Guesses how to write the property value.
|
||||||
|
*
|
||||||
|
* @param string $object
|
||||||
|
* @param string $property
|
||||||
|
* @param string|null $singular
|
||||||
|
* @param mixed $value
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
private function getWriteAccessInfo($object, $property, $singular, $value)
|
||||||
|
{
|
||||||
|
$key = get_class($object).'::'.$property;
|
||||||
|
$guessedAdders = '';
|
||||||
|
|
||||||
|
if (isset($this->writePropertyCache[$key])) {
|
||||||
|
$access = $this->writePropertyCache[$key];
|
||||||
|
} else {
|
||||||
|
$access = array();
|
||||||
|
|
||||||
|
$reflClass = new \ReflectionClass($object);
|
||||||
|
$access[self::ACCESS_HAS_PROPERTY] = $reflClass->hasProperty($property);
|
||||||
|
$plural = $this->camelize($property);
|
||||||
|
|
||||||
|
// Any of the two methods is required, but not yet known
|
||||||
|
$singulars = null !== $singular ? array($singular) : (array) StringUtil::singularify($plural);
|
||||||
|
|
||||||
|
if (is_array($value) || $value instanceof \Traversable) {
|
||||||
|
$methods = $this->findAdderAndRemover($reflClass, $singulars);
|
||||||
|
|
||||||
|
if (null === $methods) {
|
||||||
|
// It is sufficient to include only the adders in the error
|
||||||
|
// message. If the user implements the adder but not the remover,
|
||||||
|
// an exception will be thrown in findAdderAndRemover() that
|
||||||
|
// the remover has to be implemented as well.
|
||||||
|
$guessedAdders = '"add'.implode('()", "add', $singulars).'()", ';
|
||||||
|
} else {
|
||||||
|
$access[self::ACCESS_TYPE] = self::ACCESS_TYPE_ADDER_AND_REMOVER;
|
||||||
|
$access[self::ACCESS_ADDER] = $methods[0];
|
||||||
|
$access[self::ACCESS_REMOVER] = $methods[1];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!isset($access[self::ACCESS_TYPE])) {
|
||||||
|
$setter = 'set'.$this->camelize($property);
|
||||||
|
$classHasProperty = $reflClass->hasProperty($property);
|
||||||
|
|
||||||
|
if ($reflClass->hasMethod($setter) && $reflClass->getMethod($setter)->isPublic()) {
|
||||||
|
$access[self::ACCESS_TYPE] = self::ACCESS_TYPE_METHOD;
|
||||||
|
$access[self::ACCESS_NAME] = $setter;
|
||||||
|
} elseif ($reflClass->hasMethod('__set') && $reflClass->getMethod('__set')->isPublic()) {
|
||||||
|
$access[self::ACCESS_TYPE] = self::ACCESS_TYPE_PROPERTY;
|
||||||
|
$access[self::ACCESS_NAME] = $property;
|
||||||
|
} elseif ($classHasProperty && $reflClass->getProperty($property)->isPublic()) {
|
||||||
|
$access[self::ACCESS_TYPE] = self::ACCESS_TYPE_PROPERTY;
|
||||||
|
$access[self::ACCESS_NAME] = $property;
|
||||||
} elseif ($this->magicCall && $reflClass->hasMethod('__call') && $reflClass->getMethod('__call')->isPublic()) {
|
} elseif ($this->magicCall && $reflClass->hasMethod('__call') && $reflClass->getMethod('__call')->isPublic()) {
|
||||||
// we call the getter and hope the __call do the job
|
// we call the getter and hope the __call do the job
|
||||||
$object->$setter($value);
|
$access[self::ACCESS_TYPE] = self::ACCESS_TYPE_MAGIC;
|
||||||
|
$access[self::ACCESS_NAME] = $setter;
|
||||||
} else {
|
} else {
|
||||||
throw new NoSuchPropertyException(sprintf(
|
$access[self::ACCESS_TYPE] = self::ACCESS_TYPE_NOT_FOUND;
|
||||||
|
$access[self::ACCESS_NAME] = sprintf(
|
||||||
'Neither the property "%s" nor one of the methods %s"%s()", '.
|
'Neither the property "%s" nor one of the methods %s"%s()", '.
|
||||||
'"__set()" or "__call()" exist and have public access in class "%s".',
|
'"__set()" or "__call()" exist and have public access in class "%s".',
|
||||||
$property,
|
$property,
|
||||||
$guessedAdders,
|
$guessedAdders,
|
||||||
$setter,
|
$setter,
|
||||||
$reflClass->name
|
$reflClass->name
|
||||||
));
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$this->writePropertyCache[$key] = $access;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $access;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Camelizes a given string.
|
* Camelizes a given string.
|
||||||
*
|
*
|
||||||
|
Reference in New Issue
Block a user