Skip to content

ext/mbstring: Optimize valid UTF-8 in mb_substr() - #24080

Open
kamil-tekiela wants to merge 1 commit into
php:masterfrom
kamil-tekiela:Optimize-mb_substr
Open

kamil-tekiela wants to merge 1 commit into
php:masterfrom
kamil-tekiela:Optimize-mb_substr

Conversation

@kamil-tekiela

Copy link
Copy Markdown
Member

This PR adds a fast path for valid UTF-8 strings in mb_substr. It skips bytes in 256 chunks. The performance improvement is noticeable with longer strings and only ones that are flagged as valid UTF-8.

@alexdowad What do you think? Is this worth doing? I haven't got a fuzzer, but I don't anticipate any behavioural changes.

@alexdowad

Copy link
Copy Markdown
Contributor

@kamil-tekiela Fuzzing is 100% necessary. Otherwise, it is very, very easy for subtle bugs to slip through.

It's good that you optimized c < 0x80 || (c & 0xC0) != 0x80 to just (c & 0xC0) != 0x80; I think that change alone can go direct to master.

@kamil-tekiela

Copy link
Copy Markdown
Member Author

Do you have a fuzzer available? If not can you give me hints on how to build one?

@alexdowad

Copy link
Copy Markdown
Contributor

@kamil-tekiela When I have some time, I could fuzz this for you. Or, if you want to do it yourself, that might be a very good idea... fuzzing is an extremely powerful technique for finding bugs, and is an awesome tool for any programmer to know how to use.

I once wrote up my workflow for fuzzing new code in mbstring here: #10828 (comment)

@kamil-tekiela

Copy link
Copy Markdown
Member Author

I tried fuzzing as you said and it didn't find anything. Not sure I've done it properly, but when I intentionally introduced a bug it found it immediately.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants