Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
0973046
Add benchmarks
samdark Apr 23, 2026
dc72ee8
Optimize string definition normalization
samdark Apr 24, 2026
ec30e1d
Optimize array definition config parsing
samdark Apr 24, 2026
c347321
Optimize explicit definition checks
samdark Apr 24, 2026
35905e6
Cache class-only array definitions
samdark Apr 24, 2026
9fabe3f
Cache normalized references
samdark Apr 24, 2026
c307ee1
Cache callable definition dependencies
samdark Apr 24, 2026
00a08b0
Cache parsed array definition keys
samdark Apr 24, 2026
7f09ce4
Avoid build stack reset for explicit definitions
samdark Apr 24, 2026
a856499
Fast-path array definition class checks
samdark Apr 24, 2026
bb55c4e
Fast-path cached plain references
samdark Apr 24, 2026
ace9d0b
Fast-path array definition normalization
samdark Apr 24, 2026
ad619f1
Fast-path strict mode misses
samdark Apr 24, 2026
b47843e
Cache normalized static callables
samdark Apr 24, 2026
3f3af1d
Avoid internal ArrayDefinition getter calls
samdark Apr 24, 2026
a7260e1
Cache normalized object values
samdark Apr 24, 2026
cbe0a4c
Cache normalized class definitions
samdark Apr 24, 2026
cc2999a
Fast-path cached plain reference strings
samdark Apr 24, 2026
08107ba
Ensure psalm and full coverage pass
samdark Apr 24, 2026
77f1708
Merge remote-tracking branch 'origin/master' into performance2
vjik Apr 24, 2026
01f60ab
Apply PHP CS Fixer and Rector changes (CI)
vjik Apr 24, 2026
5b20f85
Address comments
samdark Apr 24, 2026
c0508a6
Fix performance regression
samdark Apr 24, 2026
c827cd6
Fix test name
samdark Apr 24, 2026
a8c1f46
Add changelog
samdark Apr 24, 2026
4311381
Fixes
samdark May 25, 2026
95f18eb
Fixes
samdark May 25, 2026
1583704
Fix edge case
samdark May 25, 2026
4f9758a
Adjust benchmarks to be closer to reality. Remove unneeded optimizations
samdark May 25, 2026
3dd134f
Refine performance changes after benchmarks
samdark May 26, 2026
a00b622
Stabilize benchmark sampling
samdark May 26, 2026
ff21c2a
Fix typo
samdark May 26, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

- Enh #112: Explicitly import constants in "use" section (@mspirkov)
- Enh #113: Remove unnecessary files from Composer package (@mspirkov)
- Enh #114: Improve array definition config parsing and add benchmarks (@samdark)

## 3.4.1 December 02, 2025

Expand Down
4 changes: 4 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,15 @@
"bamarni/composer-bin-plugin": "^1.8.3",
"friendsofphp/php-cs-fixer": "^3.69",
"maglnet/composer-require-checker": "^4.7.1",
"phpbench/phpbench": "^1.4.1",
"phpunit/phpunit": "^10.5.45",
"rector/rector": "^2.4.2",
"spatie/phpunit-watcher": "^1.24",
"yiisoft/test-support": "^3.0.1"
},
"suggest": {
"phpbench/phpbench": "To run benchmarks."
},
"autoload": {
"psr-4": {
"Yiisoft\\Definitions\\": "src"
Expand Down
12 changes: 12 additions & 0 deletions phpbench.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"runner.bootstrap": "vendor/autoload.php",
"runner.path": "tests/Benchmark",
"runner.retry_threshold": 3,
"report.outputs": {
"csv_file": {
"extends": "delimited",
"delimiter": ",",
"file": "benchmarks.csv"
}
}
}
23 changes: 11 additions & 12 deletions src/ArrayDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@
use Yiisoft\Definitions\Helpers\ArrayDefinitionHelper;
use Yiisoft\Definitions\Helpers\DefinitionExtractor;
use Yiisoft\Definitions\Helpers\DefinitionResolver;
use Yiisoft\Definitions\Tests\Unit\Helpers\DefinitionValidatorTest;

use function array_key_exists;
use function call_user_func_array;
use function count;
use function gettype;
use function is_array;
use function is_string;
use function sprintf;
use function strpos;
use function substr;

/**
* Builds an object by array config.
Expand Down Expand Up @@ -185,19 +185,18 @@ private static function getMethodsAndPropertiesFromConfig(array $config): array
$methodsAndProperties = [];

foreach ($config as $key => $value) {
if ($key === self::CONSTRUCTOR) {
if ($key === self::CONSTRUCTOR || $key === self::CLASS_NAME) {
continue;
}

/**
* @infection-ignore-all Explode limit does not affect the result.
*
* @see DefinitionValidatorTest::testIncorrectMethodName()
*/
if (count($methodArray = explode('()', $key, 2)) === 2) {
$methodsAndProperties[$key] = [self::TYPE_METHOD, $methodArray[0], $value];
} elseif (count($propertyArray = explode('$', $key)) === 2) {
$methodsAndProperties[$key] = [self::TYPE_PROPERTY, $propertyArray[1], $value];
$position = strpos($key, '()');
if ($position !== false && $position !== 0 && strpos($key, '()', $position + 2) === false) {
$methodsAndProperties[$key] = [self::TYPE_METHOD, substr($key, 0, $position), $value];
continue;
}

if (($key[0] ?? '') === '$') {
$methodsAndProperties[$key] = [self::TYPE_PROPERTY, substr($key, 1), $value];
}
Comment thread
samdark marked this conversation as resolved.
}

Expand Down
196 changes: 196 additions & 0 deletions tests/Benchmark/DefinitionStorageBench.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Definitions\Tests\Benchmark;

use PhpBench\Benchmark\Metadata\Annotations\BeforeMethods;
use PhpBench\Benchmark\Metadata\Annotations\Groups;
use PhpBench\Benchmark\Metadata\Annotations\Iterations;
use PhpBench\Benchmark\Metadata\Annotations\Revs;
use Yiisoft\Definitions\DefinitionStorage;
use Yiisoft\Definitions\Tests\Support\Car;
use Yiisoft\Definitions\Tests\Support\ColorInterface;
use Yiisoft\Definitions\Tests\Support\ColorPink;
use Yiisoft\Definitions\Tests\Support\EngineInterface;
use Yiisoft\Definitions\Tests\Support\EngineMarkOne;
use Yiisoft\Test\Support\Container\SimpleContainer;

/**
* @Iterations(15)
* @Revs(1000)
* @BeforeMethods({"before"})
*/
final class DefinitionStorageBench
{
private const SERVICE_COUNT = 200;

/**
* @var int[]
*/
private array $indexes = [];

/**
* @var array<string, string>
*/
private array $definitions = [];

private DefinitionStorage $explicitStorage;
private DefinitionStorage $resolvableStorage;
private DefinitionStorage $unresolvableStorage;
private DefinitionStorage $delegateStorage;
private DefinitionStorage $strictStorage;

public function before(): void
{
$this->indexes = [];
$this->definitions = [
EngineInterface::class => EngineMarkOne::class,
ColorInterface::class => ColorPink::class,
];

for ($i = 0; $i < self::SERVICE_COUNT; $i++) {
$this->indexes[] = $i;
$this->definitions["service$i"] = Car::class;
}

$this->explicitStorage = new DefinitionStorage($this->definitions);
$this->resolvableStorage = new DefinitionStorage([
EngineInterface::class => EngineMarkOne::class,
]);
$this->unresolvableStorage = new DefinitionStorage();
$this->delegateStorage = new DefinitionStorage();
$this->delegateStorage->setDelegateContainer(new SimpleContainer([
EngineInterface::class => new EngineMarkOne(),
]));
$this->strictStorage = new DefinitionStorage([], true);

$this->resolvableStorage->has(Car::class);
$this->delegateStorage->has(Car::class);
}

/**
* Measures construction with a typical services map.
*
* @Groups({"construct", "storage"})
*/
public function benchConstruct(): void
{
new DefinitionStorage($this->definitions);
}

/**
* Measures positive lookups of explicitly configured service IDs.
*
* @Groups({"lookup", "storage", "cold"})
*/
public function benchSequentialExplicitLookupsColdStorage(): void
{
$storage = new DefinitionStorage($this->definitions);

foreach ($this->indexes as $index) {
$storage->has("service$index");
}
}

/**
* Measures positive lookups against a reused definitions storage.
*
* @Groups({"lookup", "storage", "warm", "typical"})
*/
public function benchSequentialExplicitLookupsWarmStorage(): void
{
foreach ($this->indexes as $index) {
$this->explicitStorage->has("service$index");
}
}

/**
* Measures positive autowiring checks for an object graph.
*
* @Groups({"lookup", "storage", "autowire", "typical"})
* @Revs(1000)
*/
public function benchResolvableObjectGraphColdStorage(): void
{
$storage = new DefinitionStorage([
EngineInterface::class => EngineMarkOne::class,
]);

$storage->has(Car::class);
}

/**
* Measures positive autowiring checks for an object graph against reused storage.
*
* @Groups({"lookup", "storage", "autowire", "warm", "typical"})
* @Revs(10000)
*/
public function benchResolvableObjectGraphWarmStorage(): void
{
$this->resolvableStorage->has(Car::class);
}

/**
* Measures negative autowiring checks for a missing dependency.
*
* @Groups({"lookup", "storage", "autowire", "typical"})
* @Revs(1000)
*/
public function benchUnresolvableObjectGraphColdStorage(): void
{
$storage = new DefinitionStorage();

$storage->has(Car::class);
}

/**
* Measures negative autowiring checks for a missing dependency against reused storage.
*
* @Groups({"lookup", "storage", "autowire", "warm", "typical"})
* @Revs(1000)
*/
public function benchUnresolvableObjectGraphWarmStorage(): void
{
$this->unresolvableStorage->has(Car::class);
}

/**
* Measures fallback through a delegate container when a dependency is not explicitly defined.
*
* @Groups({"lookup", "storage", "delegate", "typical"})
* @Revs(1000)
*/
public function benchResolvableObjectGraphWithDelegateColdStorage(): void
{
$storage = new DefinitionStorage();
$storage->setDelegateContainer(new SimpleContainer([
EngineInterface::class => new EngineMarkOne(),
]));

$storage->has(Car::class);
}

/**
* Measures fallback through a delegate container against reused storage.
*
* @Groups({"lookup", "storage", "delegate", "warm", "typical"})
* @Revs(10000)
*/
public function benchResolvableObjectGraphWithDelegateWarmStorage(): void
{
$this->delegateStorage->has(Car::class);
}

/**
* Measures strict mode misses where class names are not implicitly resolvable.
*
* @Groups({"lookup", "storage", "strict"})
*/
public function benchStrictModeClassMisses(): void
{
foreach ($this->indexes as $_index) {
$this->strictStorage->has(EngineMarkOne::class);
}
}
}
Loading
Loading