From 94175ce432e30712fa46ceff1fdee49727542263 Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Sun, 5 May 2013 10:58:52 +0200 Subject: [PATCH] Make performance of the class loader more constant across classes --- src/Composer/Autoload/ClassLoader.php | 34 +++++++++++++++------------ 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/src/Composer/Autoload/ClassLoader.php b/src/Composer/Autoload/ClassLoader.php index bcf980915..739e5cc96 100644 --- a/src/Composer/Autoload/ClassLoader.php +++ b/src/Composer/Autoload/ClassLoader.php @@ -49,7 +49,7 @@ class ClassLoader public function getPrefixes() { - return $this->prefixes; + return call_user_func_array('array_merge', $this->prefixes); } public function getFallbackDirs() @@ -98,19 +98,20 @@ class ClassLoader return; } - if (!isset($this->prefixes[$prefix])) { - $this->prefixes[$prefix] = (array) $paths; + $first = substr($prefix, 0, 1); + if (!isset($this->prefixes[$first][$prefix])) { + $this->prefixes[$first][$prefix] = (array) $paths; return; } if ($prepend) { - $this->prefixes[$prefix] = array_merge( + $this->prefixes[$first][$prefix] = array_merge( (array) $paths, - $this->prefixes[$prefix] + $this->prefixes[$first][$prefix] ); } else { - $this->prefixes[$prefix] = array_merge( - $this->prefixes[$prefix], + $this->prefixes[$first][$prefix] = array_merge( + $this->prefixes[$first][$prefix], (array) $paths ); } @@ -129,7 +130,7 @@ class ClassLoader return; } - $this->prefixes[$prefix] = (array) $paths; + $this->prefixes[substr($prefix, 0, 1)][$prefix] = (array) $paths; } /** @@ -205,7 +206,7 @@ class ClassLoader if (false !== $pos = strrpos($class, '\\')) { // namespaced class name - $classPath = str_replace('\\', DIRECTORY_SEPARATOR, substr($class, 0, $pos)) . DIRECTORY_SEPARATOR; + $classPath = strtr(substr($class, 0, $pos), '\\', DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR; $className = substr($class, $pos + 1); } else { // PEAR-like class name @@ -213,13 +214,16 @@ class ClassLoader $className = $class; } - $classPath .= str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php'; + $classPath .= strtr($className, '_', DIRECTORY_SEPARATOR) . '.php'; - foreach ($this->prefixes as $prefix => $dirs) { - if (0 === strpos($class, $prefix)) { - foreach ($dirs as $dir) { - if (file_exists($dir . DIRECTORY_SEPARATOR . $classPath)) { - return $dir . DIRECTORY_SEPARATOR . $classPath; + $first = substr($class, 0, 1); + if (isset($this->prefixes[$first])) { + foreach ($this->prefixes[$first] as $prefix => $dirs) { + if (0 === strpos($class, $prefix)) { + foreach ($dirs as $dir) { + if (file_exists($dir . DIRECTORY_SEPARATOR . $classPath)) { + return $dir . DIRECTORY_SEPARATOR . $classPath; + } } } }