Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b324445

Browse files
committed
feature #29999 [PropertyAccess] speed up accessing object properties (xabbuh)
This PR was merged into the 4.3-dev branch. Discussion ---------- [PropertyAccess] speed up accessing object properties | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #28926, #29405 | License | MIT | Doc PR | I propose to improve the performance of the `ObjectNormalizer` by not adding a new interface to the PropertyAccess component, but by adding some shortcut for cases where we know that we do not need to perform all checks. The added benefit is that this will not only speed up the `ObjectNormalizer` class, but will be available for every consumer of the `PropertyAccessor` without having to adapt to a new API. TODO: - [ ] confirm that these changes indeed introduce the same benefit as #29405 doing an actual benchmark Commits ------- ef7876e speed up accessing object properties
2 parents 3417a15 + ef7876e commit b324445

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

src/Symfony/Component/PropertyAccess/PropertyAccessor.php

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,16 @@ public function __construct(bool $magicCall = false, bool $throwExceptionOnInval
8282
*/
8383
public function getValue($objectOrArray, $propertyPath)
8484
{
85-
$propertyPath = $this->getPropertyPath($propertyPath);
86-
8785
$zval = [
8886
self::VALUE => $objectOrArray,
8987
];
88+
89+
if (\is_object($objectOrArray) && false === strpbrk((string) $propertyPath, '.[')) {
90+
return $this->readProperty($zval, $propertyPath)[self::VALUE];
91+
}
92+
93+
$propertyPath = $this->getPropertyPath($propertyPath);
94+
9095
$propertyValues = $this->readPropertiesUntil($zval, $propertyPath, $propertyPath->getLength(), $this->ignoreInvalidIndices);
9196

9297
return $propertyValues[\count($propertyValues) - 1][self::VALUE];
@@ -97,6 +102,22 @@ public function getValue($objectOrArray, $propertyPath)
97102
*/
98103
public function setValue(&$objectOrArray, $propertyPath, $value)
99104
{
105+
if (\is_object($objectOrArray) && false === strpbrk((string) $propertyPath, '.[')) {
106+
$zval = [
107+
self::VALUE => $objectOrArray,
108+
];
109+
110+
try {
111+
$this->writeProperty($zval, $propertyPath, $value);
112+
113+
return;
114+
} catch (\TypeError $e) {
115+
self::throwInvalidArgumentException($e->getMessage(), $e->getTrace(), 0, $propertyPath);
116+
// It wasn't thrown in this class so rethrow it
117+
throw $e;
118+
}
119+
}
120+
100121
$propertyPath = $this->getPropertyPath($propertyPath);
101122

102123
$zval = [

0 commit comments

Comments
 (0)