Skip to content

Commit

Permalink
minor #50087 [Config] Improve GlobResource performance (clxmstaab)
Browse files Browse the repository at this point in the history
This PR was merged into the 6.3 branch.

Discussion
----------

[Config] Improve GlobResource performance

| Q             | A
| ------------- | ---
| Branch?       | 6.3
| Bug fix?      | no
| New feature?  | no <!-- please update src/**/CHANGELOG.md files -->
| Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files -->
| Tickets       | Fix #... <!-- prefix each issue number with "Fix #", no need to create an issue if none exists, explain below instead -->
| License       | MIT
| Doc PR        | symfony/symfony-docs#... <!-- required for new features -->
<!--
Replace this notice by a short README for your feature/bugfix.
This will help reviewers and should be a good start for the documentation.

Additionally (see https://symfony.com/releases):
 - Always add tests and ensure they pass.
 - Bug fixes must be submitted against the lowest maintained branch where they apply
   (lowest branches are regularly merged to upper ones so they get the fixes too).
 - Features and deprecations must be submitted against the latest branch.
 - For new features, provide some code snippets to help understand usage.
 - Changelog entry should follow https://symfony.com/doc/current/contributing/code/conventions.html#writing-a-changelog-entry
 - Never break backward compatibility (see https://symfony.com/bc).
-->

Re-order conditions in GlobResource to prevent unnecessary file IO, which improves performance of the following test-script.

put this script into the symfony/symfony project root and run it
```php
<?php

error_reporting(E_ALL);

require __DIR__.'/vendor/autoload.php';

$glob = new \Symfony\Component\Config\Resource\GlobResource(__DIR__.'/src/', '/**/*.php', true);
foreach($glob as $file) {
    echo $file->getRealPath()."\n";
}
```

I did a few measures and it consistently reports a 15-18% improvement

![grafik](https://user-images.githubusercontent.com/120441/233592449-2844bad8-6217-4ec9-a387-a83bd18a6269.png)

https://blackfire.io/profiles/compare/647af42c-c4e5-495e-8f8d-1b77f61631dd/graph

Commits
-------

172b1f7 [Config] Improve GlobResource performance
  • Loading branch information
nicolas-grekas committed Apr 21, 2023
2 parents 5aa7633 + 172b1f7 commit 24b1b02
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/Symfony/Component/Config/Resource/GlobResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public function __wakeup(): void

public function getIterator(): \Traversable
{
if (!file_exists($this->prefix) || (!$this->recursive && '' === $this->pattern)) {
if ((!$this->recursive && '' === $this->pattern) || !file_exists($this->prefix)) {
return;
}

Expand Down Expand Up @@ -148,7 +148,7 @@ public function getIterator(): \Traversable
} while ($prefix !== $dirPath && $dirPath !== $normalizedPath = \dirname($dirPath));
}

if (is_file($path) && (null === $regex || preg_match($regex, substr(str_replace('\\', '/', $path), $prefixLen)))) {
if ((null === $regex || preg_match($regex, substr(str_replace('\\', '/', $path), $prefixLen))) && is_file($path)) {
yield $path => new \SplFileInfo($path);
}
if (!is_dir($path)) {
Expand All @@ -165,7 +165,7 @@ public function getIterator(): \Traversable
new \RecursiveCallbackFilterIterator(
new \RecursiveDirectoryIterator($path, \FilesystemIterator::SKIP_DOTS | \FilesystemIterator::FOLLOW_SYMLINKS),
fn (\SplFileInfo $file, $path) => !isset($this->excludedPrefixes[$path = str_replace('\\', '/', $path)])
&& (null === $regex || $file->isDir() || preg_match($regex, substr($path, $prefixLen)))
&& (null === $regex || preg_match($regex, substr($path, $prefixLen)) || $file->isDir())
&& '.' !== $file->getBasename()[0]
),
\RecursiveIteratorIterator::LEAVES_ONLY
Expand Down

0 comments on commit 24b1b02

Please sign in to comment.