From a6f432f29dad582fb682beef0022434ff481be54 Mon Sep 17 00:00:00 2001 From: Carl Schwan Date: Fri, 19 Jun 2026 12:15:35 +0200 Subject: [PATCH] perf: Only sort wrappers when adding them Instead of doing that each time a new mount point is created (e.g. for the 7000 shares we have in production). Signed-off-by: Carl Schwan --- lib/private/Files/Storage/StorageFactory.php | 22 ++++++++++++-------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/lib/private/Files/Storage/StorageFactory.php b/lib/private/Files/Storage/StorageFactory.php index bddfaeb0da8aa..5d694b2f7fca9 100644 --- a/lib/private/Files/Storage/StorageFactory.php +++ b/lib/private/Files/Storage/StorageFactory.php @@ -17,9 +17,11 @@ class StorageFactory implements IStorageFactory { /** - * @var array[] [$name=>['priority'=>$priority, 'wrapper'=>$callable] $storageWrappers + * @var array $storageWrappers */ - private $storageWrappers = []; + private array $storageWrappers = []; + /** @var bool $dirty Whether the list of storage wrappers is sorted */ + private bool $dirty = true; #[\Override] public function addStorageWrapper(string $wrapperName, callable $callback, int $priority = 50, array $existingMounts = []): bool { @@ -33,6 +35,7 @@ public function addStorageWrapper(string $wrapperName, callable $callback, int $ } $this->storageWrappers[$wrapperName] = ['wrapper' => $callback, 'priority' => $priority]; + $this->dirty = true; return true; } @@ -44,6 +47,7 @@ public function addStorageWrapper(string $wrapperName, callable $callback, int $ */ public function removeStorageWrapper(string $wrapperName): void { unset($this->storageWrappers[$wrapperName]); + $this->dirty = true; } /** @@ -58,14 +62,14 @@ public function getInstance(IMountPoint $mountPoint, string $class, array $argum } public function wrap(IMountPoint $mountPoint, IStorage $storage): IStorage { + if ($this->dirty) { + uasort($this->storageWrappers, static fn (array $a, array $b) => $b['priority'] - $a['priority']); + $this->dirty = false; + } $wrappers = array_values($this->storageWrappers); - usort($wrappers, function ($a, $b) { - return $b['priority'] - $a['priority']; - }); - /** @var callable[] $wrappers */ - $wrappers = array_map(function ($wrapper) { - return $wrapper['wrapper']; - }, $wrappers); + + /** @var list $wrappers */ + $wrappers = array_map(static fn (array $wrapper): callable => $wrapper['wrapper'], $wrappers); foreach ($wrappers as $wrapper) { $storage = $wrapper($mountPoint->getMountPoint(), $storage, $mountPoint); if (!($storage instanceof IStorage)) {