From 6160cc045ed3dd35c0805792950c410ca749e66f Mon Sep 17 00:00:00 2001 From: Carl Schwan Date: Fri, 2 Oct 2026 15:19:53 +0200 Subject: [PATCH] perf: Cache public share Avoid doing repeatibly the same SQL query Signed-off-by: Carl Schwan --- lib/CurrentUser.php | 12 ++++++++++ tests/benchmark/profile-prepend.php | 34 +++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 tests/benchmark/profile-prepend.php diff --git a/lib/CurrentUser.php b/lib/CurrentUser.php index 31d79bb33..d3a19ad1c 100644 --- a/lib/CurrentUser.php +++ b/lib/CurrentUser.php @@ -18,6 +18,9 @@ class CurrentUser { + private bool $publicShareLoaded = false; + private ?IShare $publicShare = null; + public function __construct( protected readonly IUserSession $userSession, protected readonly IRequest $request, @@ -107,6 +110,15 @@ protected function getCloudIDFromToken(): ?string { } protected function getPublicShare(): ?IShare { + if (!$this->publicShareLoaded) { + $this->publicShare = $this->loadPublicShare(); + $this->publicShareLoaded = true; + } + + return $this->publicShare; + } + + private function loadPublicShare(): ?IShare { if (basename($this->request->getScriptName()) !== 'public.php') { return null; } diff --git a/tests/benchmark/profile-prepend.php b/tests/benchmark/profile-prepend.php new file mode 100644 index 000000000..f7b71cb53 --- /dev/null +++ b/tests/benchmark/profile-prepend.php @@ -0,0 +1,34 @@ +setPeriod(0.001); + $benchProfiler->setEventType(EXCIMER_REAL); + $benchProfiler->setMaxDepth(250); + $benchProfiler->start(); + + register_shutdown_function(static function () use ($benchProfiler): void { + $benchProfiler->stop(); + $label = preg_replace('/[^A-Za-z0-9_.-]/', '', (string)$_SERVER['HTTP_X_BENCH_PROFILE']); + $dir = '/tmp/bench-profiles/' . $label; + if (!is_dir($dir)) { + @mkdir($dir, 0777, true); + } + file_put_contents($dir . '/' . uniqid('', true) . '.collapsed', $benchProfiler->getLog()->formatCollapsed()); + }); +}