Skip to content

Commit

Permalink
perf: Avoid updating the folder size if we know the size difference
Browse files Browse the repository at this point in the history
Signed-off-by: Julius Härtl <jus@bitgrid.net>
  • Loading branch information
juliusknorr committed Jan 5, 2024
1 parent fdb4e77 commit 7db5bcc
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 7 deletions.
6 changes: 3 additions & 3 deletions lib/private/Files/Cache/Updater.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public function propagate($path, $time = null) {
* @param string $path
* @param int $time
*/
public function update($path, $time = null) {
public function update($path, $time = null, ?int $sizeDifference = null) {
if (!$this->enabled or Scanner::isPartialFile($path)) {
return;
}
Expand All @@ -135,10 +135,10 @@ public function update($path, $time = null) {
$sizeDifference = $data['size'] - $data['oldSize'];
} else {
// scanner didn't provide size info, fallback to full size calculation
$sizeDifference = 0;
if ($this->cache instanceof Cache) {
if ($this->cache instanceof Cache && $sizeDifference !== 0) {
$this->cache->correctFolderSize($path, $data);
}
$sizeDifference = $sizeDifference ?? 0;
}
$this->correctParentStorageMtime($path);
$this->propagator->propagateChange($path, $time, $sizeDifference);
Expand Down
8 changes: 5 additions & 3 deletions lib/private/Files/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -287,12 +287,12 @@ public function enableCacheUpdate(): void {
$this->updaterEnabled = true;
}

protected function writeUpdate(Storage $storage, string $internalPath, ?int $time = null): void {
protected function writeUpdate(Storage $storage, string $internalPath, ?int $time = null, ?int $sizeDifference = null): void {
if ($this->updaterEnabled) {
if (is_null($time)) {
$time = time();
}
$storage->getUpdater()->update($internalPath, $time);
$storage->getUpdater()->update($internalPath, $time, $sizeDifference);
}
}

Expand Down Expand Up @@ -1173,7 +1173,9 @@ private function basicOperation(string $operation, string $path, array $hooks =
$this->removeUpdate($storage, $internalPath);
}
if ($result !== false && in_array('write', $hooks, true) && $operation !== 'fopen' && $operation !== 'touch') {
$this->writeUpdate($storage, $internalPath);
$isCreateOperation = $operation === 'mkdir' || in_array('create', $hooks, true);
$sizeDifference = $operation === 'mkdir' ? 0 : $result;
$this->writeUpdate($storage, $internalPath, null, $isCreateOperation ? $sizeDifference : null);
}
if ($result !== false && in_array('touch', $hooks)) {
$this->writeUpdate($storage, $internalPath, $extraParam);
Expand Down
2 changes: 1 addition & 1 deletion lib/public/Files/Cache/IUpdater.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function propagate($path, $time = null);
* @param int $time
* @since 9.0.0
*/
public function update($path, $time = null);
public function update($path, $time = null, ?int $sizeDifference = null);

/**
* Remove $path from the cache and update the size, etag and mtime of the parent folders
Expand Down

0 comments on commit 7db5bcc

Please sign in to comment.