Skip to content

Commit f4ca886

Browse files
minor symfony#61107 optimize in_array calls (gharlan)
This PR was squashed before being merged into the 7.4 branch. Discussion ---------- optimize `in_array` calls | Q | A | ------------- | --- | Branch? | 7.4 | Bug fix? | no | New feature? | no | Deprecations? | no | Issues | | License | MIT `in_array` calls are optimized by php when fully-qualified and using a static array and the strict param: ```php namespace Foo; $a = [1, 2, 3]; \in_array($b, $a, true); // not optimized because variable array \in_array($b, [1, 2, 3]); // not optimized because missing strict param in_array($b, [1, 2, 3], true); // not optimized because no leading `\` or import \in_array($b, [1, 2, 3], true); // optimized ``` https://3v4l.org/FjKJ0/vld You can see in the "VLD" tab that lines 6, 8 and 10 use the function call opcodes (`INIT_FCALL` or `INIT_NS_FCALL_BY_NAME`) while line 12 uses the optimized `IN_ARRAY` opcode without a function call. In this pull request I changed only those places where the change enables the optimized opcode (by adding the strict param or inlining the array). Commits ------- 33157f7 optimize `in_array` calls
2 parents 36d8d5d + 33157f7 commit f4ca886

File tree

70 files changed

+89
-120
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

70 files changed

+89
-120
lines changed

src/Symfony/Bridge/Doctrine/DependencyInjection/AbstractDoctrineExtension.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ protected function getMappingDriverBundleConfigDefaults(array $bundleConfig, \Re
153153
}
154154

155155
if (!$bundleConfig['dir']) {
156-
if (\in_array($bundleConfig['type'], ['staticphp', 'attribute'])) {
156+
if (\in_array($bundleConfig['type'], ['staticphp', 'attribute'], true)) {
157157
$bundleConfig['dir'] = $bundleClassDir.'/'.$this->getMappingObjectDefaultName();
158158
} else {
159159
$bundleConfig['dir'] = $bundleDir.'/'.$this->getMappingResourceConfigDirectory($bundleDir);
@@ -225,7 +225,7 @@ protected function assertValidMappingConfiguration(array $mappingConfig, string
225225
throw new \InvalidArgumentException(\sprintf('Specified non-existing directory "%s" as Doctrine mapping source.', $mappingConfig['dir']));
226226
}
227227

228-
if (!\in_array($mappingConfig['type'], ['xml', 'yml', 'php', 'staticphp', 'attribute'])) {
228+
if (!\in_array($mappingConfig['type'], ['xml', 'yml', 'php', 'staticphp', 'attribute'], true)) {
229229
throw new \InvalidArgumentException(\sprintf('Can only configure "xml", "yml", "php", "staticphp" or "attribute" through the DoctrineBundle. Use your own bundle to configure other metadata drivers. You can register them by adding a new driver to the "%s" service definition.', $this->getObjectManagerElementName($objectManagerName.'_metadata_driver')));
230230
}
231231
}

src/Symfony/Bridge/Doctrine/Form/ChoiceList/IdReader.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public function __construct(
4848
$singleId = $this->associationIdReader->isSingleId();
4949
$this->intId = $this->associationIdReader->isIntId();
5050
} else {
51-
$this->intId = $singleId && \in_array($idType, ['integer', 'smallint', 'bigint']);
51+
$this->intId = $singleId && \in_array($idType, ['integer', 'smallint', 'bigint'], true);
5252
$this->associationIdReader = null;
5353
}
5454

src/Symfony/Bridge/Doctrine/Form/ChoiceList/ORMQueryBuilderLoader.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,13 @@ public function getEntitiesByIds(string $identifier, array $values): array
6161
// Guess type
6262
$entity = current($qb->getRootEntities());
6363
$metadata = $qb->getEntityManager()->getClassMetadata($entity);
64-
if (\in_array($type = $metadata->getTypeOfField($identifier), ['integer', 'bigint', 'smallint'])) {
64+
if (\in_array($type = $metadata->getTypeOfField($identifier), ['integer', 'bigint', 'smallint'], true)) {
6565
$parameterType = ArrayParameterType::INTEGER;
6666

6767
// Filter out non-integer values (e.g. ""). If we don't, some
6868
// databases such as PostgreSQL fail.
6969
$values = array_values(array_filter($values, fn ($v) => (string) $v === (string) (int) $v || ctype_digit($v)));
70-
} elseif (\in_array($type, ['ulid', 'uuid', 'guid'])) {
70+
} elseif (\in_array($type, ['ulid', 'uuid', 'guid'], true)) {
7171
$parameterType = ArrayParameterType::STRING;
7272

7373
// Like above, but we just filter out empty strings.

src/Symfony/Bridge/Doctrine/Form/DoctrineOrmTypeGuesser.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ public function guessMaxLength(string $class, string $property): ?ValueGuess
134134
return new ValueGuess($length, Guess::HIGH_CONFIDENCE);
135135
}
136136

137-
if (\in_array($ret[0]->getTypeOfField($property), [Types::DECIMAL, Types::FLOAT])) {
137+
if (\in_array($ret[0]->getTypeOfField($property), [Types::DECIMAL, Types::FLOAT], true)) {
138138
return new ValueGuess(null, Guess::MEDIUM_CONFIDENCE);
139139
}
140140
}
@@ -146,7 +146,7 @@ public function guessPattern(string $class, string $property): ?ValueGuess
146146
{
147147
$ret = $this->getMetadata($class);
148148
if ($ret && isset($ret[0]->fieldMappings[$property]) && !$ret[0]->hasAssociation($property)) {
149-
if (\in_array($ret[0]->getTypeOfField($property), [Types::DECIMAL, Types::FLOAT])) {
149+
if (\in_array($ret[0]->getTypeOfField($property), [Types::DECIMAL, Types::FLOAT], true)) {
150150
return new ValueGuess(null, Guess::MEDIUM_CONFIDENCE);
151151
}
152152
}

src/Symfony/Bundle/FrameworkBundle/Command/CacheClearCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ private function isNfs(string $dir): bool
213213
if ('/' === \DIRECTORY_SEPARATOR && @is_readable('/proc/mounts') && $files = @file('/proc/mounts')) {
214214
foreach ($files as $mount) {
215215
$mount = \array_slice(explode(' ', $mount), 1, -3);
216-
if (!\in_array(array_pop($mount), ['vboxsf', 'nfs'])) {
216+
if (!\in_array(array_pop($mount), ['vboxsf', 'nfs'], true)) {
217217
continue;
218218
}
219219
$mounts[] = implode(' ', $mount).'/';

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2563,7 +2563,7 @@ private function addRateLimiterSection(ArrayNodeDefinition $rootNode, callable $
25632563
->end()
25642564
->end()
25652565
->validate()
2566-
->ifTrue(static fn ($v) => !\in_array($v['policy'], ['no_limit', 'compound']) && !isset($v['limit']))
2566+
->ifTrue(static fn ($v) => !\in_array($v['policy'], ['no_limit', 'compound'], true) && !isset($v['limit']))
25672567
->thenInvalid('A limit must be provided when using a policy different than "compound" or "no_limit".')
25682568
->end()
25692569
->end()

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2053,7 +2053,7 @@ private function registerSerializerConfiguration(array $config, ContainerBuilder
20532053
}
20542054

20552055
$fileRecorder = function ($extension, $path) use (&$serializerLoaders) {
2056-
$definition = new Definition(\in_array($extension, ['yaml', 'yml']) ? YamlFileLoader::class : XmlFileLoader::class, [$path]);
2056+
$definition = new Definition(\in_array($extension, ['yaml', 'yml'], true) ? YamlFileLoader::class : XmlFileLoader::class, [$path]);
20572057
$serializerLoaders[] = $definition;
20582058
};
20592059

@@ -2935,7 +2935,7 @@ private function registerMailerConfiguration(array $config, ContainerBuilder $co
29352935
$headers = new Definition(Headers::class);
29362936
foreach ($config['headers'] as $name => $data) {
29372937
$value = $data['value'];
2938-
if (\in_array(strtolower($name), ['from', 'to', 'cc', 'bcc', 'reply-to'])) {
2938+
if (\in_array(strtolower($name), ['from', 'to', 'cc', 'bcc', 'reply-to'], true)) {
29392939
$value = (array) $value;
29402940
}
29412941
$headers->addMethodCall('addHeader', [$name, $value]);

src/Symfony/Bundle/TwigBundle/Tests/DependencyInjection/TwigExtensionTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public function testLoadFullConfiguration(string $format, ?string $buildDir)
9292
$this->assertEquals(3.14, $calls[4][1][1], '->load() registers variables as Twig globals');
9393

9494
// Yaml and Php specific configs
95-
if (\in_array($format, ['yml', 'php'])) {
95+
if (\in_array($format, ['yml', 'php'], true)) {
9696
$this->assertEquals('bad', $calls[5][1][0], '->load() registers variables as Twig globals');
9797
$this->assertEquals(['key' => 'foo'], $calls[5][1][1], '->load() registers variables as Twig globals');
9898
}

src/Symfony/Component/AssetMapper/ImportMap/ImportMapVersionChecker.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ public function checkVersions(): array
120120
public static function convertNpmConstraint(string $versionConstraint): ?string
121121
{
122122
// special npm constraint that don't translate to composer
123-
if (\in_array($versionConstraint, ['latest', 'next'])
123+
if (\in_array($versionConstraint, ['latest', 'next'], true)
124124
|| preg_match('/^(git|http|file):/', $versionConstraint)
125125
|| str_contains($versionConstraint, '/')
126126
) {

src/Symfony/Component/AssetMapper/ImportMap/PackageUpdateInfo.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,6 @@ public function __construct(
2929

3030
public function hasUpdate(): bool
3131
{
32-
return !\in_array($this->updateType, [self::UPDATE_TYPE_DOWNGRADE, self::UPDATE_TYPE_DOWNGRADE, self::UPDATE_TYPE_UP_TO_DATE]);
32+
return !\in_array($this->updateType, [self::UPDATE_TYPE_DOWNGRADE, self::UPDATE_TYPE_DOWNGRADE, self::UPDATE_TYPE_UP_TO_DATE], true);
3333
}
3434
}

0 commit comments

Comments
 (0)