From ae0790613ee5f43a9da3cb7438eead22b332e531 Mon Sep 17 00:00:00 2001
From: Christian Kuhn <lolli@schwarzbu.ch>
Date: Sun, 1 Jul 2012 12:59:18 +0200
Subject: [PATCH] [TASK] Autoloader performance improvement

If the autoload registry is read from cache, all class names are
already lowercased. The patch calls the lowercase logic only if
the registry files are not read from cache. The unit tests show
that nothing breaks with this patch.

Cachegrind shows ~22% of rendering time is spend in
loadCoreAndExtensionRegistry() on a fully cached page,
this is reduced to ~2% with the patch.

Change-Id: I07dc95b29bf05970061b5fb2f9a5d8a5ba194960
Resolves: #38498
Releases: 6.0
Reviewed-on: http://review.typo3.org/12494
Reviewed-by: Wouter Wolters
Tested-by: Wouter Wolters
Reviewed-by: Susanne Moog
Tested-by: Susanne Moog
---
 t3lib/class.t3lib_autoloader.php | 30 ++++++++++++++++++++----------
 1 file changed, 20 insertions(+), 10 deletions(-)

diff --git a/t3lib/class.t3lib_autoloader.php b/t3lib/class.t3lib_autoloader.php
index f2ac8a96fb63..564de46a425e 100644
--- a/t3lib/class.t3lib_autoloader.php
+++ b/t3lib/class.t3lib_autoloader.php
@@ -143,7 +143,7 @@ class t3lib_autoloader {
 			$classRegistry = $phpCodeCache->requireOnce(self::getAutoloadCacheIdentifier());
 		} else {
 			self::$cacheUpdateRequired = TRUE;
-			$classRegistry = self::createCoreAndExtensionRegistry();
+			$classRegistry = self::lowerCaseClassRegistry(self::createCoreAndExtensionRegistry());
 		}
 
 			// This can only happen if the autoloader was already registered
@@ -154,18 +154,11 @@ class t3lib_autoloader {
 			// switched to NullBackend for example to simplify development
 		if (!is_array($classRegistry)) {
 			self::$cacheUpdateRequired = TRUE;
-			$classRegistry = self::createCoreAndExtensionRegistry();
+			$classRegistry = self::lowerCaseClassRegistry(self::createCoreAndExtensionRegistry());
 		}
 
-			// Lowercase all keys. We must use the multi byte safe version
-			// of strtolower from t3lib_div here, so array_change_key_case()
-			// can not be used
-		$lowerCasedClassRegistry = array();
-		foreach ($classRegistry as $className => $classFile) {
-			$lowerCasedClassRegistry[t3lib_div::strtolower($className)] = $classFile;
-		}
 
-		self::$classNameToFileMapping = $lowerCasedClassRegistry;
+		self::$classNameToFileMapping = $classRegistry;
 	}
 
 	/**
@@ -391,5 +384,22 @@ class t3lib_autoloader {
 		}
 		return self::$autoloadCacheIdentifier;
 	}
+
+	/**
+	 * Lowercase all keys of the class registry.
+	 *
+	 * Use the multi byte safe version of strtolower from t3lib_div,
+	 * so array_change_key_case() can not be used
+	 *
+	 * @param array $registry Given registry entries
+	 * @return array with lower cased keys
+	 */
+	protected static function lowerCaseClassRegistry($registry) {
+		$lowerCasedClassRegistry = array();
+		foreach ($registry as $className => $classFile) {
+			$lowerCasedClassRegistry[t3lib_div::strtolower($className)] = $classFile;
+		}
+		return $lowerCasedClassRegistry;
+	}
 }
 ?>
\ No newline at end of file
-- 
GitLab