Skip to content

Commit 3da64c6

Browse files
committed
Create functions for setting and unsetting non public-set properties only once (performance optimization)
1 parent 92a6e4c commit 3da64c6

File tree

2 files changed

+27
-25
lines changed

2 files changed

+27
-25
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
1515
- Simplify finding missing fields (performance optimization)
1616
- Reduce calls needed to find field names for "did you mean" error helper (performance optimization)
1717
- Call `unset()` only for initialized properties (performance optimization)
18+
- Create functions for setting and unsetting non public-set properties only once (performance optimization)
1819
- `ArrayShapeRule`
1920
- Simplify finding missing fields (performance optimization)
2021
- Reduce calls needed to find field names for "did you mean" error helper (performance optimization)

src/Processing/DefaultProcessor.php

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
use Orisai\ObjectMapper\Types\MappedObjectType;
3030
use Orisai\ObjectMapper\Types\MessageType;
3131
use Orisai\ObjectMapper\Types\Type;
32-
use ReflectionProperty;
3332
use function array_key_exists;
3433
use function array_keys;
3534
use function array_map;
@@ -51,7 +50,10 @@ final class DefaultProcessor implements Processor
5150
private ServicesContext $services;
5251

5352
/** @var Closure(MappedObject, string, mixed): void */
54-
private ?Closure $setterFunction;
53+
private Closure $setFunction;
54+
55+
/** @var Closure(MappedObject, string): void */
56+
private Closure $unsetFunction;
5557

5658
/** @var array<class-string<MappedObject>, RuntimeMeta> */
5759
private array $metaCache = [];
@@ -67,9 +69,12 @@ public function __construct(MetaLoader $metaLoader, RuleManager $ruleManager, Ob
6769
$this->rawValuesMap = new RawValuesMap();
6870
$this->services = new ServicesContext($metaLoader, $ruleManager, $this);
6971
// phpcs:disable SlevomatCodingStandard.Functions.StaticClosure
70-
$this->setterFunction = function (MappedObject $object, string $name, $value): void {
72+
$this->setFunction = function (MappedObject $object, string $name, $value): void {
7173
$object->$name = $value;
7274
};
75+
$this->unsetFunction = function (MappedObject $object, string $name): void {
76+
unset($object->$name);
77+
};
7378
// phpcs:enable
7479
}
7580

@@ -557,13 +562,29 @@ private function fillObject(
557562
$fieldsMeta = $meta->fields;
558563

559564
if ($dynamic->getOptions()->getRequiredFields() === RequiredFields::none()) {
565+
$unsetter = $this->unsetFunction;
566+
560567
// Reset mapped properties state
561568
foreach ($fieldsMeta as $fieldMeta) {
562-
$this->objectUnset($object, $fieldMeta->property);
569+
$property = $fieldMeta->property;
570+
$declaringClass = $property->getDeclaringClass();
571+
$name = $property->getName();
572+
573+
if (
574+
$property->isInitialized($object)
575+
&& $property->isPublic()
576+
&& (PHP_VERSION_ID < 8_01_00 || !$property->isReadOnly())
577+
) {
578+
unset($object->$name);
579+
} else {
580+
// phpcs:disable SlevomatCodingStandard.Functions.StaticClosure
581+
$unsetter->bindTo($object, $declaringClass->getName())($object, $name);
582+
// phpcs:enable
583+
}
563584
}
564585
}
565586

566-
$setter = $this->setterFunction;
587+
$setter = $this->setFunction;
567588

568589
// Set processed data
569590
foreach ($data as $fieldName => $value) {
@@ -586,24 +607,4 @@ public function getRawValues(MappedObject $object)
586607
return $this->rawValuesMap->getRawValues($object);
587608
}
588609

589-
private function objectUnset(MappedObject $object, ReflectionProperty $property): void
590-
{
591-
$declaringClass = $property->getDeclaringClass();
592-
$name = $property->getName();
593-
594-
if (
595-
$property->isInitialized($object)
596-
&& $property->isPublic()
597-
&& (PHP_VERSION_ID < 8_01_00 || !$property->isReadOnly())
598-
) {
599-
unset($object->$name);
600-
} else {
601-
// phpcs:disable SlevomatCodingStandard.Functions.StaticClosure
602-
(function () use ($object, $name): void {
603-
unset($object->$name);
604-
})->bindTo($object, $declaringClass->getName())();
605-
// phpcs:enable
606-
}
607-
}
608-
609610
}

0 commit comments

Comments
 (0)