Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/FieldTypeSystem/FieldManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ final class FieldManager
*/
private array $cachedInstances = [];

/**
* @var array<string, FieldTypeData|null>
*/
private array $cachedFieldTypeData = [];

/**
* @param array<string, array<int, string> | string> | Closure $fieldTypes
*/
Expand Down Expand Up @@ -132,7 +137,11 @@ public function getFieldType(?string $fieldType): ?FieldTypeData
return null;
}

return $this->toCollection()->firstWhere('key', $fieldType);
if (! array_key_exists($fieldType, $this->cachedFieldTypeData)) {
$this->cachedFieldTypeData[$fieldType] = $this->toCollection()->firstWhere('key', $fieldType);
}

return $this->cachedFieldTypeData[$fieldType];
}

/**
Expand Down
16 changes: 16 additions & 0 deletions src/Services/Visibility/BackendVisibilityService.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,19 @@ public function extractFieldValues(Model $record, Collection $fields): array
$record->load('customFieldValues.customField');
}

// Memoise against the loaded relation object so the memo has exactly
// the lifetime of the relation this method caches above: a refresh()
// or load() installs a new collection and drops the memo with it.
static $cache = null;
$cache ??= new \WeakMap();
$loadedValues = $record->getRelation('customFieldValues');
$signature = $fields->pluck('id')->implode(',');
$memoised = $cache[$loadedValues] ?? [];

if (array_key_exists($signature, $memoised)) {
return $memoised[$signature];
}

$fieldValues = [];

foreach ($fields as $field) {
Expand All @@ -94,6 +107,9 @@ public function extractFieldValues(Model $record, Collection $fields): array
);
}

$memoised[$signature] = $fieldValues;
$cache[$loadedValues] = $memoised;

return $fieldValues;
}

Expand Down