XF/AddOn/Manager::coerceAddOnId causes performance issues with large number of add-ons

Painbaker

Well-known member
Affected version
2.3.6
The XF/AddOn/Manager::coerceAddOnId() function repeatedly calls XF/AddOn/Manager::getAvailableAddOnIds() (which is itself relatively slow) and performs repeated array transformations. This leads to a significant performance bottleneck, especially noticeable during active add-on cache rebuilds.

PHP:
protected function coerceAddOnId($addOnId)
{
    $addOnIds = $this->getAvailableAddOnIds();

    $index = array_search(strtoupper($addOnId), array_map('strtoupper', $addOnIds));

    if ($index !== false)
    {
       return $addOnIds[$index];
    }

    return $addOnId;
}

On a test installation with over 600 active add-ons, the original code takes 21 seconds to execute add-on version bump command.


With caching: ~0.2477 seconds
PHP:
    protected $addOnIdsCache = null;
    protected $uppercaseAddonIdMap = null;

    protected function coerceAddOnId($addOnId)
    {
        if ($this->addOnIdsCache === null)
        {
            $this->addOnIdsCache = $this->getAvailableAddOnIds();
        }

        if ($this->uppercaseAddonIdMap === null)
        {
            $this->uppercaseAddonIdMap = array_combine(
                array_map('strtoupper', $this->addOnIdsCache),
                $this->addOnIdsCache
            );
        }

        $upperAddOnId = strtoupper($addOnId);
        return $this->uppercaseAddonIdMap[$upperAddOnId] ?? $addOnId;
    }
 
Last edited:
Back
Top Bottom