From 1739007a1cf43cff17ac2d92f5c2c9694419ef41 Mon Sep 17 00:00:00 2001 From: Josh Date: Tue, 7 Jul 2026 08:32:54 -0400 Subject: [PATCH 1/2] perf(sharing): optimize duplicate share filtering and array flattening Signed-off-by: Josh --- .../lib/Controller/ShareAPIController.php | 34 ++++++++++++------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/apps/files_sharing/lib/Controller/ShareAPIController.php b/apps/files_sharing/lib/Controller/ShareAPIController.php index efff71416dc38..9b0e4b8eabd91 100644 --- a/apps/files_sharing/lib/Controller/ShareAPIController.php +++ b/apps/files_sharing/lib/Controller/ShareAPIController.php @@ -880,27 +880,30 @@ private function getSharesInDir(Node $folder): array { $nodes = $folder->getDirectoryListing(); /** @var IShare[] $shares */ - $shares = array_reduce($nodes, function ($carry, $node) { - $carry = array_merge($carry, $this->getAllShares($node, true)); - return $carry; - }, []); - - // filter out duplicate shares - $known = []; + $shares = []; + foreach ($nodes as $node) { + foreach ($this->getAllShares($node, true) as $share) { + $shares[] = $share; + } + } $formatted = $miniFormatted = []; $resharingRight = false; - $known = []; + $knownIds = []; + foreach ($shares as $share) { - if (in_array($share->getId(), $known) || $share->getSharedWith() === $this->userId) { + $shareId = $share->getId(); + + if (isset($knownIds[$shareId]) || $share->getSharedWith() === $this->userId) { continue; } try { $format = $this->formatShare($share); - $known[] = $share->getId(); + $knownIds[$shareId] = true; $formatted[] = $format; + if ($share->getSharedBy() === $this->userId) { $miniFormatted[] = $format; } @@ -1058,8 +1061,10 @@ private function getFormattedShares( $shares = $this->getSharesFromNode($viewer, $node, $reShares); - $known = $formatted = $miniFormatted = []; + $formatted = $miniFormatted = []; $resharingRight = false; + $knownIds = []; + foreach ($shares as $share) { try { $share->getNode(); @@ -1071,15 +1076,18 @@ private function getFormattedShares( continue; } - if (in_array($share->getId(), $known) + $shareId = $share->getId(); + + if (isset($knownIds[$shareId]) || ($share->getSharedWith() === $this->userId && $share->getShareType() === IShare::TYPE_USER)) { continue; } - $known[] = $share->getId(); try { /** @var IShare $share */ $format = $this->formatShare($share, $node); + + $knownIds[$shareId] = true; $formatted[] = $format; // let's also build a list of shares created From f98c19443add4dc3ce1c11e7577da5614d175055 Mon Sep 17 00:00:00 2001 From: Josh Date: Tue, 7 Jul 2026 09:30:42 -0400 Subject: [PATCH 2/2] perf(files_sharing): avoid repeated array_merge in getSharesFromNode Signed-off-by: Josh --- .../lib/Controller/ShareAPIController.php | 27 ++++++++++--------- 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/apps/files_sharing/lib/Controller/ShareAPIController.php b/apps/files_sharing/lib/Controller/ShareAPIController.php index 9b0e4b8eabd91..a166ecc7015e7 100644 --- a/apps/files_sharing/lib/Controller/ShareAPIController.php +++ b/apps/files_sharing/lib/Controller/ShareAPIController.php @@ -1834,30 +1834,33 @@ private function getSharesFromNode(string $viewer, $node, bool $reShares): array IShare::TYPE_DECK, ]; - // Should we assume that the (currentUser) viewer is the owner of the node !? + /** @var IShare[] $shares */ $shares = []; + foreach ($providers as $provider) { if (!$this->shareManager->shareProviderExists($provider)) { continue; } - $providerShares - = $this->shareManager->getSharesBy($viewer, $provider, $node, $reShares, -1, 0); - $shares = array_merge($shares, $providerShares); + // Should we assume that the (currentUser) viewer is the owner of the node !? + $providerShares = $this->shareManager->getSharesBy($viewer, $provider, $node, $reShares, -1, 0); + if (!empty($providerShares)) { + array_push($shares, ...$providerShares); + } } if ($this->shareManager->outgoingServer2ServerSharesAllowed()) { - $federatedShares = $this->shareManager->getSharesBy( - $this->userId, IShare::TYPE_REMOTE, $node, $reShares, -1, 0 - ); - $shares = array_merge($shares, $federatedShares); + $providerShares = $this->shareManager->getSharesBy($this->userId, IShare::TYPE_REMOTE, $node, $reShares, -1, 0); + if (!empty($providerShares)) { + array_push($shares, ...$providerShares); + } } if ($this->shareManager->outgoingServer2ServerGroupSharesAllowed()) { - $federatedShares = $this->shareManager->getSharesBy( - $this->userId, IShare::TYPE_REMOTE_GROUP, $node, $reShares, -1, 0 - ); - $shares = array_merge($shares, $federatedShares); + $providerShares = $this->shareManager->getSharesBy($this->userId, IShare::TYPE_REMOTE_GROUP, $node, $reShares, -1, 0); + if (!empty($providerShares)) { + array_push($shares, ...$providerShares); + } } return $shares;