Hi,

I had a discussion recently about the performance of the component's autoload 
infrastructure. I did a little profiling and found some places to speed the 
autoload process.
I don't copy the numbers here, as you get a better picture by looking at 
kcachegrind on your own. You can use the following short script:

require "your/path/to/Base/src/base.php";
function __autoload( $className )
{
        ezcBase::autoload( $className );
}
echo ezcArchiveV7Tar::TAR;
echo ezcConsoleInput::TYPE_NONE;

The following things could be optimized IMHO:

* ezcBase::setPackageDir is called for every __autoload() which is unnecessary 
and expensive

* This hack in ezcBase::autoload() doesn't use preg_match and therefore is 
faster but also more ugly:


        $fileNames = array();

        $prefix = substr( $className, 0, strspn( 
$className, 'abcdefghijklmnopqrstuvwxyz') );

        $trimmedClassName = trim( 
$className, 'abcdefghijklmnopqrstuvwxyz0123456789' );

        $firstWordLength =strspn( 
$trimmedClassName, 'abcdefghijklmnopqrstuvwxyz0123456789', 1 )+1; 
        $firstWord = substr( $trimmedClassName, 0, $firstWordLength);
        $firstWord[0] = strtolower( $firstWord[0] );

        $secondWord = substr( $trimmedClassName, 
                                $firstWordLength, 
                                strspn( 
                                    $trimmedClassName, 
                                    'abcdefghijklmnopqrstuvwxyz0123456789',
                                    $firstWordLength+1)+1 );
        
        $secondWord[0] = strtolower( $secondWord[0] );

        if( $secondWord !== "" && ezcBase::requireFile( $fileNames[] = 
$firstWord.'_'.$secondWord.'_autoload.php', $className, $prefix ) )
        {
            return true;
        }

        if( ezcBase::requireFile( $fileNames[] = $firstWord.'_autoload.php', 
$className, $prefix ) )
        {
            return true;
        }

        if( ezcBase::requireFile( $fileNames[] = 'autoload.php', $className, 
$firstWord    ) )
        {
            return true;
        }

* In ezcBase::requireFile you first check the existence of the autoload-array 
file with file_exists and afterwards require it.
  The file_exists is nearly as slow as a preg_match. You could do do the 
following instead:

        if ( $prefix === 'ezc' 
          && is_array( $array = @include "$autoloadDir$fileName" ) 
          && array_key_exists( $className, $array ) )
        {
            // Add the array to the cache, and include the requested file.
            ezcBase::$autoloadArray = array_merge( ezcBase::$autoloadArray, 
$array );

* There's another file_exists to eliminate in loadFile().

Instead of changing the current ezcBase, you could add a second base.php file 
to Base which is optimized for speed and doesn't do so much 
checking. In the application I can simply switch between the two ezcBase 
classes.

Cheers,

Thomas Koch
-- 
Components mailing list
[email protected]
http://lists.ez.no/mailman/listinfo/components

Reply via email to