From e15018ce2a90abf2242ce2e839d1df59ceab0356 Mon Sep 17 00:00:00 2001 From: snapshotpl Date: Wed, 13 Jan 2016 00:24:04 +0100 Subject: [PATCH 01/50] Perfomance optimalization --- benchmarks/BenchAsset/AbstractFactoryFoo.php | 11 +++--- benchmarks/FetchServiceManager.php | 39 ++++++++++++++++++++ src/ServiceManager.php | 28 +++++++++----- 3 files changed, 64 insertions(+), 14 deletions(-) create mode 100644 benchmarks/FetchServiceManager.php diff --git a/benchmarks/BenchAsset/AbstractFactoryFoo.php b/benchmarks/BenchAsset/AbstractFactoryFoo.php index f72476fd..ba592840 100644 --- a/benchmarks/BenchAsset/AbstractFactoryFoo.php +++ b/benchmarks/BenchAsset/AbstractFactoryFoo.php @@ -6,11 +6,6 @@ class AbstractFactoryFoo implements AbstractFactoryInterface { - public function canCreateServiceWithName(ContainerInterface $container, $requestedName) - { - return ($requestedName === 'foo'); - } - public function __invoke(ContainerInterface $container, $requestedName, array $options = null) { if ($requestedName === 'foo') { @@ -18,4 +13,10 @@ public function __invoke(ContainerInterface $container, $requestedName, array $o } return false; } + + public function canCreate(ContainerInterface $container, $requestedName) + { + return ($requestedName === 'foo'); + } + } diff --git a/benchmarks/FetchServiceManager.php b/benchmarks/FetchServiceManager.php new file mode 100644 index 00000000..bf53cb3a --- /dev/null +++ b/benchmarks/FetchServiceManager.php @@ -0,0 +1,39 @@ +config = $this->getConfig(); + } + + /** + * @iterations 500 + */ + public function fetchServiceManagerCreation() + { + $retult = new ServiceManager($this->config); + } +} diff --git a/src/ServiceManager.php b/src/ServiceManager.php index 75c43fab..0fef5d82 100644 --- a/src/ServiceManager.php +++ b/src/ServiceManager.php @@ -126,6 +126,13 @@ class ServiceManager implements ServiceLocatorInterface */ protected $sharedByDefault = true; + /** + * Service manager was already configured? + * + * @var bool + */ + protected $configured = false; + /** * Constructor. * @@ -164,7 +171,6 @@ public function getServiceLocator() public function get($name) { $requestedName = $name; - $name = isset($this->resolvedAliases[$name]) ? $this->resolvedAliases[$name] : $name; // We start by checking if we have cached the requested service (this // is the fastest method). @@ -172,6 +178,8 @@ public function get($name) return $this->services[$requestedName]; } + $name = isset($this->resolvedAliases[$name]) ? $this->resolvedAliases[$name] : $name; + // Next, if the alias should be shared, and we have cached the resolved // service, use it. if ($requestedName !== $name @@ -210,7 +218,7 @@ public function get($name) public function build($name, array $options = null) { // We never cache when using "build" - $name = isset($this->resolvedAliases[$name]) ? $this->resolvedAliases[$name] : $name; + $name = isset($this->resolvedAliases[$name]) ? $this->resolvedAliases[$name] : $name; return $this->doCreate($name, $options); } @@ -333,6 +341,7 @@ public function configure(array $config) if (isset($config['aliases'])) { $this->aliases = $config['aliases'] + $this->aliases; + $this->resolveAliases($this->aliases); } if (isset($config['shared_by_default'])) { @@ -356,7 +365,7 @@ public function configure(array $config) $this->resolveInitializers($config['initializers']); } - $this->resolveAliases(); + $this->configured = true; return $this; } @@ -411,7 +420,7 @@ public function mapLazyService($name, $class = null) /** * Add an abstract factory for resolving services. * - * @param string|Factory\AbstractFactoryInterface $name Service name + * @param string|Factory\AbstractFactoryInterface $factory Service name */ public function addAbstractFactory($factory) { @@ -433,7 +442,7 @@ public function addDelegator($name, $factory) /** * Add an initializer. * - * @param string|callable|InitializerInterface $initalizer + * @param string|callable|InitializerInterface $initializer */ public function addInitializer($initializer) { @@ -575,9 +584,9 @@ private function resolveAlias($alias) /** * Resolve all aliases to their canonical service names. */ - private function resolveAliases() + private function resolveAliases(array $aliases) { - foreach ($this->aliases as $alias => $service) { + foreach ($aliases as $alias => $service) { $this->resolvedAliases[$alias] = $this->resolveAlias($alias); } } @@ -804,7 +813,8 @@ private function createFactoriesForInvokables(array $invokables) /** * Determine if one or more services already exist in the container. * - * If the allow override flag is true, this method does nothing. + * If the allow override flag is true or it's first time configured, + * this method does nothing. * * Otherwise, it checks against each of the following service types, * if present, and validates that none are defining services that @@ -817,7 +827,7 @@ private function createFactoriesForInvokables(array $invokables) */ private function validateOverrides(array $config) { - if ($this->allowOverride) { + if ($this->allowOverride || !$this->configured) { return; } From c7fbb0c94f9aabc61531434290514ab98e9d94ba Mon Sep 17 00:00:00 2001 From: snapshotpl Date: Wed, 13 Jan 2016 00:39:43 +0100 Subject: [PATCH 02/50] Add missing stdlib --- composer.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 1261f5e9..ef3f8740 100644 --- a/composer.json +++ b/composer.json @@ -15,7 +15,8 @@ }, "require": { "php": "^5.5 || ^7.0", - "container-interop/container-interop": "~1.0" + "container-interop/container-interop": "~1.0", + "zendframework/zend-stdlib": "^2.4" }, "require-dev": { "phpunit/phpunit": "~4.6", From 3282a84a69be2637d00c7259234adf6a8932016c Mon Sep 17 00:00:00 2001 From: snapshotpl Date: Wed, 13 Jan 2016 18:46:00 +0100 Subject: [PATCH 03/50] Remove stdlib require in composer --- composer.json | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/composer.json b/composer.json index ef3f8740..1261f5e9 100644 --- a/composer.json +++ b/composer.json @@ -15,8 +15,7 @@ }, "require": { "php": "^5.5 || ^7.0", - "container-interop/container-interop": "~1.0", - "zendframework/zend-stdlib": "^2.4" + "container-interop/container-interop": "~1.0" }, "require-dev": { "phpunit/phpunit": "~4.6", From dde689f16efc39c5dbcc36631038820a34d995d0 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Sun, 24 Jan 2016 16:53:54 +0100 Subject: [PATCH 04/50] Switching to phpbench/phpbench based benchmarks --- README.md | 4 +- benchmarks/FetchServiceManager.php | 39 -------------- benchmarks/FetchServiceManagerBench.php | 43 ++++++++++++++++ ...tchServices.php => FetchServicesBench.php} | 51 +++++++++++-------- composer.json | 2 +- phpbench.json | 5 ++ 6 files changed, 81 insertions(+), 63 deletions(-) delete mode 100644 benchmarks/FetchServiceManager.php create mode 100644 benchmarks/FetchServiceManagerBench.php rename benchmarks/{FetchServices.php => FetchServicesBench.php} (57%) create mode 100644 phpbench.json diff --git a/README.md b/README.md index 9e48dfc1..50ab43af 100644 --- a/README.md +++ b/README.md @@ -18,11 +18,11 @@ retrieving other objects. ## Benchmarks We provide scripts for benchmarking zend-servicemanager using the -[Athletic](https://github.com/polyfractal/athletic) framework; these can be +[PHPBench](https://github.com/phpbench/phpbench) framework; these can be found in the `benchmarks/` directory. To execute the benchmarks you can run the following command: ```bash -$ vendor/bin/athletic -p benchmarks +$ vendor/bin/phpbench --report=aggregate ``` diff --git a/benchmarks/FetchServiceManager.php b/benchmarks/FetchServiceManager.php deleted file mode 100644 index bf53cb3a..00000000 --- a/benchmarks/FetchServiceManager.php +++ /dev/null @@ -1,39 +0,0 @@ -config = $this->getConfig(); - } - - /** - * @iterations 500 - */ - public function fetchServiceManagerCreation() - { - $retult = new ServiceManager($this->config); - } -} diff --git a/benchmarks/FetchServiceManagerBench.php b/benchmarks/FetchServiceManagerBench.php new file mode 100644 index 00000000..33211466 --- /dev/null +++ b/benchmarks/FetchServiceManagerBench.php @@ -0,0 +1,43 @@ +config = $config; + } + + /** + * @Revs(1000) + * @Iterations(20) + */ + public function benchFetchServiceManagerCreation() + { + $result = new ServiceManager($this->config); + } +} diff --git a/benchmarks/FetchServices.php b/benchmarks/FetchServicesBench.php similarity index 57% rename from benchmarks/FetchServices.php rename to benchmarks/FetchServicesBench.php index 9f86289e..8d453697 100644 --- a/benchmarks/FetchServices.php +++ b/benchmarks/FetchServicesBench.php @@ -2,42 +2,47 @@ namespace ZendBench\ServiceManager; -use Athletic\AthleticEvent; +use PhpBench\Benchmark\Metadata\Annotations\BeforeMethods; +use PhpBench\Benchmark\Metadata\Annotations\Iterations; +use PhpBench\Benchmark\Metadata\Annotations\Revs; use Zend\ServiceManager\ServiceManager; -class FetchServices extends AthleticEvent +/** + * @BeforeMethods({"initServiceManager"}) + */ +class FetchServicesBench { const NUM_SERVICES = 1000; /** * @var ServiceManager */ - protected $sm; + private $sm; - protected function getConfig() + public function initServiceManager() { - $config = []; + $config = []; + $service = new \stdClass(); + for ($i = 0; $i <= self::NUM_SERVICES; $i++) { $config['factories']["factory_$i"] = BenchAsset\FactoryFoo::class; $config['invokables']["invokable_$i"] = BenchAsset\Foo::class; - $config['services']["service_$i"] = $this; + $config['services']["service_$i"] = $service; $config['aliases']["alias_$i"] = "service_$i"; } + $config['abstract_factories'] = [ BenchAsset\AbstractFactoryFoo::class ]; - return $config; - } - public function classSetUp() - { - $this->sm = new ServiceManager($this->getConfig()); + $this->sm = new ServiceManager($config); } /** * Fetch the factory services * - * @iterations 5000 + * @Revs(1000) + * @Iterations(20) */ - public function fetchFactoryService() + public function benchFetchFactoryService() { $result = $this->sm->get('factory_' . rand(0, self::NUM_SERVICES)); } @@ -45,9 +50,10 @@ public function fetchFactoryService() /** * Fetch the invokable services * - * @iterations 5000 + * @Revs(1000) + * @Iterations(20) */ - public function fetchInvokableService() + public function benchFetchInvokableService() { $result = $this->sm->get('invokable_' . rand(0, self::NUM_SERVICES)); } @@ -55,9 +61,10 @@ public function fetchInvokableService() /** * Fetch the services * - * @iterations 5000 + * @Revs(1000) + * @Iterations(20) */ - public function fetchService() + public function benchFetchService() { $result = $this->sm->get('service_' . rand(0, self::NUM_SERVICES)); } @@ -65,9 +72,10 @@ public function fetchService() /** * Fetch the alias services * - * @iterations 5000 + * @Revs(1000) + * @Iterations(20) */ - public function fetchAliasService() + public function benchFetchAliasService() { $result = $this->sm->get('alias_' . rand(0, self::NUM_SERVICES)); } @@ -75,9 +83,10 @@ public function fetchAliasService() /** * Fetch the abstract factory services * - * @iterations 5000 + * @Revs(1000) + * @Iterations(20) */ - public function fetchAbstractFactoryService() + public function benchFetchAbstractFactoryService() { $result = $this->sm->get('foo'); } diff --git a/composer.json b/composer.json index 1261f5e9..65e7a7d8 100644 --- a/composer.json +++ b/composer.json @@ -21,7 +21,7 @@ "phpunit/phpunit": "~4.6", "ocramius/proxy-manager": "~1.0", "squizlabs/php_codesniffer": "^2.0@dev", - "athletic/athletic": "dev-master" + "phpbench/phpbench": "^0.10.0" }, "suggest": { "ocramius/proxy-manager": "ProxyManager 1.* to handle lazy initialization of services", diff --git a/phpbench.json b/phpbench.json new file mode 100644 index 00000000..f782945a --- /dev/null +++ b/phpbench.json @@ -0,0 +1,5 @@ +{ + "bootstrap": "vendor/autoload.php", + "path": "benchmarks", + "retry_threshold": 5 +} From c78a744da31cfd7485620b95de28c964131b62fb Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Sun, 24 Jan 2016 17:56:29 +0100 Subject: [PATCH 05/50] Rewrote tests to be time-constant, and not `rand()` based --- benchmarks/FetchCachedServicesBench.php | 140 ++++++++++++++++++ ...ch.php => FetchNewServiceManagerBench.php} | 30 ++-- benchmarks/FetchNewServicesBench.php | 88 +++++++++++ benchmarks/FetchServicesBench.php | 93 ------------ 4 files changed, 247 insertions(+), 104 deletions(-) create mode 100644 benchmarks/FetchCachedServicesBench.php rename benchmarks/{FetchServiceManagerBench.php => FetchNewServiceManagerBench.php} (59%) create mode 100644 benchmarks/FetchNewServicesBench.php delete mode 100644 benchmarks/FetchServicesBench.php diff --git a/benchmarks/FetchCachedServicesBench.php b/benchmarks/FetchCachedServicesBench.php new file mode 100644 index 00000000..9d4383d0 --- /dev/null +++ b/benchmarks/FetchCachedServicesBench.php @@ -0,0 +1,140 @@ +sm = new ServiceManager([ + 'factories' => [ + 'factory1' => BenchAsset\FactoryFoo::class, + ], + 'invokables' => [ + 'invokable1' => BenchAsset\Foo::class, + ], + 'services' => [ + 'service1' => new \stdClass(), + ], + 'aliases' => [ + 'alias1' => 'service1', + 'recursiveAlias1' => 'alias1', + 'recursiveAlias2' => 'recursiveAlias1', + ], + 'abstract_factories' => [ + BenchAsset\AbstractFactoryFoo::class + ] + ]); + + // forcing initialization of all the services + $this->sm->get('factory1'); + $this->sm->get('invokable1'); + $this->sm->get('service1'); + $this->sm->get('alias1'); + $this->sm->get('recursiveAlias1'); + $this->sm->get('recursiveAlias2'); + } + + /** + * Fetch the factory services + * + * @Revs(1000) + * @Iterations(20) + * @Warmup(2) + */ + public function benchFetchFactory1() + { + // @todo workaround until phpbench provides initialization around each loop, excluded from measurement + $sm = clone $this->sm; + + $sm->get('factory1'); + } + + /** + * @Revs(1000) + * @Iterations(20) + * @Warmup(2) + */ + public function benchFetchInvokable1() + { + // @todo workaround until phpbench provides initialization around each loop, excluded from measurement + $sm = clone $this->sm; + + $sm->get('invokable1'); + } + + /** + * @Revs(1000) + * @Iterations(20) + * @Warmup(2) + */ + public function benchFetchService1() + { + // @todo workaround until phpbench provides initialization around each loop, excluded from measurement + $sm = clone $this->sm; + + $sm->get('service1'); + } + + /** + * @Revs(1000) + * @Iterations(20) + * @Warmup(2) + */ + public function benchFetchAlias1() + { + // @todo workaround until phpbench provides initialization around each loop, excluded from measurement + $sm = clone $this->sm; + + $sm->get('alias1'); + } + + /** + * @Revs(1000) + * @Iterations(20) + * @Warmup(2) + */ + public function benchFetchRecursiveAlias1() + { + // @todo workaround until phpbench provides initialization around each loop, excluded from measurement + $sm = clone $this->sm; + + $sm->get('recursiveAlias1'); + } + + /** + * @Revs(1000) + * @Iterations(20) + * @Warmup(2) + */ + public function benchFetchRecursiveAlias2() + { + // @todo workaround until phpbench provides initialization around each loop, excluded from measurement + $sm = clone $this->sm; + + $sm->get('recursiveAlias2'); + } + + /** + * @Revs(1000) + * @Iterations(20) + * @Warmup(2) + */ + public function benchFetchAbstractFactoryService() + { + // @todo workaround until phpbench provides initialization around each loop, excluded from measurement + $sm = clone $this->sm; + + $sm->get('foo'); + } +} diff --git a/benchmarks/FetchServiceManagerBench.php b/benchmarks/FetchNewServiceManagerBench.php similarity index 59% rename from benchmarks/FetchServiceManagerBench.php rename to benchmarks/FetchNewServiceManagerBench.php index 33211466..7f598fdd 100644 --- a/benchmarks/FetchServiceManagerBench.php +++ b/benchmarks/FetchNewServiceManagerBench.php @@ -4,20 +4,30 @@ use PhpBench\Benchmark\Metadata\Annotations\Iterations; use PhpBench\Benchmark\Metadata\Annotations\Revs; +use PhpBench\Benchmark\Metadata\Annotations\Warmup; use Zend\ServiceManager\ServiceManager; -/** - * @BeforeMethods({"initConfig"}) - */ -class FetchServiceManagerBench +class FetchNewServiceManagerBench { const NUM_SERVICES = 1000; + /** + * @var array + */ private $config = []; - public function initConfig() + public function __construct() { - $config = []; + $config = [ + 'factories' => [], + 'invokables' => [], + 'services' => [], + 'aliases' => [], + 'abstract_factories' => [ + BenchAsset\AbstractFactoryFoo::class, + ], + ]; + $service = new \stdClass(); for ($i = 0; $i <= self::NUM_SERVICES; $i++) { @@ -26,18 +36,16 @@ public function initConfig() $config['services']["service_$i"] = $service; $config['aliases']["alias_$i"] = "service_$i"; } - - $config['abstract_factories'] = [ BenchAsset\AbstractFactoryFoo::class ]; - $this->config = $config; } /** - * @Revs(1000) + * @Revs(100) * @Iterations(20) + * @Warmup(2) */ public function benchFetchServiceManagerCreation() { - $result = new ServiceManager($this->config); + new ServiceManager($this->config); } } diff --git a/benchmarks/FetchNewServicesBench.php b/benchmarks/FetchNewServicesBench.php new file mode 100644 index 00000000..6e1d5469 --- /dev/null +++ b/benchmarks/FetchNewServicesBench.php @@ -0,0 +1,88 @@ +sm = new ServiceManager([ + 'factories' => [ + 'factory1' => BenchAsset\FactoryFoo::class, + ], + 'invokables' => [ + 'invokable1' => BenchAsset\Foo::class, + ], + 'services' => [ + 'service1' => new \stdClass(), + ], + 'aliases' => [ + 'alias1' => 'service1', + 'recursiveAlias1' => 'alias1', + 'recursiveAlias2' => 'recursiveAlias1', + ], + ]); + } + + /** + * @Revs(1000) + * @Iterations(10) + * @Warmup(2) + */ + public function benchFetchFactory1() + { + // @todo workaround until phpbench provides initialization around each loop, excluded from measurement + $sm = clone $this->sm; + + $sm->get('factory1'); + } + + /** + * @Revs(1000) + * @Iterations(10) + * @Warmup(2) + */ + public function benchBuildFactory1() + { + // @todo workaround until phpbench provides initialization around each loop, excluded from measurement + $sm = clone $this->sm; + + $sm->build('factory1'); + } + + /** + * @Revs(1000) + * @Iterations(10) + * @Warmup(2) + */ + public function benchFetchInvokable1() + { + // @todo workaround until phpbench provides initialization around each loop, excluded from measurement + $sm = clone $this->sm; + + $sm->get('invokable1'); + } + + /** + * @Revs(1000) + * @Iterations(10) + * @Warmup(2) + */ + public function benchBuildInvokable1() + { + // @todo workaround until phpbench provides initialization around each loop, excluded from measurement + $sm = clone $this->sm; + + $sm->build('invokable1'); + } +} diff --git a/benchmarks/FetchServicesBench.php b/benchmarks/FetchServicesBench.php deleted file mode 100644 index 8d453697..00000000 --- a/benchmarks/FetchServicesBench.php +++ /dev/null @@ -1,93 +0,0 @@ -sm = new ServiceManager($config); - } - - /** - * Fetch the factory services - * - * @Revs(1000) - * @Iterations(20) - */ - public function benchFetchFactoryService() - { - $result = $this->sm->get('factory_' . rand(0, self::NUM_SERVICES)); - } - - /** - * Fetch the invokable services - * - * @Revs(1000) - * @Iterations(20) - */ - public function benchFetchInvokableService() - { - $result = $this->sm->get('invokable_' . rand(0, self::NUM_SERVICES)); - } - - /** - * Fetch the services - * - * @Revs(1000) - * @Iterations(20) - */ - public function benchFetchService() - { - $result = $this->sm->get('service_' . rand(0, self::NUM_SERVICES)); - } - - /** - * Fetch the alias services - * - * @Revs(1000) - * @Iterations(20) - */ - public function benchFetchAliasService() - { - $result = $this->sm->get('alias_' . rand(0, self::NUM_SERVICES)); - } - - /** - * Fetch the abstract factory services - * - * @Revs(1000) - * @Iterations(20) - */ - public function benchFetchAbstractFactoryService() - { - $result = $this->sm->get('foo'); - } -} From 177850e34bd9ecf32dab8d1987ea4674a3eafdfa Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Sun, 24 Jan 2016 18:02:14 +0100 Subject: [PATCH 06/50] Adding benchmarks for fetching aliases --- benchmarks/FetchNewServicesBench.php | 100 ++++++++++++++++++++++++++- 1 file changed, 97 insertions(+), 3 deletions(-) diff --git a/benchmarks/FetchNewServicesBench.php b/benchmarks/FetchNewServicesBench.php index 6e1d5469..b0a68b64 100644 --- a/benchmarks/FetchNewServicesBench.php +++ b/benchmarks/FetchNewServicesBench.php @@ -27,9 +27,12 @@ public function __construct() 'service1' => new \stdClass(), ], 'aliases' => [ - 'alias1' => 'service1', - 'recursiveAlias1' => 'alias1', - 'recursiveAlias2' => 'recursiveAlias1', + 'factoryAlias1' => 'factory1', + 'recursiveFactoryAlias1' => 'factoryAlias1', + 'recursiveFactoryAlias2' => 'recursiveFactoryAlias1', + ], + 'abstract_factories' => [ + BenchAsset\AbstractFactoryFoo::class ], ]); } @@ -85,4 +88,95 @@ public function benchBuildInvokable1() $sm->build('invokable1'); } + + /** + * @Revs(1000) + * @Iterations(10) + * @Warmup(2) + */ + public function benchFetchService1() + { + // @todo workaround until phpbench provides initialization around each loop, excluded from measurement + $sm = clone $this->sm; + + $sm->get('service1'); + } + + /** + * @Revs(1000) + * @Iterations(10) + * @Warmup(2) + */ + public function benchFetchFactoryAlias1() + { + // @todo workaround until phpbench provides initialization around each loop, excluded from measurement + $sm = clone $this->sm; + + $sm->build('factoryAlias1'); + } + + /** + * @Revs(1000) + * @Iterations(10) + * @Warmup(2) + */ + public function benchBuildFactoryAlias1() + { + // @todo workaround until phpbench provides initialization around each loop, excluded from measurement + $sm = clone $this->sm; + + $sm->build('factoryAlias1'); + } + + /** + * @Revs(1000) + * @Iterations(10) + * @Warmup(2) + */ + public function benchFetchRecursiveFactoryAlias1() + { + // @todo workaround until phpbench provides initialization around each loop, excluded from measurement + $sm = clone $this->sm; + + $sm->build('recursiveFactoryAlias1'); + } + + /** + * @Revs(1000) + * @Iterations(10) + * @Warmup(2) + */ + public function benchBuildRecursiveFactoryAlias1() + { + // @todo workaround until phpbench provides initialization around each loop, excluded from measurement + $sm = clone $this->sm; + + $sm->build('recursiveFactoryAlias1'); + } + + /** + * @Revs(1000) + * @Iterations(10) + * @Warmup(2) + */ + public function benchFetchRecursiveFactoryAlias2() + { + // @todo workaround until phpbench provides initialization around each loop, excluded from measurement + $sm = clone $this->sm; + + $sm->build('recursiveFactoryAlias2'); + } + + /** + * @Revs(1000) + * @Iterations(10) + * @Warmup(2) + */ + public function benchBuildRecursiveFactoryAlias2() + { + // @todo workaround until phpbench provides initialization around each loop, excluded from measurement + $sm = clone $this->sm; + + $sm->build('recursiveFactoryAlias2'); + } } From 047001320f64e1f0518aa01205f8218e2cce6a5e Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Sun, 24 Jan 2016 18:03:20 +0100 Subject: [PATCH 07/50] Adding benchmarks for fetching services from abstract factories --- benchmarks/FetchNewServicesBench.php | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/benchmarks/FetchNewServicesBench.php b/benchmarks/FetchNewServicesBench.php index b0a68b64..b1cef989 100644 --- a/benchmarks/FetchNewServicesBench.php +++ b/benchmarks/FetchNewServicesBench.php @@ -179,4 +179,30 @@ public function benchBuildRecursiveFactoryAlias2() $sm->build('recursiveFactoryAlias2'); } + + /** + * @Revs(1000) + * @Iterations(10) + * @Warmup(2) + */ + public function benchFetchAbstractFactoryFoo() + { + // @todo workaround until phpbench provides initialization around each loop, excluded from measurement + $sm = clone $this->sm; + + $sm->get('foo'); + } + + /** + * @Revs(1000) + * @Iterations(10) + * @Warmup(2) + */ + public function benchBuildAbstractFactoryFoo() + { + // @todo workaround until phpbench provides initialization around each loop, excluded from measurement + $sm = clone $this->sm; + + $sm->build('foo'); + } } From 890238ec631df8c1a268f4d500e082fbeaa41221 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Sun, 24 Jan 2016 18:09:20 +0100 Subject: [PATCH 08/50] Correcting benchmark `run` CLI command in the README --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 50ab43af..b2ed9afc 100644 --- a/README.md +++ b/README.md @@ -24,5 +24,5 @@ found in the `benchmarks/` directory. To execute the benchmarks you can run the following command: ```bash -$ vendor/bin/phpbench --report=aggregate +$ vendor/bin/phpbench run --report=aggregate ``` From e155e62ac2a292fcec5d28d635dde39aa5e76f58 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Sun, 24 Jan 2016 18:26:27 +0100 Subject: [PATCH 09/50] Performance tweak: inlined `resolveAlias` --- src/ServiceManager.php | 26 +++++++------------------- 1 file changed, 7 insertions(+), 19 deletions(-) diff --git a/src/ServiceManager.php b/src/ServiceManager.php index 0fef5d82..f6d4d97d 100644 --- a/src/ServiceManager.php +++ b/src/ServiceManager.php @@ -563,31 +563,19 @@ private function resolveInitializers(array $initializers) } } - /** - * Recursively resolve an alias name to a service name - * - * @param string $alias - * @return string - */ - private function resolveAlias($alias) - { - $name = $alias; - - do { - $canBeResolved = isset($this->aliases[$name]); - $name = $canBeResolved ? $this->aliases[$name] : $name; - } while ($canBeResolved); - - return $name; - } - /** * Resolve all aliases to their canonical service names. */ private function resolveAliases(array $aliases) { foreach ($aliases as $alias => $service) { - $this->resolvedAliases[$alias] = $this->resolveAlias($alias); + $name = $alias; + + while (isset($this->aliases[$name])) { + $name = $this->aliases[$name]; + } + + $this->resolvedAliases[$alias] = $name; } } From 45ce3e2d9eb2a7273be3537dd34836e5b4a487ac Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Sun, 24 Jan 2016 18:35:58 +0100 Subject: [PATCH 10/50] Linking related phpbench issue --- benchmarks/FetchCachedServicesBench.php | 14 ++++++------- benchmarks/FetchNewServicesBench.php | 26 ++++++++++++------------- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/benchmarks/FetchCachedServicesBench.php b/benchmarks/FetchCachedServicesBench.php index 9d4383d0..8c4f5a43 100644 --- a/benchmarks/FetchCachedServicesBench.php +++ b/benchmarks/FetchCachedServicesBench.php @@ -54,7 +54,7 @@ public function __construct() */ public function benchFetchFactory1() { - // @todo workaround until phpbench provides initialization around each loop, excluded from measurement + // @todo @link https://github.com/phpbench/phpbench/issues/304 $sm = clone $this->sm; $sm->get('factory1'); @@ -67,7 +67,7 @@ public function benchFetchFactory1() */ public function benchFetchInvokable1() { - // @todo workaround until phpbench provides initialization around each loop, excluded from measurement + // @todo @link https://github.com/phpbench/phpbench/issues/304 $sm = clone $this->sm; $sm->get('invokable1'); @@ -80,7 +80,7 @@ public function benchFetchInvokable1() */ public function benchFetchService1() { - // @todo workaround until phpbench provides initialization around each loop, excluded from measurement + // @todo @link https://github.com/phpbench/phpbench/issues/304 $sm = clone $this->sm; $sm->get('service1'); @@ -93,7 +93,7 @@ public function benchFetchService1() */ public function benchFetchAlias1() { - // @todo workaround until phpbench provides initialization around each loop, excluded from measurement + // @todo @link https://github.com/phpbench/phpbench/issues/304 $sm = clone $this->sm; $sm->get('alias1'); @@ -106,7 +106,7 @@ public function benchFetchAlias1() */ public function benchFetchRecursiveAlias1() { - // @todo workaround until phpbench provides initialization around each loop, excluded from measurement + // @todo @link https://github.com/phpbench/phpbench/issues/304 $sm = clone $this->sm; $sm->get('recursiveAlias1'); @@ -119,7 +119,7 @@ public function benchFetchRecursiveAlias1() */ public function benchFetchRecursiveAlias2() { - // @todo workaround until phpbench provides initialization around each loop, excluded from measurement + // @todo @link https://github.com/phpbench/phpbench/issues/304 $sm = clone $this->sm; $sm->get('recursiveAlias2'); @@ -132,7 +132,7 @@ public function benchFetchRecursiveAlias2() */ public function benchFetchAbstractFactoryService() { - // @todo workaround until phpbench provides initialization around each loop, excluded from measurement + // @todo @link https://github.com/phpbench/phpbench/issues/304 $sm = clone $this->sm; $sm->get('foo'); diff --git a/benchmarks/FetchNewServicesBench.php b/benchmarks/FetchNewServicesBench.php index b1cef989..e0d50f12 100644 --- a/benchmarks/FetchNewServicesBench.php +++ b/benchmarks/FetchNewServicesBench.php @@ -44,7 +44,7 @@ public function __construct() */ public function benchFetchFactory1() { - // @todo workaround until phpbench provides initialization around each loop, excluded from measurement + // @todo @link https://github.com/phpbench/phpbench/issues/304 $sm = clone $this->sm; $sm->get('factory1'); @@ -57,7 +57,7 @@ public function benchFetchFactory1() */ public function benchBuildFactory1() { - // @todo workaround until phpbench provides initialization around each loop, excluded from measurement + // @todo @link https://github.com/phpbench/phpbench/issues/304 $sm = clone $this->sm; $sm->build('factory1'); @@ -70,7 +70,7 @@ public function benchBuildFactory1() */ public function benchFetchInvokable1() { - // @todo workaround until phpbench provides initialization around each loop, excluded from measurement + // @todo @link https://github.com/phpbench/phpbench/issues/304 $sm = clone $this->sm; $sm->get('invokable1'); @@ -83,7 +83,7 @@ public function benchFetchInvokable1() */ public function benchBuildInvokable1() { - // @todo workaround until phpbench provides initialization around each loop, excluded from measurement + // @todo @link https://github.com/phpbench/phpbench/issues/304 $sm = clone $this->sm; $sm->build('invokable1'); @@ -96,7 +96,7 @@ public function benchBuildInvokable1() */ public function benchFetchService1() { - // @todo workaround until phpbench provides initialization around each loop, excluded from measurement + // @todo @link https://github.com/phpbench/phpbench/issues/304 $sm = clone $this->sm; $sm->get('service1'); @@ -109,7 +109,7 @@ public function benchFetchService1() */ public function benchFetchFactoryAlias1() { - // @todo workaround until phpbench provides initialization around each loop, excluded from measurement + // @todo @link https://github.com/phpbench/phpbench/issues/304 $sm = clone $this->sm; $sm->build('factoryAlias1'); @@ -122,7 +122,7 @@ public function benchFetchFactoryAlias1() */ public function benchBuildFactoryAlias1() { - // @todo workaround until phpbench provides initialization around each loop, excluded from measurement + // @todo @link https://github.com/phpbench/phpbench/issues/304 $sm = clone $this->sm; $sm->build('factoryAlias1'); @@ -135,7 +135,7 @@ public function benchBuildFactoryAlias1() */ public function benchFetchRecursiveFactoryAlias1() { - // @todo workaround until phpbench provides initialization around each loop, excluded from measurement + // @todo @link https://github.com/phpbench/phpbench/issues/304 $sm = clone $this->sm; $sm->build('recursiveFactoryAlias1'); @@ -148,7 +148,7 @@ public function benchFetchRecursiveFactoryAlias1() */ public function benchBuildRecursiveFactoryAlias1() { - // @todo workaround until phpbench provides initialization around each loop, excluded from measurement + // @todo @link https://github.com/phpbench/phpbench/issues/304 $sm = clone $this->sm; $sm->build('recursiveFactoryAlias1'); @@ -161,7 +161,7 @@ public function benchBuildRecursiveFactoryAlias1() */ public function benchFetchRecursiveFactoryAlias2() { - // @todo workaround until phpbench provides initialization around each loop, excluded from measurement + // @todo @link https://github.com/phpbench/phpbench/issues/304 $sm = clone $this->sm; $sm->build('recursiveFactoryAlias2'); @@ -174,7 +174,7 @@ public function benchFetchRecursiveFactoryAlias2() */ public function benchBuildRecursiveFactoryAlias2() { - // @todo workaround until phpbench provides initialization around each loop, excluded from measurement + // @todo @link https://github.com/phpbench/phpbench/issues/304 $sm = clone $this->sm; $sm->build('recursiveFactoryAlias2'); @@ -187,7 +187,7 @@ public function benchBuildRecursiveFactoryAlias2() */ public function benchFetchAbstractFactoryFoo() { - // @todo workaround until phpbench provides initialization around each loop, excluded from measurement + // @todo @link https://github.com/phpbench/phpbench/issues/304 $sm = clone $this->sm; $sm->get('foo'); @@ -200,7 +200,7 @@ public function benchFetchAbstractFactoryFoo() */ public function benchBuildAbstractFactoryFoo() { - // @todo workaround until phpbench provides initialization around each loop, excluded from measurement + // @todo @link https://github.com/phpbench/phpbench/issues/304 $sm = clone $this->sm; $sm->build('foo'); From 8ad06a886673157d3af486075897d6389834d7e0 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Sun, 24 Jan 2016 18:48:32 +0100 Subject: [PATCH 11/50] Moved annotations to class level (less redundancy) --- benchmarks/FetchCachedServicesBench.php | 42 ++----------- benchmarks/FetchNewServiceManagerBench.php | 10 ++-- benchmarks/FetchNewServicesBench.php | 70 ++-------------------- 3 files changed, 15 insertions(+), 107 deletions(-) diff --git a/benchmarks/FetchCachedServicesBench.php b/benchmarks/FetchCachedServicesBench.php index 8c4f5a43..c10f2929 100644 --- a/benchmarks/FetchCachedServicesBench.php +++ b/benchmarks/FetchCachedServicesBench.php @@ -7,6 +7,11 @@ use PhpBench\Benchmark\Metadata\Annotations\Warmup; use Zend\ServiceManager\ServiceManager; +/** + * @Revs(1000) + * @Iterations(20) + * @Warmup(2) + */ class FetchCachedServicesBench { /** @@ -45,13 +50,6 @@ public function __construct() $this->sm->get('recursiveAlias2'); } - /** - * Fetch the factory services - * - * @Revs(1000) - * @Iterations(20) - * @Warmup(2) - */ public function benchFetchFactory1() { // @todo @link https://github.com/phpbench/phpbench/issues/304 @@ -60,11 +58,6 @@ public function benchFetchFactory1() $sm->get('factory1'); } - /** - * @Revs(1000) - * @Iterations(20) - * @Warmup(2) - */ public function benchFetchInvokable1() { // @todo @link https://github.com/phpbench/phpbench/issues/304 @@ -73,11 +66,6 @@ public function benchFetchInvokable1() $sm->get('invokable1'); } - /** - * @Revs(1000) - * @Iterations(20) - * @Warmup(2) - */ public function benchFetchService1() { // @todo @link https://github.com/phpbench/phpbench/issues/304 @@ -86,11 +74,6 @@ public function benchFetchService1() $sm->get('service1'); } - /** - * @Revs(1000) - * @Iterations(20) - * @Warmup(2) - */ public function benchFetchAlias1() { // @todo @link https://github.com/phpbench/phpbench/issues/304 @@ -99,11 +82,6 @@ public function benchFetchAlias1() $sm->get('alias1'); } - /** - * @Revs(1000) - * @Iterations(20) - * @Warmup(2) - */ public function benchFetchRecursiveAlias1() { // @todo @link https://github.com/phpbench/phpbench/issues/304 @@ -112,11 +90,6 @@ public function benchFetchRecursiveAlias1() $sm->get('recursiveAlias1'); } - /** - * @Revs(1000) - * @Iterations(20) - * @Warmup(2) - */ public function benchFetchRecursiveAlias2() { // @todo @link https://github.com/phpbench/phpbench/issues/304 @@ -125,11 +98,6 @@ public function benchFetchRecursiveAlias2() $sm->get('recursiveAlias2'); } - /** - * @Revs(1000) - * @Iterations(20) - * @Warmup(2) - */ public function benchFetchAbstractFactoryService() { // @todo @link https://github.com/phpbench/phpbench/issues/304 diff --git a/benchmarks/FetchNewServiceManagerBench.php b/benchmarks/FetchNewServiceManagerBench.php index 7f598fdd..0ffa63db 100644 --- a/benchmarks/FetchNewServiceManagerBench.php +++ b/benchmarks/FetchNewServiceManagerBench.php @@ -7,6 +7,11 @@ use PhpBench\Benchmark\Metadata\Annotations\Warmup; use Zend\ServiceManager\ServiceManager; +/** + * @Revs(100) + * @Iterations(20) + * @Warmup(2) + */ class FetchNewServiceManagerBench { const NUM_SERVICES = 1000; @@ -39,11 +44,6 @@ public function __construct() $this->config = $config; } - /** - * @Revs(100) - * @Iterations(20) - * @Warmup(2) - */ public function benchFetchServiceManagerCreation() { new ServiceManager($this->config); diff --git a/benchmarks/FetchNewServicesBench.php b/benchmarks/FetchNewServicesBench.php index e0d50f12..caf1b3d1 100644 --- a/benchmarks/FetchNewServicesBench.php +++ b/benchmarks/FetchNewServicesBench.php @@ -7,6 +7,11 @@ use PhpBench\Benchmark\Metadata\Annotations\Warmup; use Zend\ServiceManager\ServiceManager; +/** + * @Revs(1000) + * @Iterations(10) + * @Warmup(2) + */ class FetchNewServicesBench { /** @@ -37,11 +42,6 @@ public function __construct() ]); } - /** - * @Revs(1000) - * @Iterations(10) - * @Warmup(2) - */ public function benchFetchFactory1() { // @todo @link https://github.com/phpbench/phpbench/issues/304 @@ -50,11 +50,6 @@ public function benchFetchFactory1() $sm->get('factory1'); } - /** - * @Revs(1000) - * @Iterations(10) - * @Warmup(2) - */ public function benchBuildFactory1() { // @todo @link https://github.com/phpbench/phpbench/issues/304 @@ -63,11 +58,6 @@ public function benchBuildFactory1() $sm->build('factory1'); } - /** - * @Revs(1000) - * @Iterations(10) - * @Warmup(2) - */ public function benchFetchInvokable1() { // @todo @link https://github.com/phpbench/phpbench/issues/304 @@ -76,11 +66,6 @@ public function benchFetchInvokable1() $sm->get('invokable1'); } - /** - * @Revs(1000) - * @Iterations(10) - * @Warmup(2) - */ public function benchBuildInvokable1() { // @todo @link https://github.com/phpbench/phpbench/issues/304 @@ -89,11 +74,6 @@ public function benchBuildInvokable1() $sm->build('invokable1'); } - /** - * @Revs(1000) - * @Iterations(10) - * @Warmup(2) - */ public function benchFetchService1() { // @todo @link https://github.com/phpbench/phpbench/issues/304 @@ -102,11 +82,6 @@ public function benchFetchService1() $sm->get('service1'); } - /** - * @Revs(1000) - * @Iterations(10) - * @Warmup(2) - */ public function benchFetchFactoryAlias1() { // @todo @link https://github.com/phpbench/phpbench/issues/304 @@ -115,11 +90,6 @@ public function benchFetchFactoryAlias1() $sm->build('factoryAlias1'); } - /** - * @Revs(1000) - * @Iterations(10) - * @Warmup(2) - */ public function benchBuildFactoryAlias1() { // @todo @link https://github.com/phpbench/phpbench/issues/304 @@ -128,11 +98,6 @@ public function benchBuildFactoryAlias1() $sm->build('factoryAlias1'); } - /** - * @Revs(1000) - * @Iterations(10) - * @Warmup(2) - */ public function benchFetchRecursiveFactoryAlias1() { // @todo @link https://github.com/phpbench/phpbench/issues/304 @@ -141,11 +106,6 @@ public function benchFetchRecursiveFactoryAlias1() $sm->build('recursiveFactoryAlias1'); } - /** - * @Revs(1000) - * @Iterations(10) - * @Warmup(2) - */ public function benchBuildRecursiveFactoryAlias1() { // @todo @link https://github.com/phpbench/phpbench/issues/304 @@ -154,11 +114,6 @@ public function benchBuildRecursiveFactoryAlias1() $sm->build('recursiveFactoryAlias1'); } - /** - * @Revs(1000) - * @Iterations(10) - * @Warmup(2) - */ public function benchFetchRecursiveFactoryAlias2() { // @todo @link https://github.com/phpbench/phpbench/issues/304 @@ -167,11 +122,6 @@ public function benchFetchRecursiveFactoryAlias2() $sm->build('recursiveFactoryAlias2'); } - /** - * @Revs(1000) - * @Iterations(10) - * @Warmup(2) - */ public function benchBuildRecursiveFactoryAlias2() { // @todo @link https://github.com/phpbench/phpbench/issues/304 @@ -180,11 +130,6 @@ public function benchBuildRecursiveFactoryAlias2() $sm->build('recursiveFactoryAlias2'); } - /** - * @Revs(1000) - * @Iterations(10) - * @Warmup(2) - */ public function benchFetchAbstractFactoryFoo() { // @todo @link https://github.com/phpbench/phpbench/issues/304 @@ -193,11 +138,6 @@ public function benchFetchAbstractFactoryFoo() $sm->get('foo'); } - /** - * @Revs(1000) - * @Iterations(10) - * @Warmup(2) - */ public function benchBuildAbstractFactoryFoo() { // @todo @link https://github.com/phpbench/phpbench/issues/304 From c4b9c9fc5a9532f186276a09918ffc77ce7bfd46 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Mon, 25 Jan 2016 00:04:17 +0100 Subject: [PATCH 12/50] issue #64 - adding to release notes --- CHANGELOG.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4486890a..08d4cd1d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,11 +20,12 @@ All notable changes to this project will be documented in this file, in reverse - Nothing. -## 3.0.2 - TBD +## 3.0.2 - 2015-01-24 ### Added -- Nothing. +- [#64](https://github.com/zendframework/zend-servicemanager/pull/64) performance optimizations + when dealing with alias resolution during service manager instantiation ### Deprecated From 89972c81ca014fbdeff62ddd4b9a29c8160617e5 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Mon, 25 Jan 2016 00:05:56 +0100 Subject: [PATCH 13/50] #64 - also includes #62 --- CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 08d4cd1d..6d9d612c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -37,7 +37,8 @@ All notable changes to this project will be documented in this file, in reverse ### Fixed -- Nothing. +- [#62](https://github.com/zendframework/zend-servicemanager/pull/62) + [#64](https://github.com/zendframework/zend-servicemanager/pull/64) corrected benchmark assets signature ## 3.0.1 - 2016-01-19 From 6c5e81710ba201cd5f3aa5aac8b781df77613106 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Mon, 25 Jan 2016 00:04:17 +0100 Subject: [PATCH 14/50] #64 - adding to release notes --- CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6d9d612c..bff24712 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,7 +6,8 @@ All notable changes to this project will be documented in this file, in reverse ### Added -- Nothing. +- [#64](https://github.com/zendframework/zend-servicemanager/pull/64) performance optimizations + when dealing with alias resolution during service manager instantiation ### Deprecated From ea77836c11259ccfbaa76dc1bedf00f699914969 Mon Sep 17 00:00:00 2001 From: maks feltrin Date: Sat, 16 Jan 2016 17:25:33 +0100 Subject: [PATCH 15/50] fixes link to wikipedia --- doc/book/lazy-services.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/book/lazy-services.md b/doc/book/lazy-services.md index 962cfb4c..5cdb1676 100644 --- a/doc/book/lazy-services.md +++ b/doc/book/lazy-services.md @@ -3,7 +3,7 @@ `Zend\ServiceManager` can use [delegator factories](delegators.md) to generate "lazy" references to your services. -Lazy services are [proxies](http://en.wikipedia.org/wiki/Proxypattern) that +Lazy services are [proxies](http://en.wikipedia.org/wiki/Proxy_pattern) that get lazily instantiated, and keep a reference to the real instance of the proxied service. From 3082c3f5ab8d36664227d2ca5d7fa8d6a71892f0 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Mon, 25 Jan 2016 00:12:34 +0100 Subject: [PATCH 16/50] #72 - changelong entry for the patch --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index bff24712..ab748161 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -40,6 +40,8 @@ All notable changes to this project will be documented in this file, in reverse - [#62](https://github.com/zendframework/zend-servicemanager/pull/62) [#64](https://github.com/zendframework/zend-servicemanager/pull/64) corrected benchmark assets signature +- [#72](https://github.com/zendframework/zend-servicemanager/pull/72) corrected link to the Proxy Pattern Wikipedia + page in the documentation ## 3.0.1 - 2016-01-19 From 8fc7cff8608436a557faf238e2852bbfb1a8768d Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Mon, 25 Jan 2016 00:04:17 +0100 Subject: [PATCH 17/50] #64 - adding to release notes --- CHANGELOG.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ab748161..306f787c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,8 +6,7 @@ All notable changes to this project will be documented in this file, in reverse ### Added -- [#64](https://github.com/zendframework/zend-servicemanager/pull/64) performance optimizations - when dealing with alias resolution during service manager instantiation +- Nothing. ### Deprecated From 55895a525ac4d259c1111b46c9d934ec77f1251a Mon Sep 17 00:00:00 2001 From: Matt Kynaston Date: Sat, 23 Jan 2016 18:44:50 +0000 Subject: [PATCH 18/50] Fixed creationContext not being passed to abstract factory canCreate() --- src/ServiceManager.php | 4 ++-- test/AbstractPluginManagerTest.php | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/ServiceManager.php b/src/ServiceManager.php index f6d4d97d..2bd3872a 100644 --- a/src/ServiceManager.php +++ b/src/ServiceManager.php @@ -236,7 +236,7 @@ public function has($name) // Check abstract factories foreach ($this->abstractFactories as $abstractFactory) { - if ($abstractFactory->canCreate($this, $name)) { + if ($abstractFactory->canCreate($this->creationContext, $name)) { return true; } } @@ -605,7 +605,7 @@ private function getFactory($name) // Check abstract factories foreach ($this->abstractFactories as $abstractFactory) { - if ($abstractFactory->canCreate($this, $name)) { + if ($abstractFactory->canCreate($this->creationContext, $name)) { return $abstractFactory; } } diff --git a/test/AbstractPluginManagerTest.php b/test/AbstractPluginManagerTest.php index c06cc054..c144ac30 100644 --- a/test/AbstractPluginManagerTest.php +++ b/test/AbstractPluginManagerTest.php @@ -16,6 +16,7 @@ use Zend\ServiceManager\Exception\InvalidArgumentException; use Zend\ServiceManager\Exception\InvalidServiceException; use Zend\ServiceManager\Exception\ServiceNotFoundException; +use Zend\ServiceManager\Factory\AbstractFactoryInterface; use Zend\ServiceManager\Factory\FactoryInterface; use Zend\ServiceManager\Factory\InvokableFactory; use Zend\ServiceManager\ServiceManager; @@ -349,4 +350,17 @@ public function testPassingServiceInstanceViaConfigureShouldRaiseExceptionForInv stdClass::class => new stdClass(), ]]); } + + public function testAbstractFactoryGetsCreationContext() + { + $serviceManager = new ServiceManager(); + $pluginManager = new TestAsset\SimplePluginManager($serviceManager); + $abstractFactory = $this->prophesize(AbstractFactoryInterface::class); + $abstractFactory->canCreate($serviceManager, 'foo') + ->willReturn(true); + $abstractFactory->__invoke($serviceManager, 'foo', null) + ->willReturn(new InvokableObject()); + $pluginManager->addAbstractFactory($abstractFactory->reveal()); + $this->assertInstanceOf(InvokableObject::class, $pluginManager->get('foo')); + } } From e56e5e19862735944ffc411d0b825e0c62724dcc Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Mon, 25 Jan 2016 00:21:41 +0100 Subject: [PATCH 19/50] #78 #79 - adding `@group` annotations to the newly introduced test case --- test/AbstractPluginManagerTest.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/test/AbstractPluginManagerTest.php b/test/AbstractPluginManagerTest.php index c144ac30..d48a38eb 100644 --- a/test/AbstractPluginManagerTest.php +++ b/test/AbstractPluginManagerTest.php @@ -351,6 +351,10 @@ public function testPassingServiceInstanceViaConfigureShouldRaiseExceptionForInv ]]); } + /** + * @group 79 + * @group 78 + */ public function testAbstractFactoryGetsCreationContext() { $serviceManager = new ServiceManager(); From 3a55b8df6f5b854fd61bf4b3bfa6ff7fd8eaa322 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Mon, 25 Jan 2016 00:22:58 +0100 Subject: [PATCH 20/50] #78 #79 - documenting patch changes in the changelog --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 306f787c..223084bf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -41,6 +41,9 @@ All notable changes to this project will be documented in this file, in reverse [#64](https://github.com/zendframework/zend-servicemanager/pull/64) corrected benchmark assets signature - [#72](https://github.com/zendframework/zend-servicemanager/pull/72) corrected link to the Proxy Pattern Wikipedia page in the documentation +- [#78](https://github.com/zendframework/zend-servicemanager/issues/78) + [#79](https://github.com/zendframework/zend-servicemanager/pull/79) creation context was not being correctly passed + to abstract factories when using plugin managers ## 3.0.1 - 2016-01-19 From e3e8f8f35dd07f71d3c60f66f210e72ba455b4da Mon Sep 17 00:00:00 2001 From: Matthieu Napoli Date: Mon, 25 Jan 2016 00:22:53 +0100 Subject: [PATCH 21/50] Fix phpdoc --- src/InitializerInterface.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/InitializerInterface.php b/src/InitializerInterface.php index a33d7106..7e4c65cf 100644 --- a/src/InitializerInterface.php +++ b/src/InitializerInterface.php @@ -16,11 +16,11 @@ * * If upgrading from v2, take the following steps: * - * - rename the method `initializer()` to `__invoke()`, and: + * - rename the method `initialize()` to `__invoke()`, and: * - rename the `$serviceLocator` argument to `$container`, and change the * typehint to `Interop\Container\ContainerInterface` * - swap the order of the arguments (so that `$instance` comes second) - * - create an `initializer()` method as defined in this interface, and have it + * - create an `initialize()` method as defined in this interface, and have it * proxy to `__invoke()`, passing the arguments in the new order. * * Once you have tested your code, you can then update your class to only implement From 8299501c9f42b98518ca5ecfcb756b58acd5eaea Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Mon, 25 Jan 2016 00:27:40 +0100 Subject: [PATCH 22/50] #82 - adding entry to the changelog --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 223084bf..3670edcf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -44,6 +44,8 @@ All notable changes to this project will be documented in this file, in reverse - [#78](https://github.com/zendframework/zend-servicemanager/issues/78) [#79](https://github.com/zendframework/zend-servicemanager/pull/79) creation context was not being correctly passed to abstract factories when using plugin managers +- [#82](https://github.com/zendframework/zend-servicemanager/pull/82) corrected migration guide in the DocBlock of + the `InitializerInterface` ## 3.0.1 - 2016-01-19 From 55a84941485c5c1b795570f37f7479b69e627e93 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Mon, 25 Jan 2016 00:48:26 +0100 Subject: [PATCH 23/50] Adding 3.0.3 changelog block (for future releases) --- CHANGELOG.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3670edcf..aea0c7d3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,24 @@ All notable changes to this project will be documented in this file, in reverse - Nothing. +## 3.0.3 - TBD + +### Added + +- Nothing. + +### Deprecated + +- Nothing. + +### Removed + +- Nothing. + +### Fixed + +- Nothing. + ## 3.0.2 - 2015-01-24 ### Added From 820df6feacb391a15f74c4e726c4f8f285544b79 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Mon, 25 Jan 2016 00:04:17 +0100 Subject: [PATCH 24/50] #64 - adding to release notes --- CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index aea0c7d3..7d14d1f6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,7 +6,8 @@ All notable changes to this project will be documented in this file, in reverse ### Added -- Nothing. +- [#64](https://github.com/zendframework/zend-servicemanager/pull/64) performance optimizations + when dealing with alias resolution during service manager instantiation ### Deprecated From 2f334069f55ef01b7bb16be9c0b655b873be1275 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Mon, 25 Jan 2016 00:48:26 +0100 Subject: [PATCH 25/50] Adding 3.0.3 changelog block (for future releases) --- CHANGELOG.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7d14d1f6..91378a1e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -39,6 +39,24 @@ All notable changes to this project will be documented in this file, in reverse - Nothing. +## 3.0.3 - TBD + +### Added + +- Nothing. + +### Deprecated + +- Nothing. + +### Removed + +- Nothing. + +### Fixed + +- Nothing. + ## 3.0.2 - 2015-01-24 ### Added From 897d016636391b939735366d428b5b3a53029d81 Mon Sep 17 00:00:00 2001 From: Vytautas Stankus Date: Mon, 25 Jan 2016 10:26:40 +0200 Subject: [PATCH 26/50] Fixed release date someone is still living in 2015 :D --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 91378a1e..ea4574b9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -57,7 +57,7 @@ All notable changes to this project will be documented in this file, in reverse - Nothing. -## 3.0.2 - 2015-01-24 +## 3.0.2 - 2016-01-24 ### Added From 292f36f1af4be8cae75d5a57c366d6df0b53cd08 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Mon, 25 Jan 2016 15:17:08 +0100 Subject: [PATCH 27/50] #84 - merge conflict resolution --- CHANGELOG.md | 18 ------------------ 1 file changed, 18 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ea4574b9..3cceeb7f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -39,24 +39,6 @@ All notable changes to this project will be documented in this file, in reverse - Nothing. -## 3.0.3 - TBD - -### Added - -- Nothing. - -### Deprecated - -- Nothing. - -### Removed - -- Nothing. - -### Fixed - -- Nothing. - ## 3.0.2 - 2016-01-24 ### Added From 69147735961ab678b9e6cc929f1a8b11acbf80d2 Mon Sep 17 00:00:00 2001 From: JakeFr Date: Fri, 5 Feb 2016 15:42:44 +0100 Subject: [PATCH 28/50] [DOC] MD syntax correction Fix typo in list of parameters passed to the delegator factory --- doc/book/delegators.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/doc/book/delegators.md b/doc/book/delegators.md index 359afd3b..2cb779ae 100644 --- a/doc/book/delegators.md +++ b/doc/book/delegators.md @@ -29,8 +29,7 @@ The parameters passed to the delegator factory are the following: - `$container` is the service locator that is used while creating the delegator for the requested service. - `$name` is the name of the service being requested. -- `$callback` is a -- [callable](http://www.php.net/manual/en/language.types.callable.php) that is +- `$callback` is a [callable](http://www.php.net/manual/en/language.types.callable.php) that is responsible for instantiating the delegated service (the real service instance). - `$options` is an array of options to use when creating the instance; these are typically used only during `build()` operations. From b8ff06cc82f6e96ee21b1a29595664e1a2cedb3f Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Wed, 1 Jun 2016 15:07:39 +0200 Subject: [PATCH 29/50] #97 adding changelog entry for #97 --- CHANGELOG.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cbb5dc34..415ac069 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ All notable changes to this project will be documented in this file, in reverse chronological order by release. -## 3.1.0 - TBD +## 3.1.0 - 2016-06-01 ### Added @@ -18,7 +18,8 @@ All notable changes to this project will be documented in this file, in reverse ### Fixed -- Nothing. +- [#97](https://github.com/zendframework/zend-servicemanager/pull/97) Typo corrections + in the delegator factories documentation ## 3.0.4 - TBD From 5ff81c1b3a0ac04fd4e5b53e0001523ebf69eee8 Mon Sep 17 00:00:00 2001 From: Vytautas Stankus Date: Wed, 24 Feb 2016 10:01:08 +0200 Subject: [PATCH 30/50] fixed coveralls config and restricted satooshi/php-coveralls to ^1.0 stable version --- .coveralls.yml | 1 - .travis.yml | 4 ++-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/.coveralls.yml b/.coveralls.yml index 53bda829..bc71b62f 100644 --- a/.coveralls.yml +++ b/.coveralls.yml @@ -1,3 +1,2 @@ coverage_clover: clover.xml json_path: coveralls-upload.json -src_dir: src diff --git a/.travis.yml b/.travis.yml index 2948647d..e97da0ed 100644 --- a/.travis.yml +++ b/.travis.yml @@ -45,7 +45,7 @@ notifications: before_install: - if [[ $EXECUTE_TEST_COVERALLS != 'true' ]]; then phpenv config-rm xdebug.ini || return 0 ; fi - composer self-update - - if [[ $EXECUTE_TEST_COVERALLS == 'true' ]]; then composer require --dev --no-update satooshi/php-coveralls ; fi + - if [[ $EXECUTE_TEST_COVERALLS == 'true' ]]; then composer require --dev --no-update satooshi/php-coveralls:^1.0 ; fi install: - travis_retry composer install --no-interaction --ignore-platform-reqs @@ -60,4 +60,4 @@ after_success: - if [[ $DEPLOY_DOCS == "true" ]]; then echo "Preparing to build and deploy documentation" ; ./zf-mkdoc-theme/deploy.sh ; echo "Completed deploying documentation" ; fi after_script: - - if [[ $EXECUTE_TEST_COVERALLS == 'true' ]]; then ./vendor/bin/coveralls ; fi + - if [[ $EXECUTE_TEST_COVERALLS == 'true' ]]; then ./vendor/bin/coveralls -v ; fi From 5fe4d5f9d88e0948fe52a27db8a6e44abd6fa545 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Wed, 1 Jun 2016 15:10:32 +0200 Subject: [PATCH 31/50] #98 adding changelog entry for #98 --- CHANGELOG.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 415ac069..ea2bb85e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,7 +19,9 @@ All notable changes to this project will be documented in this file, in reverse ### Fixed - [#97](https://github.com/zendframework/zend-servicemanager/pull/97) Typo corrections - in the delegator factories documentation + in the delegator factories documentation. +- [#98](https://github.com/zendframework/zend-servicemanager/pull/98) Using coveralls ^1.0 + for tracking test code coverage changes. ## 3.0.4 - TBD From 8a85b64f42da8201fee8a682a5572d4221a08a42 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Sun, 13 Mar 2016 01:19:45 +0100 Subject: [PATCH 32/50] Allowing ProxyManager v2 installation --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 65e7a7d8..8199e96b 100644 --- a/composer.json +++ b/composer.json @@ -19,7 +19,7 @@ }, "require-dev": { "phpunit/phpunit": "~4.6", - "ocramius/proxy-manager": "~1.0", + "ocramius/proxy-manager": "^1.0 || ^2.0", "squizlabs/php_codesniffer": "^2.0@dev", "phpbench/phpbench": "^0.10.0" }, From 3d4e4166c3e646dd34e0bd0e89c5bc1a9598bb74 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Sun, 13 Mar 2016 01:27:29 +0100 Subject: [PATCH 33/50] PHPUnit 5.0 is required in order to mock methods with return type declarations --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 8199e96b..07a5138a 100644 --- a/composer.json +++ b/composer.json @@ -18,7 +18,7 @@ "container-interop/container-interop": "~1.0" }, "require-dev": { - "phpunit/phpunit": "~4.6", + "phpunit/phpunit": "^4.6 || ^5.2.10", "ocramius/proxy-manager": "^1.0 || ^2.0", "squizlabs/php_codesniffer": "^2.0@dev", "phpbench/phpbench": "^0.10.0" From 05d36656b7eed7d7d4ad935048751d7917cd5b29 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Sun, 13 Mar 2016 01:31:46 +0100 Subject: [PATCH 34/50] ProxyManager does not write files by default in version 2.* - need to tell it to do so manually --- src/ServiceManager.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/ServiceManager.php b/src/ServiceManager.php index 8e5b6bee..22265c56 100644 --- a/src/ServiceManager.php +++ b/src/ServiceManager.php @@ -14,7 +14,9 @@ use Interop\Container\Exception\ContainerException; use ProxyManager\Configuration as ProxyConfiguration; use ProxyManager\Factory\LazyLoadingValueHolderFactory; +use ProxyManager\FileLocator\FileLocator; use ProxyManager\GeneratorStrategy\EvaluatingGeneratorStrategy; +use ProxyManager\GeneratorStrategy\FileWriterGeneratorStrategy; use Zend\ServiceManager\Exception\ContainerModificationsNotAllowedException; use Zend\ServiceManager\Exception\CyclicAliasException; use Zend\ServiceManager\Exception\InvalidArgumentException; @@ -750,6 +752,10 @@ private function createLazyServiceDelegatorFactory() if (! isset($this->lazyServices['write_proxy_files']) || ! $this->lazyServices['write_proxy_files']) { $factoryConfig->setGeneratorStrategy(new EvaluatingGeneratorStrategy()); + } else { + $factoryConfig->setGeneratorStrategy(new FileWriterGeneratorStrategy( + new FileLocator($factoryConfig->getProxiesTargetDir()) + )); } spl_autoload_register($factoryConfig->getProxyAutoloader()); From 80b96de7424d09e3b33327dd2f2ab59787c62233 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Sun, 13 Mar 2016 01:36:35 +0100 Subject: [PATCH 35/50] We should rely on stable versions of `squizlabs/php_codesniffer`, aye? --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 07a5138a..17c1dea8 100644 --- a/composer.json +++ b/composer.json @@ -20,7 +20,7 @@ "require-dev": { "phpunit/phpunit": "^4.6 || ^5.2.10", "ocramius/proxy-manager": "^1.0 || ^2.0", - "squizlabs/php_codesniffer": "^2.0@dev", + "squizlabs/php_codesniffer": "^2.5.1", "phpbench/phpbench": "^0.10.0" }, "suggest": { From 8908f562ee25ac007349222ee14d8177d83495a8 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Sun, 13 Mar 2016 01:44:33 +0100 Subject: [PATCH 36/50] Ignoring platform requirements? Really? --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index e97da0ed..9fa17021 100644 --- a/.travis.yml +++ b/.travis.yml @@ -48,7 +48,7 @@ before_install: - if [[ $EXECUTE_TEST_COVERALLS == 'true' ]]; then composer require --dev --no-update satooshi/php-coveralls:^1.0 ; fi install: - - travis_retry composer install --no-interaction --ignore-platform-reqs + - travis_retry composer install --no-interaction script: - if [[ $EXECUTE_TEST_COVERALLS == 'true' ]]; then ./vendor/bin/phpunit --coverage-clover clover.xml ; fi From 9bb697b7ce389eddf3c0fa4838a79b16bdb343f9 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Sun, 13 Mar 2016 01:46:13 +0100 Subject: [PATCH 37/50] PHP 7.0 should never be allowed to fail --- .travis.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 9fa17021..764a0a1b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -35,7 +35,6 @@ matrix: - php: 7 - php: hhvm allow_failures: - - php: 7 - php: hhvm notifications: From 2aec46d48545477e63fb2820ab1398718fdd49e9 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Wed, 1 Jun 2016 15:18:11 +0200 Subject: [PATCH 38/50] #103 adding changelog entries --- CHANGELOG.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ea2bb85e..a20478e3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,7 +6,11 @@ All notable changes to this project will be documented in this file, in reverse ### Added -- Nothing. +- [#103](https://github.com/zendframework/zend-servicemanager/pull/103) Allowing + installation of `ocramius/proxy-manager` `^2.0` together with + `zendframework/zend-servicemanager`. +- [#103](https://github.com/zendframework/zend-servicemanager/pull/103) Disallowing + test failures when running tests against PHP `7.0.*`. ### Deprecated From b9bbd4fe15170d8da68d2dd8b542ed1027431096 Mon Sep 17 00:00:00 2001 From: malinink Date: Tue, 5 Apr 2016 21:20:22 +0300 Subject: [PATCH 39/50] add test to setAlias method --- test/ServiceManagerTest.php | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/test/ServiceManagerTest.php b/test/ServiceManagerTest.php index 44666433..580e0db3 100644 --- a/test/ServiceManagerTest.php +++ b/test/ServiceManagerTest.php @@ -236,4 +236,29 @@ public function testAliasToAnExplicitServiceShouldWork() $this->assertSame($service, $alias); } + + /** + * @depends testAliasToAnExplicitServiceShouldWork + */ + public function testSetAliasShouldWorkWithRecursiveAlias() + { + $config = [ + 'aliases' => [ + 'Alias' => 'TailInvokable', + ], + 'services' => [ + InvokableObject::class => new InvokableObject(), + ], + ]; + $serviceManager = new ServiceManager($config); + $serviceManager->setAlias('HeadAlias', 'Alias'); + $serviceManager->setAlias('TailInvokable', InvokableObject::class); + + $service = $serviceManager->get(InvokableObject::class); + $alias = $serviceManager->get('Alias'); + $headAlias = $serviceManager->get('HeadAlias'); + + $this->assertSame($service, $alias); + $this->assertSame($service, $headAlias); + } } From be8ae43d066838ba9c11ea57cd71e7fad0b94ba3 Mon Sep 17 00:00:00 2001 From: malinink Date: Tue, 5 Apr 2016 21:45:33 +0300 Subject: [PATCH 40/50] add benchmark for setAlias and setFactories methods --- benchmarks/SetNewServicesBench.php | 79 ++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 benchmarks/SetNewServicesBench.php diff --git a/benchmarks/SetNewServicesBench.php b/benchmarks/SetNewServicesBench.php new file mode 100644 index 00000000..bafbc168 --- /dev/null +++ b/benchmarks/SetNewServicesBench.php @@ -0,0 +1,79 @@ + [ + 'factory1' => BenchAsset\FactoryFoo::class, + ], + 'invokables' => [ + 'invokable1' => BenchAsset\Foo::class, + ], + 'services' => [ + 'service1' => new \stdClass(), + ], + 'aliases' => [ + 'factoryAlias1' => 'factory1', + 'recursiveFactoryAlias1' => 'factoryAlias1', + 'recursiveFactoryAlias2' => 'recursiveFactoryAlias1', + ], + 'abstract_factories' => [ + BenchAsset\AbstractFactoryFoo::class + ], + ]; + + $service = new \stdClass(); + for ($i = 0; $i <= self::NUM_SERVICES; $i++) { + $config['factories']["factory_$i"] = BenchAsset\FactoryFoo::class; + $config['aliases']["alias_$i"] = "service_$i"; + } + + $this->sm = new ServiceManager($config); + } + + public function benchSetFactory() + { + // @todo @link https://github.com/phpbench/phpbench/issues/304 + $sm = clone $this->sm; + + $sm->setFactory('factory2', BenchAsset\FactoryFoo::class); + } + + public function benchSetAlias() + { + // @todo @link https://github.com/phpbench/phpbench/issues/304 + $sm = clone $this->sm; + + $sm->setAlias('factoryAlias2', 'factory1'); + } + + public function benchSetAliasOverrided() + { + // @todo @link https://github.com/phpbench/phpbench/issues/304 + $sm = clone $this->sm; + + $sm->setAlias('recursiveFactoryAlias1', 'factory1'); + } +} From 4270b287d83554fc46938ff68380302a75ddbd0a Mon Sep 17 00:00:00 2001 From: malinink Date: Wed, 6 Apr 2016 10:12:12 +0300 Subject: [PATCH 41/50] improve actions with aliases --- src/ServiceManager.php | 63 +++++++++++++++++++++++++++++++++++++----- 1 file changed, 56 insertions(+), 7 deletions(-) diff --git a/src/ServiceManager.php b/src/ServiceManager.php index 22265c56..ff3066c4 100644 --- a/src/ServiceManager.php +++ b/src/ServiceManager.php @@ -324,7 +324,6 @@ public function configure(array $config) ? array_merge($config['aliases'], $aliases) : $aliases; } - $config['factories'] = (isset($config['factories'])) ? array_merge($config['factories'], $factories) : $factories; @@ -343,10 +342,18 @@ public function configure(array $config) } if (isset($config['aliases'])) { - $this->aliases = $config['aliases'] + $this->aliases; - } - - if (! empty($this->aliases)) { + if ($this->configured) { + $intersect = $this->mergeIntoArray($this->aliases, $config['aliases']); + if ($intersect) { + $this->resolveAliases($this->aliases); + } else { + $this->resolveAliases($config['aliases'], true); + } + } else { + $this->aliases = $config['aliases'] + $this->aliases; + $this->resolveAliases($this->aliases); + } + } elseif (! $this->configured && ! empty($this->aliases)) { $this->resolveAliases($this->aliases); } @@ -376,6 +383,35 @@ public function configure(array $config) return $this; } + /** + * Merge source into destination. + * This method has good performance: + * - at empty destination + * - at adding few elements into array + * - at checking arrays keys intersection + * + * Provide only present intersect + * + * @param array $destination + * @param array $source + * @return boolean + */ + private function mergeIntoArray(array &$destination, array &$source) + { + if (empty($destination)) { + $destination = $source; + return false; + } + $intersect = false; + foreach ($source as $name => $target) { + if (isset($destination[$name])) { + $intersect = true; + } + $destination[$name] = $target; + } + return $intersect; + } + /** * Add an alias. * @@ -570,9 +606,12 @@ private function resolveInitializers(array $initializers) } /** - * Resolve all aliases to their canonical service names. + * Resolve aliases to their canonical service names. + * + * @param array $aliases + * @param boolean $onlyNewAliases */ - private function resolveAliases(array $aliases) + private function resolveAliases(array $aliases, $onlyNewAliases = false) { foreach ($aliases as $alias => $service) { $visited = []; @@ -589,6 +628,16 @@ private function resolveAliases(array $aliases) $this->resolvedAliases[$alias] = $name; } + + // Check and replace new aliases as targets in exists aliases. + + if ($onlyNewAliases) { + foreach ($this->resolvedAliases as $name => $target) { + if (isset($aliases[$target])) { + $this->resolvedAliases[$name] = $this->resolvedAliases[$target]; + } + } + } } /** From 500e805b97e7dc66cb8212616ec2c753fedd97dc Mon Sep 17 00:00:00 2001 From: malinink Date: Wed, 6 Apr 2016 10:33:21 +0300 Subject: [PATCH 42/50] [cs] revert removed translation --- src/ServiceManager.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/ServiceManager.php b/src/ServiceManager.php index ff3066c4..07e1cb33 100644 --- a/src/ServiceManager.php +++ b/src/ServiceManager.php @@ -324,6 +324,7 @@ public function configure(array $config) ? array_merge($config['aliases'], $aliases) : $aliases; } + $config['factories'] = (isset($config['factories'])) ? array_merge($config['factories'], $factories) : $factories; From d2aafccf3cb4a7946244eb88f1089c0a2ce6c836 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Wed, 1 Jun 2016 15:19:51 +0200 Subject: [PATCH 43/50] #112 #113 CS -alignment, removed unused variables --- benchmarks/SetNewServicesBench.php | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/benchmarks/SetNewServicesBench.php b/benchmarks/SetNewServicesBench.php index bafbc168..b3c2c011 100644 --- a/benchmarks/SetNewServicesBench.php +++ b/benchmarks/SetNewServicesBench.php @@ -15,7 +15,7 @@ class SetNewServicesBench { const NUM_SERVICES = 100; - + /** * @var ServiceManager */ @@ -23,18 +23,17 @@ class SetNewServicesBench public function __construct() { - $config = [ - 'factories' => [ - 'factory1' => BenchAsset\FactoryFoo::class, + 'factories' => [ + 'factory1' => BenchAsset\FactoryFoo::class, ], - 'invokables' => [ + 'invokables' => [ 'invokable1' => BenchAsset\Foo::class, ], - 'services' => [ + 'services' => [ 'service1' => new \stdClass(), ], - 'aliases' => [ + 'aliases' => [ 'factoryAlias1' => 'factory1', 'recursiveFactoryAlias1' => 'factoryAlias1', 'recursiveFactoryAlias2' => 'recursiveFactoryAlias1', @@ -43,13 +42,12 @@ public function __construct() BenchAsset\AbstractFactoryFoo::class ], ]; - - $service = new \stdClass(); + for ($i = 0; $i <= self::NUM_SERVICES; $i++) { - $config['factories']["factory_$i"] = BenchAsset\FactoryFoo::class; - $config['aliases']["alias_$i"] = "service_$i"; + $config['factories']["factory_$i"] = BenchAsset\FactoryFoo::class; + $config['aliases']["alias_$i"] = "service_$i"; } - + $this->sm = new ServiceManager($config); } From 094fb53a631e848ffd0c09c587c11acd32c64e55 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Wed, 1 Jun 2016 16:16:46 +0200 Subject: [PATCH 44/50] #112 #113 removed excessive inlining, named actual logic that resolves new aliases via old aliases --- src/ServiceManager.php | 65 ++++++++++++++++++++++++++++-------------- 1 file changed, 44 insertions(+), 21 deletions(-) diff --git a/src/ServiceManager.php b/src/ServiceManager.php index 07e1cb33..59840060 100644 --- a/src/ServiceManager.php +++ b/src/ServiceManager.php @@ -343,17 +343,7 @@ public function configure(array $config) } if (isset($config['aliases'])) { - if ($this->configured) { - $intersect = $this->mergeIntoArray($this->aliases, $config['aliases']); - if ($intersect) { - $this->resolveAliases($this->aliases); - } else { - $this->resolveAliases($config['aliases'], true); - } - } else { - $this->aliases = $config['aliases'] + $this->aliases; - $this->resolveAliases($this->aliases); - } + $this->configureAliases($config['aliases']); } elseif (! $this->configured && ! empty($this->aliases)) { $this->resolveAliases($this->aliases); } @@ -384,6 +374,31 @@ public function configure(array $config) return $this; } + /** + * @param string[] $aliases + * + * @return void + */ + private function configureAliases(array $aliases) + { + if (! $this->configured) { + $this->aliases = $aliases + $this->aliases; + + $this->resolveAliases($this->aliases); + + return; + } + + if ($this->mergeIntoArray($this->aliases, $aliases)) { + $this->resolveAliases($this->aliases); + + return; + } + + $this->resolveAliases($aliases); + $this->resolveNewAliasesWithPreviouslyResolvedAliases($aliases); + } + /** * Merge source into destination. * This method has good performance: @@ -609,10 +624,11 @@ private function resolveInitializers(array $initializers) /** * Resolve aliases to their canonical service names. * - * @param array $aliases - * @param boolean $onlyNewAliases + * @param string[] $aliases + * + * @returns void */ - private function resolveAliases(array $aliases, $onlyNewAliases = false) + private function resolveAliases(array $aliases) { foreach ($aliases as $alias => $service) { $visited = []; @@ -629,14 +645,21 @@ private function resolveAliases(array $aliases, $onlyNewAliases = false) $this->resolvedAliases[$alias] = $name; } + } - // Check and replace new aliases as targets in exists aliases. - - if ($onlyNewAliases) { - foreach ($this->resolvedAliases as $name => $target) { - if (isset($aliases[$target])) { - $this->resolvedAliases[$name] = $this->resolvedAliases[$target]; - } + /** + * Rewrites the map of aliases by resolving the given $aliases with the existing resolved ones. + * This is mostly done for performance reasons. + * + * @param string[] $aliases + * + * @return void + */ + private function resolveNewAliasesWithPreviouslyResolvedAliases(array $aliases) + { + foreach ($this->resolvedAliases as $name => $target) { + if (isset($aliases[$target])) { + $this->resolvedAliases[$name] = $this->resolvedAliases[$target]; } } } From 2b8d094022079f9e8045e9d11d561a393ef30d28 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Wed, 1 Jun 2016 17:05:38 +0200 Subject: [PATCH 45/50] #112 #113 removing unused by-ref assignments (not needed at all) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Before: +-----------------------------+----------------------------------+-------+--------+------+-----+-------------+-------------+-------------+-------------+-------------+----------+--------+-------------+ | benchmark | subject | group | params | revs | its | mem | best | mean | mode | worst | stdev | rstdev | diff | +-----------------------------+----------------------------------+-------+--------+------+-----+-------------+-------------+-------------+-------------+-------------+----------+--------+-------------+ | FetchCachedServicesBench | benchFetchFactory1 | | [] | 1000 | 20 | 964,792b | 2.764μs | 2.875μs | 2.847μs | 2.977μs | 0.071μs | 2.45% | +2.64% | | FetchCachedServicesBench | benchFetchInvokable1 | | [] | 1000 | 20 | 964,792b | 2.702μs | 2.801μs | 2.781μs | 2.939μs | 0.060μs | 2.13% | 0.00% | | FetchCachedServicesBench | benchFetchService1 | | [] | 1000 | 20 | 964,792b | 2.831μs | 2.948μs | 2.970μs | 3.047μs | 0.068μs | 2.31% | +5.25% | | FetchCachedServicesBench | benchFetchAlias1 | | [] | 1000 | 20 | 964,792b | 2.686μs | 2.812μs | 2.838μs | 2.908μs | 0.065μs | 2.32% | +0.40% | | FetchCachedServicesBench | benchFetchRecursiveAlias1 | | [] | 1000 | 20 | 964,800b | 2.812μs | 2.911μs | 2.930μs | 3.021μs | 0.067μs | 2.32% | +3.95% | | FetchCachedServicesBench | benchFetchRecursiveAlias2 | | [] | 1000 | 20 | 964,800b | 2.747μs | 2.862μs | 2.935μs | 2.961μs | 0.074μs | 2.59% | +2.18% | | FetchCachedServicesBench | benchFetchAbstractFactoryService | | [] | 1000 | 20 | 964,808b | 13.010μs | 13.501μs | 13.659μs | 14.150μs | 0.299μs | 2.21% | +382.09% | | FetchNewServiceManagerBench | benchFetchServiceManagerCreation | | [] | 100 | 20 | 24,030,120b | 1,925.660μs | 1,962.191μs | 1,936.676μs | 2,009.680μs | 28.444μs | 1.45% | +69,965.74% | | FetchNewServicesBench | benchFetchFactory1 | | [] | 1000 | 10 | 967,480b | 13.060μs | 13.409μs | 13.214μs | 14.046μs | 0.303μs | 2.26% | +378.82% | | FetchNewServicesBench | benchBuildFactory1 | | [] | 1000 | 10 | 967,480b | 11.992μs | 12.426μs | 12.285μs | 12.991μs | 0.310μs | 2.49% | +343.69% | | FetchNewServicesBench | benchFetchInvokable1 | | [] | 1000 | 10 | 967,480b | 13.611μs | 14.086μs | 14.041μs | 14.667μs | 0.291μs | 2.07% | +402.98% | | FetchNewServicesBench | benchBuildInvokable1 | | [] | 1000 | 10 | 967,480b | 12.400μs | 12.837μs | 13.069μs | 13.199μs | 0.296μs | 2.31% | +358.40% | | FetchNewServicesBench | benchFetchService1 | | [] | 1000 | 10 | 967,480b | 2.723μs | 2.814μs | 2.762μs | 2.939μs | 0.068μs | 2.42% | +0.46% | | FetchNewServicesBench | benchFetchFactoryAlias1 | | [] | 1000 | 10 | 967,480b | 12.683μs | 13.027μs | 12.956μs | 13.508μs | 0.251μs | 1.93% | +365.15% | | FetchNewServicesBench | benchBuildFactoryAlias1 | | [] | 1000 | 10 | 967,480b | 12.385μs | 12.893μs | 12.766μs | 13.410μs | 0.322μs | 2.50% | +360.39% | | FetchNewServicesBench | benchFetchRecursiveFactoryAlias1 | | [] | 1000 | 10 | 967,496b | 12.296μs | 12.566μs | 12.421μs | 13.015μs | 0.239μs | 1.90% | +348.70% | | FetchNewServicesBench | benchBuildRecursiveFactoryAlias1 | | [] | 1000 | 10 | 967,496b | 12.272μs | 12.825μs | 13.105μs | 13.331μs | 0.356μs | 2.77% | +357.96% | | FetchNewServicesBench | benchFetchRecursiveFactoryAlias2 | | [] | 1000 | 10 | 967,496b | 12.286μs | 12.649μs | 12.569μs | 13.152μs | 0.271μs | 2.15% | +351.68% | | FetchNewServicesBench | benchBuildRecursiveFactoryAlias2 | | [] | 1000 | 10 | 967,496b | 12.125μs | 12.582μs | 12.716μs | 13.166μs | 0.340μs | 2.71% | +349.27% | | FetchNewServicesBench | benchFetchAbstractFactoryFoo | | [] | 1000 | 10 | 967,488b | 12.851μs | 13.358μs | 13.600μs | 13.813μs | 0.351μs | 2.63% | +376.99% | | FetchNewServicesBench | benchBuildAbstractFactoryFoo | | [] | 1000 | 10 | 967,488b | 12.266μs | 12.705μs | 12.633μs | 13.135μs | 0.295μs | 2.33% | +353.65% | | SetNewServicesBench | benchSetFactory | | [] | 1000 | 10 | 992,128b | 12.394μs | 12.755μs | 12.657μs | 13.189μs | 0.237μs | 1.86% | +355.45% | | SetNewServicesBench | benchSetAlias | | [] | 1000 | 10 | 992,128b | 28.579μs | 29.372μs | 29.209μs | 30.831μs | 0.590μs | 2.01% | +948.80% | | SetNewServicesBench | benchSetAliasOverrided | | [] | 1000 | 10 | 992,136b | 76.288μs | 77.842μs | 77.077μs | 81.144μs | 1.435μs | 1.84% | +2,679.58% | +-----------------------------+----------------------------------+-------+--------+------+-----+-------------+-------------+-------------+-------------+-------------+----------+--------+-------------+ After: +-----------------------------+----------------------------------+-------+--------+------+-----+-------------+-------------+-------------+-------------+-------------+----------+--------+-------------+ | benchmark | subject | group | params | revs | its | mem | best | mean | mode | worst | stdev | rstdev | diff | +-----------------------------+----------------------------------+-------+--------+------+-----+-------------+-------------+-------------+-------------+-------------+----------+--------+-------------+ | FetchCachedServicesBench | benchFetchFactory1 | | [] | 1000 | 20 | 963,704b | 2.727μs | 2.858μs | 2.847μs | 2.983μs | 0.064μs | 2.25% | +7.11% | | FetchCachedServicesBench | benchFetchInvokable1 | | [] | 1000 | 20 | 963,704b | 2.577μs | 2.705μs | 2.689μs | 2.829μs | 0.081μs | 3.00% | +1.38% | | FetchCachedServicesBench | benchFetchService1 | | [] | 1000 | 20 | 963,704b | 2.597μs | 2.668μs | 2.636μs | 2.766μs | 0.046μs | 1.74% | 0.00% | | FetchCachedServicesBench | benchFetchAlias1 | | [] | 1000 | 20 | 963,704b | 2.597μs | 2.721μs | 2.702μs | 2.852μs | 0.084μs | 3.07% | +1.99% | | FetchCachedServicesBench | benchFetchRecursiveAlias1 | | [] | 1000 | 20 | 963,712b | 2.622μs | 2.725μs | 2.795μs | 2.829μs | 0.079μs | 2.89% | +2.13% | | FetchCachedServicesBench | benchFetchRecursiveAlias2 | | [] | 1000 | 20 | 963,712b | 2.647μs | 2.760μs | 2.725μs | 2.893μs | 0.064μs | 2.31% | +3.44% | | FetchCachedServicesBench | benchFetchAbstractFactoryService | | [] | 1000 | 20 | 963,720b | 12.608μs | 13.053μs | 13.206μs | 13.434μs | 0.254μs | 1.94% | +389.20% | | FetchNewServiceManagerBench | benchFetchServiceManagerCreation | | [] | 100 | 20 | 24,029,952b | 1,699.350μs | 1,733.026μs | 1,720.312μs | 1,790.900μs | 24.906μs | 1.44% | +64,849.89% | | FetchNewServicesBench | benchFetchFactory1 | | [] | 1000 | 10 | 966,392b | 12.609μs | 13.165μs | 13.185μs | 13.559μs | 0.266μs | 2.02% | +393.38% | | FetchNewServicesBench | benchBuildFactory1 | | [] | 1000 | 10 | 966,392b | 11.787μs | 12.125μs | 11.949μs | 12.514μs | 0.262μs | 2.16% | +354.41% | | FetchNewServicesBench | benchFetchInvokable1 | | [] | 1000 | 10 | 966,392b | 13.441μs | 13.798μs | 13.877μs | 14.157μs | 0.209μs | 1.51% | +417.12% | | FetchNewServicesBench | benchBuildInvokable1 | | [] | 1000 | 10 | 966,392b | 12.281μs | 12.605μs | 12.432μs | 13.029μs | 0.256μs | 2.03% | +372.42% | | FetchNewServicesBench | benchFetchService1 | | [] | 1000 | 10 | 966,392b | 2.810μs | 2.864μs | 2.834μs | 2.958μs | 0.049μs | 1.72% | +7.33% | | FetchNewServicesBench | benchFetchFactoryAlias1 | | [] | 1000 | 10 | 966,392b | 11.910μs | 12.376μs | 12.187μs | 12.974μs | 0.351μs | 2.84% | +363.82% | | FetchNewServicesBench | benchBuildFactoryAlias1 | | [] | 1000 | 10 | 966,392b | 12.000μs | 12.354μs | 12.329μs | 12.672μs | 0.176μs | 1.42% | +363.01% | | FetchNewServicesBench | benchFetchRecursiveFactoryAlias1 | | [] | 1000 | 10 | 966,408b | 11.691μs | 12.134μs | 12.277μs | 12.501μs | 0.262μs | 2.16% | +354.76% | | FetchNewServicesBench | benchBuildRecursiveFactoryAlias1 | | [] | 1000 | 10 | 966,408b | 12.137μs | 12.516μs | 12.344μs | 12.971μs | 0.277μs | 2.21% | +369.09% | | FetchNewServicesBench | benchFetchRecursiveFactoryAlias2 | | [] | 1000 | 10 | 966,408b | 11.863μs | 12.326μs | 12.412μs | 12.721μs | 0.295μs | 2.40% | +361.95% | | FetchNewServicesBench | benchBuildRecursiveFactoryAlias2 | | [] | 1000 | 10 | 966,408b | 11.780μs | 12.177μs | 12.138μs | 12.648μs | 0.275μs | 2.26% | +356.38% | | FetchNewServicesBench | benchFetchAbstractFactoryFoo | | [] | 1000 | 10 | 966,400b | 12.754μs | 13.143μs | 12.982μs | 13.743μs | 0.311μs | 2.37% | +392.58% | | FetchNewServicesBench | benchBuildAbstractFactoryFoo | | [] | 1000 | 10 | 966,400b | 11.730μs | 12.002μs | 11.872μs | 12.480μs | 0.237μs | 1.98% | +349.82% | | SetNewServicesBench | benchSetFactory | | [] | 1000 | 10 | 991,040b | 11.968μs | 12.282μs | 12.419μs | 12.579μs | 0.204μs | 1.66% | +360.32% | | SetNewServicesBench | benchSetAlias | | [] | 1000 | 10 | 991,040b | 28.803μs | 29.834μs | 29.417μs | 30.981μs | 0.703μs | 2.36% | +1,018.11% | | SetNewServicesBench | benchSetAliasOverrided | | [] | 1000 | 10 | 991,048b | 74.980μs | 76.211μs | 75.839μs | 78.355μs | 0.944μs | 1.24% | +2,756.22% | +-----------------------------+----------------------------------+-------+--------+------+-----+-------------+-------------+-------------+-------------+-------------+----------+--------+-------------+ --- src/ServiceManager.php | 29 +++++++++++++---------------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/src/ServiceManager.php b/src/ServiceManager.php index 59840060..98453d03 100644 --- a/src/ServiceManager.php +++ b/src/ServiceManager.php @@ -389,7 +389,7 @@ private function configureAliases(array $aliases) return; } - if ($this->mergeIntoArray($this->aliases, $aliases)) { + if ($this->mergeNewAliasesIntoOriginalOnes($aliases)) { $this->resolveAliases($this->aliases); return; @@ -400,31 +400,28 @@ private function configureAliases(array $aliases) } /** - * Merge source into destination. - * This method has good performance: - * - at empty destination - * - at adding few elements into array - * - at checking arrays keys intersection - * - * Provide only present intersect - * - * @param array $destination * @param array $source - * @return boolean + * + * @return bool whether any of the aliases got replaced */ - private function mergeIntoArray(array &$destination, array &$source) + private function mergeNewAliasesIntoOriginalOnes(array $source) { - if (empty($destination)) { - $destination = $source; + if (empty($this->aliases)) { + $this->aliases = $source; + return false; } + $intersect = false; + foreach ($source as $name => $target) { - if (isset($destination[$name])) { + if (isset($this->aliases[$name])) { $intersect = true; } - $destination[$name] = $target; + + $this->aliases[$name] = $target; } + return $intersect; } From f5d9d95e04c73fc9a770af9fa7bec50894faa89e Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Wed, 1 Jun 2016 18:07:41 +0200 Subject: [PATCH 46/50] #112 #113 naming corrections, for better clarity --- src/ServiceManager.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/ServiceManager.php b/src/ServiceManager.php index 98453d03..5f1cfb79 100644 --- a/src/ServiceManager.php +++ b/src/ServiceManager.php @@ -400,21 +400,21 @@ private function configureAliases(array $aliases) } /** - * @param array $source + * @param array $newAliases * * @return bool whether any of the aliases got replaced */ - private function mergeNewAliasesIntoOriginalOnes(array $source) + private function mergeNewAliasesIntoOriginalOnes(array $newAliases) { if (empty($this->aliases)) { - $this->aliases = $source; + $this->aliases = $newAliases; return false; } $intersect = false; - foreach ($source as $name => $target) { + foreach ($newAliases as $name => $target) { if (isset($this->aliases[$name])) { $intersect = true; } From a8092a22b1bb84f792c4b8ab34a5733c8b6f5ee6 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Wed, 1 Jun 2016 18:15:21 +0200 Subject: [PATCH 47/50] #112 #113 removed unused performance optimization loops (too much complexity for too little gain) --- src/ServiceManager.php | 32 +++++--------------------------- 1 file changed, 5 insertions(+), 27 deletions(-) diff --git a/src/ServiceManager.php b/src/ServiceManager.php index 5f1cfb79..8f626c79 100644 --- a/src/ServiceManager.php +++ b/src/ServiceManager.php @@ -389,7 +389,11 @@ private function configureAliases(array $aliases) return; } - if ($this->mergeNewAliasesIntoOriginalOnes($aliases)) { + // Performance optimization. If there are no collisions, then we don't need to recompute loops + $intersecting = $this->aliases && \array_intersect_key($this->aliases, $aliases); + $this->aliases = $this->aliases ? \array_merge($this->aliases, $aliases) : $aliases; + + if ($intersecting) { $this->resolveAliases($this->aliases); return; @@ -399,32 +403,6 @@ private function configureAliases(array $aliases) $this->resolveNewAliasesWithPreviouslyResolvedAliases($aliases); } - /** - * @param array $newAliases - * - * @return bool whether any of the aliases got replaced - */ - private function mergeNewAliasesIntoOriginalOnes(array $newAliases) - { - if (empty($this->aliases)) { - $this->aliases = $newAliases; - - return false; - } - - $intersect = false; - - foreach ($newAliases as $name => $target) { - if (isset($this->aliases[$name])) { - $intersect = true; - } - - $this->aliases[$name] = $target; - } - - return $intersect; - } - /** * Add an alias. * From 7520428e60b4f81dde1367edae8a5ab321f81ba3 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Wed, 1 Jun 2016 18:19:16 +0200 Subject: [PATCH 48/50] #112 #113 changelog entry --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index a20478e3..ede2a16c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,9 @@ All notable changes to this project will be documented in this file, in reverse `zendframework/zend-servicemanager`. - [#103](https://github.com/zendframework/zend-servicemanager/pull/103) Disallowing test failures when running tests against PHP `7.0.*`. +- [#103](https://github.com/zendframework/zend-servicemanager/pull/103) Improved performance + when dealing with registering aliases and factories via `ServiceManager#setFactory()` and + `ServiceManager#setAlias()` ### Deprecated From 465bc995de926c66c3a42694b98157be26c0778c Mon Sep 17 00:00:00 2001 From: Moln Date: Wed, 18 May 2016 17:18:05 +0800 Subject: [PATCH 49/50] Add the "provide" in composer.json --- composer.json | 3 +++ 1 file changed, 3 insertions(+) diff --git a/composer.json b/composer.json index 17c1dea8..d63debfe 100644 --- a/composer.json +++ b/composer.json @@ -40,5 +40,8 @@ "ZendTest\\ServiceManager\\": "test/", "ZendBench\\ServiceManager\\": "benchmarks/" } + }, + "provide": { + "container-interop/container-interop-implementation": "^1.1" } } From 66ffc8f90118cb1db4f7906a8fa2a8bfa97b071e Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Wed, 1 Jun 2016 18:38:17 +0200 Subject: [PATCH 50/50] #120 changelog entry --- CHANGELOG.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ede2a16c..9ea10320 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,9 +11,12 @@ All notable changes to this project will be documented in this file, in reverse `zendframework/zend-servicemanager`. - [#103](https://github.com/zendframework/zend-servicemanager/pull/103) Disallowing test failures when running tests against PHP `7.0.*`. -- [#103](https://github.com/zendframework/zend-servicemanager/pull/103) Improved performance +- [#113](https://github.com/zendframework/zend-servicemanager/pull/113) Improved performance when dealing with registering aliases and factories via `ServiceManager#setFactory()` and `ServiceManager#setAlias()` +- [#120](https://github.com/zendframework/zend-servicemanager/pull/120) The + `zendframework/zend-servicemanager` component now provides a + `container-interop/container-interop-implementation` implementation ### Deprecated