2017 © Pedro Peláez
 

library speed-loader

Load classes faster

image

thadafinser/speed-loader

Load classes faster

  • Monday, April 13, 2015
  • by ThaDafinser
  • Repository
  • 1 Watchers
  • 1 Stars
  • 55 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 1 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

SpeedLoader - load only one file

Build Status Code Coverage Scrutinizer Code Quality, (*1)

One file to load all your needed classes at once! Since autoloading takes more and more time, it has become very important to have an efficient autoloading. SpeedLoader aims to improve your autoloading experience, (*2)

Install

Get it with composer:, (*3)

composer require thadafinser/speed-loader

Create a seperate file

// composer autoloading
require 'vendor/autoload.php';

//since composer is always needed, exclude all classes loaded until here
$classesNoLoad = array_merge(get_declared_interfaces(), get_declared_traits(), get_declared_classes());

//execute your app part you want to cache
$app = MyApplication::init();

//find all loaded files until here
$classes = array_merge(get_declared_interfaces(), get_declared_traits(), get_declared_classes());
//remove the classes loaded by composer
$classes = array_diff($classes, $classesNoLoad);

//cache it now
$cache = new SpeedLoader\BuildCache();
$cache->setClasses($classes);
//$cache->setNewLine("\n");
//$cache->setCompressionLevel(SpeedLoader\BuildClass::COMPRESS_HIGH);

file_put_contents('data/cache/classes.php.cache', '<?php ' . "\n" . $cache->getCachedString());

Add the cache to your application

if (file_exists('data/cache/classes.php.cache')) {
    require_once 'data/cache/classes.php.cache';
}

Is this something new?

No it's nothing completely new. There are a couple of solutions around, but all of them are having some problems, that's why i "reinvited the wheel"., (*4)

Features

  • independent
    • can be used with all packages or frameworks around
    • or only with your code
  • different compression modes
    • compression for production
    • normal for development
  • save the cache how you want
    • you get the complete cache as a string
    • e.g. save it as a file or memory

Example

Generate

Just execute the part of your application you want to cache. Dont do this in your normal process - warm up your cache with a cronjob or similar, (*5)

// composer autoloading
require 'vendor/autoload.php';

//since composer is always needed, exclude all classes loaded until here
$classesNoLoad = array_merge(get_declared_interfaces(), get_declared_traits(), get_declared_classes());

//execute your app...(only the bootstrap)
$app = Zend\Mvc\Application::init($appConfig);

//find all loaded files
$classes = array_merge(get_declared_interfaces(), get_declared_traits(), get_declared_classes());
$classes = array_diff($classes, $classesNoLoad);

//cache it
$cache = new SpeedLoader\BuildCache();
$cache->setClasses($classes);
//$cache->setNewLine("\n");
//$cache->setCompressionLevel(SpeedLoader\BuildClass::COMPRESS_HIGH);

file_put_contents('data/cache/classes.php.cache', '<?php ' . "\n" . $cache->getCachedString());

Include the file in your final application

if (file_exists('data/cache/classes.php.cache')) {
    require_once 'data/cache/classes.php.cache';
}

Why concat classes

Finding and opening a lot of files on the filesystem is expensive. (similar reason why you should combine JS or CSS files...but there its HTTP), (*6)

Alternatives

EdpSuperluminal - not maintained - only for ZF2 - only compressed possible, (*7)

Symfony - only output direct to a file - no compression possible, (*8)

ClassPreloader - class hierarchy can be wrong (e.g. a class requires an interface and the interface comes later in the file...but in the meantime autoloader have loaded the interface -> "cannot redeclare error"), (*9)

Benchmarks

http://stackoverflow.com/questions/8240726/are-there-performance-downsides-while-using-autoloading-classes-in-php https://mwop.net/blog/245-Autoloading-Benchmarks.html, (*10)

The Versions

13/04 2015

dev-master

9999999-dev https://github.com/ThaDafinser/SpeedLoader

Load classes faster

  Sources   Download

MIT

The Requires

 

The Development Requires

by Martin Keckeis

performance speed classmap

13/04 2015

v1.0.0

1.0.0.0 https://github.com/ThaDafinser/SpeedLoader

Load classes faster

  Sources   Download

MIT

The Requires

 

The Development Requires

by Martin Keckeis

performance speed classmap