Skip to content

[Php80] Skip non-zero mb_strpos() offset in MbStrContainsRector - #8642

Merged
TomasVotruba merged 1 commit into
rectorphp:mainfrom
guillaume-sainthillier:mbstrcontains-skip-offset
Oct 12, 2026
Merged

TomasVotruba merged 1 commit into
rectorphp:mainfrom
guillaume-sainthillier:mbstrcontains-skip-offset

Conversation

@guillaume-sainthillier

Copy link
Copy Markdown
Contributor

Same as #8640, for MbStrContainsRector: an offset mb_strpos() is rewritten to str_contains(mb_substr($haystack, $offset), $needle), which copies the haystack on every call. mb_substr() also has to walk the string to count characters, so it's much worse than substr().

1,000 calls on a 1 MB string, offset 1000, PHP 8.5:

case mb_strpos(..., $offset) str_contains(mb_substr(...))
needle ~1 KB after offset 0.5 ms 1,217 ms
needle absent 21 ms 1,279 ms

Change:

  • offset resolving to 0: str_contains($haystack, $needle), no mb_substr()
  • any other offset: skipped

@TomasVotruba

Copy link
Copy Markdown
Member

Valid change, thank you 👍

@TomasVotruba
TomasVotruba merged commit d22964a into rectorphp:main Oct 12, 2026
46 of 49 checks passed
@guillaume-sainthillier
guillaume-sainthillier deleted the mbstrcontains-skip-offset branch October 12, 2026 11:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants