Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid UTF-8 validating same string multiple times #3957

Closed
wants to merge 1 commit into from

Conversation

nikic
Copy link
Member

@nikic nikic commented Mar 18, 2019

Fix for https://bugs.php.net/bug.php?id=72685.

We currently have a huge performance problem when implementing lexers working on UTF-8 strings in PHP. This kind of code tends to perform a large number of matches at different offsets on a single string. This is generally fast. However, if /u mode is used, the full string will be UTF-8 validated on each match. This results in quadratic runtime.

This patch fixes the issue by adding a IS_STR_VALID_UTF8 flag, which is set when we have determined that the string is valid UTF8 and further validation is skipped.

A limitation of this approach is that we can't set the flag for interned strings. I think this is not a problem for this use-case which will generally work on dynamic data. If we want to use this flag for other purposes as well (mbstring?) then it might be worthwhile to UTF-8 validate strings during interning. But right now this doesn't seem useful.

@nikic
Copy link
Member Author

nikic commented Mar 18, 2019

@dstogov Can you take a look at this?

@KalleZ KalleZ added the Bug label Mar 18, 2019
@dstogov
Copy link
Member

dstogov commented Mar 18, 2019

Looks fine to me.

@cmb69
Copy link
Member

cmb69 commented Mar 18, 2019

This could be a base for solving bug #52998, too.

@nikic
Copy link
Member Author

nikic commented Mar 18, 2019

Merged as 2b9acd3 into 7.4.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants