Skip to content

Commit 1a36611

Browse files
authored
perf: cache type keys in DataConverter to avoid repeated array_keys calls (#10753)
1 parent 9b8ecca commit 1a36611

1 file changed

Lines changed: 10 additions & 2 deletions

File tree

‎system/DataConverter/DataConverter.php‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@
2929
*/
3030
private DataCaster $dataCaster;
3131

32+
/**
33+
* List of field names defined in $types.
34+
*
35+
* @var list<string>
36+
*/
37+
private array $typeFields;
38+
3239
/**
3340
* @param array<string, class-string> $castHandlers Custom convert handlers
3441
*
@@ -62,6 +69,7 @@ public function __construct(
6269
private Closure|string|null $extractor = null,
6370
) {
6471
$this->dataCaster = new DataCaster($castHandlers, $types, $this->helper);
72+
$this->typeFields = array_keys($types);
6573
}
6674

6775
/**
@@ -75,7 +83,7 @@ public function __construct(
7583
*/
7684
public function fromDataSource(array $data): array
7785
{
78-
foreach (array_keys($this->types) as $field) {
86+
foreach ($this->typeFields as $field) {
7987
if (array_key_exists($field, $data)) {
8088
$data[$field] = $this->dataCaster->castAs($data[$field], $field, 'get');
8189
}
@@ -95,7 +103,7 @@ public function fromDataSource(array $data): array
95103
*/
96104
public function toDataSource(array $phpData): array
97105
{
98-
foreach (array_keys($this->types) as $field) {
106+
foreach ($this->typeFields as $field) {
99107
if (array_key_exists($field, $phpData)) {
100108
$phpData[$field] = $this->dataCaster->castAs($phpData[$field], $field, 'set');
101109
}

0 commit comments

Comments
 (0)