Skip to content

Commit efc974d

Browse files
minor #46017 [Cache][String] Don't use is_iterable() in perf-critical code paths (nicolas-grekas)
This PR was merged into the 6.1 branch. Discussion ---------- [Cache][String] Don't use is_iterable() in perf-critical code paths | Q | A | ------------- | --- | Branch? | 6.1 | Bug fix? | no | New feature? | no | Deprecations? | no | Tickets | - | License | MIT | Doc PR | - This is 5 times faster than calling `is_iterable()` Partially reverting #45978 /cc `@fancyweb` FYI. Commits ------- 33fa00c [Cache][String] Don't use is_iterable() in perf-critical code paths
2 parents 7751cd3 + 33fa00c commit efc974d

File tree

2 files changed

+3
-3
lines changed

2 files changed

+3
-3
lines changed

src/Symfony/Component/Cache/CacheItem.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public function tag(mixed $tags): static
110110
if (!$this->isTaggable) {
111111
throw new LogicException(sprintf('Cache item "%s" comes from a non tag-aware pool: you cannot tag it.', $this->key));
112112
}
113-
if (!is_iterable($tags)) {
113+
if (!\is_array($tags) && !$tags instanceof \Traversable) { // don't use is_iterable(), it's slow
114114
$tags = [$tags];
115115
}
116116
foreach ($tags as $tag) {

src/Symfony/Component/String/AbstractString.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -558,7 +558,7 @@ abstract public function trimEnd(string $chars = " \t\n\r\0\x0B\x0C\u{A0}\u{FEFF
558558
*/
559559
public function trimPrefix($prefix): static
560560
{
561-
if (is_iterable($prefix)) {
561+
if (\is_array($prefix) || $prefix instanceof \Traversable) { // don't use is_iterable(), it's slow
562562
foreach ($prefix as $s) {
563563
$t = $this->trimPrefix($s);
564564

@@ -592,7 +592,7 @@ abstract public function trimStart(string $chars = " \t\n\r\0\x0B\x0C\u{A0}\u{FE
592592
*/
593593
public function trimSuffix($suffix): static
594594
{
595-
if (is_iterable($suffix)) {
595+
if (\is_array($suffix) || $suffix instanceof \Traversable) { // don't use is_iterable(), it's slow
596596
foreach ($suffix as $s) {
597597
$t = $this->trimSuffix($s);
598598

0 commit comments

Comments
 (0)