]> source.dussan.org Git - nextcloud-server.git/commitdiff
Cache results of `normalizePath`
authorLukas Reschke <lukas@owncloud.com>
Sat, 10 Jan 2015 09:48:28 +0000 (10:48 +0100)
committerLukas Reschke <lukas@owncloud.com>
Sat, 10 Jan 2015 10:10:38 +0000 (11:10 +0100)
`normalizePath` is a rather expensive operation and called multiple times for a single path for every file related operation.

In my development installation with about 9GB of data and 60k files this leads to a performance boost of 24% - in seconds that are 1.86s (!) - for simple searches. With more files the impact will be even more noticeable. Obviously this affects every operation that has in any regard something to do with using OC\Files\Filesystem.

Part of https://github.com/owncloud/core/issues/13221

lib/private/files/filesystem.php

index ed2be59c092e6ed8c046a959caf242a5af9485b8..bc43eb5d1ce75021096214fe3bfda0adf69c4541 100644 (file)
@@ -47,6 +47,8 @@ class Filesystem {
 
        static private $usersSetup = array();
 
+       static private $normalizedPathCache = array();
+
        /**
         * classname which used for hooks handling
         * used as signalclass in OC_Hooks::emit()
@@ -713,6 +715,12 @@ class Filesystem {
         * @return string
         */
        public static function normalizePath($path, $stripTrailingSlash = true, $isAbsolutePath = false) {
+               $cacheKey = $path.'-'.-$stripTrailingSlash.'-'.$isAbsolutePath;
+
+               if(isset(self::$normalizedPathCache[$cacheKey])) {
+                       return self::$normalizedPathCache[$cacheKey];
+               }
+
                if ($path == '') {
                        return '/';
                }
@@ -756,7 +764,10 @@ class Filesystem {
                //normalize unicode if possible
                $path = \OC_Util::normalizeUnicode($path);
 
-               return $windows_drive_letter . $path;
+               $normalizedPath = $windows_drive_letter . $path;
+               self::$normalizedPathCache[$cacheKey] = $normalizedPath;
+
+               return $normalizedPath;
        }
 
        /**