feat: performance improvements

This commit is contained in:
Björn Fromme
2026-03-16 11:59:11 +01:00
parent 4decaa6789
commit 1e464cef0a
6 changed files with 122 additions and 103 deletions
@@ -35,6 +35,9 @@ use App\Service\ServiceAvailabilityCalculator;
*/
class ParticipantFieldOptionsProvider extends AbstractFieldOptionsProvider
{
/** @var array<string, array<string, mixed>> Request-scoped cache for field options */
private array $optionsCache = [];
/**
* Initializes the provider with required dependencies.
*
@@ -57,6 +60,39 @@ class ParticipantFieldOptionsProvider extends AbstractFieldOptionsProvider
parent::__construct();
}
/**
* Retrieves form field options with request-scoped caching.
*
* Overrides parent to add instance-level caching, preventing redundant
* field option calculations when the same field is accessed multiple times
* during form building (happens ~16 times per participant × 50 participants = 800 calls).
*
* Cache key includes: field name, participant index, mode, and date of birth.
* The cache is automatically cleared between requests since the service is request-scoped.
*
* @param string $fieldName The name of the field to configure
* @param BookingDto $bookingDto The current booking data for context
* @param int $participantIndex The index of the participant being configured
* @param array $options Additional options to customize field behavior
*
* @return array<string, mixed> Symfony form field options, or empty array if field not supported
*/
public function getFieldOptions(string $fieldName, BookingDto $bookingDto, int $participantIndex, array $options = []): array
{
// Generate cache key based on factors that affect field options
$participant = $bookingDto->getParticipant($participantIndex);
$cacheKey = sprintf(
'%s_%d_%s_%s',
$fieldName,
$participantIndex,
$bookingDto->getMode(),
$participant?->dateOfBirth?->format('Y-m-d') ?? 'no_dob'
);
// Return cached result if available, otherwise compute and cache
return $this->optionsCache[$cacheKey] ??= parent::getFieldOptions($fieldName, $bookingDto, $participantIndex, $options);
}
/**
* Registers all field option providers during service initialization.
*