Hello,
We've discovered a performance issue in the loadEnabledVariations method of the ProductVariationAttribtesWidgetCustom class when dealing with a large number of product variations (around 4000+).
The issue seems to be related to the way the method handles caching. When the cache is empty, the method loads all enabled variations for a product and then sets them in the cache. However, when dealing with a large number of variations, this operation can be quite slow and resource-intensive.
Here's the relevant code snippet:
protected function loadEnabledVariations(ProductInterface $product) {
$cache_key = 'custom_module:enabled_variations:' . $product->id();
$cacheBackend = \Drupal::service('cache.default');
$langcode = $product->language()->getId();
if ($cache = $cacheBackend->get($cache_key)) {
$variations = $cache->data;
} else {
$variations = $this->variationStorage->loadEnabled($product);
$cacheBackend->set($cache_key, $variations, CacheBackendInterface::CACHE_PERMANENT, ['commerce_product:' . $product->id()]);
}
foreach ($variations as $key => $variation) {
$variations[$key] = $this->entityRepository->getTranslationFromContext($variation, $langcode);
}
return $variations;
}
One potential solution could be to implement pagination or some form of lazy loading for the variations, so that only a subset of the variations is loaded and cached at a time. This could significantly reduce the amount of data that needs to be processed and stored in the cache, improving performance.
We would appreciate any feedback or suggestions on how to best address this issue.
Thank you.