From 3be9591930688dc8be146095a321cdb280098e2b Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Wed, 3 Jan 2018 16:24:22 +0100 Subject: [PATCH] Simplify some ClassLoader code, minor memory improvement, fixes #6937 --- src/Composer/Autoload/AutoloadGenerator.php | 8 ++++++++ src/Composer/Autoload/ClassLoader.php | 18 ++++++++---------- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/src/Composer/Autoload/AutoloadGenerator.php b/src/Composer/Autoload/AutoloadGenerator.php index 9b9710d9e..dca6a7553 100644 --- a/src/Composer/Autoload/AutoloadGenerator.php +++ b/src/Composer/Autoload/AutoloadGenerator.php @@ -783,6 +783,14 @@ HEADER; } } + // BC handling when converting to a new ClassLoader + if (isset($maps['prefixLengthsPsr4'])) { + $maps['firstCharsPsr4'] = array_map(function () { + return true; + }, $maps['prefixLengthsPsr4']); + unset($maps['prefixLengthsPsr4']); + } + foreach ($maps as $prop => $value) { if (count($value) > 32767) { // Static arrays are limited to 32767 values on PHP 5.6 diff --git a/src/Composer/Autoload/ClassLoader.php b/src/Composer/Autoload/ClassLoader.php index 6e8d09f43..bebe83706 100644 --- a/src/Composer/Autoload/ClassLoader.php +++ b/src/Composer/Autoload/ClassLoader.php @@ -43,7 +43,7 @@ namespace Composer\Autoload; class ClassLoader { // PSR-4 - private $prefixLengthsPsr4 = array(); + private $firstCharsPsr4 = array(); private $prefixDirsPsr4 = array(); private $fallbackDirsPsr4 = array(); @@ -170,11 +170,10 @@ class ClassLoader } } elseif (!isset($this->prefixDirsPsr4[$prefix])) { // Register directories for a new namespace. - $length = strlen($prefix); - if ('\\' !== $prefix[$length - 1]) { + if ('\\' !== substr($prefix, -1)) { throw new \InvalidArgumentException("A non-empty PSR-4 prefix must end with a namespace separator."); } - $this->prefixLengthsPsr4[$prefix[0]][$prefix] = $length; + $this->firstCharsPsr4[$prefix[0]] = true; $this->prefixDirsPsr4[$prefix] = (array) $paths; } elseif ($prepend) { // Prepend directories for an already registered namespace. @@ -221,11 +220,10 @@ class ClassLoader if (!$prefix) { $this->fallbackDirsPsr4 = (array) $paths; } else { - $length = strlen($prefix); - if ('\\' !== $prefix[$length - 1]) { + if ('\\' !== substr($prefix, -1)) { throw new \InvalidArgumentException("A non-empty PSR-4 prefix must end with a namespace separator."); } - $this->prefixLengthsPsr4[$prefix[0]][$prefix] = $length; + $this->firstCharsPsr4[$prefix[0]] = true; $this->prefixDirsPsr4[$prefix] = (array) $paths; } } @@ -373,15 +371,15 @@ class ClassLoader $logicalPathPsr4 = strtr($class, '\\', DIRECTORY_SEPARATOR) . $ext; $first = $class[0]; - if (isset($this->prefixLengthsPsr4[$first])) { + if (isset($this->firstCharsPsr4[$first])) { $subPath = $class; while (false !== $lastPos = strrpos($subPath, '\\')) { $subPath = substr($subPath, 0, $lastPos); $search = $subPath.'\\'; if (isset($this->prefixDirsPsr4[$search])) { - $length = $this->prefixLengthsPsr4[$first][$search]; + $pathEnd = substr($logicalPathPsr4, $lastPos + 1); foreach ($this->prefixDirsPsr4[$search] as $dir) { - if (file_exists($file = $dir . DIRECTORY_SEPARATOR . substr($logicalPathPsr4, $length))) { + if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $pathEnd)) { return $file; } }