-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Optimize PHP html_entity_decode function #18092
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
Optimize PHP html_entity_decode function #18092
Conversation
d166abe
to
66f5709
Compare
@Girgias are you going to review the logic as well? Just checking if I should look into this or if you are happy to handle it all? |
Please do review the logic, I only had a cursory glance :) |
Ok I will check it out next week if no one is quicker. |
9b3e96d
to
f093c30
Compare
Nice idea @ArtUkrainskiy :-) 👍 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it looks reasonable except that introduction of valid_entity
boolean and checking that everywhere which doesn't look like performance optimization to me. I understand that it was probably meant to make code more readable but not sure if it's worth it in this case. Might be worth to check if it has any impact.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks better. The comments are pretty much only for minor issue / optimizations. Overall I think it looks good.
I went through this again and think it looks good. Doesn't make sense to hold it because of few NITs which I can easily address during the merge. I will try do a bit of testing in about two weeks time and merge it then. |
Optimize scanning for '&' and ';' using memchr. Use memcpy instead of character-by-character copying language. Closes phpGH-18092
5f8363b
to
10589dc
Compare
I have done a bit of testing. Also fixed few nits and squash / rebased it so think it should be good enough. I will do one last round of testing in a couple of weeks and if I don't find anything, I will merge it. |
I did a bit more checking and it seems all fine so merged to master. Thanks for the contribution! |
Improvements affect the C function
traverse_for_entities
:memchr
to search for '&' instead of scanning character by character.memchr
to locate ';' to determine potential entity boundaries instead ofprocess_named_entity_html
, avoiding unnecessary per-character validations.memcpy
instead of character-by-character copying.Benchmark branch - https://github.com/ArtUkrainskiy/php-src/tree/html_entity_decode/benchmark
As you can see, the speedup depends on the number of entities and
&
characters in the string — the fewer there are, the more noticeable the performance improvement.In edge cases, where the string consists entirely of
&
characters or valid HTML entities, performance actually worsens. However, I don't think this is a common scenario.Either way, I plan to continue optimizing and implement
&
scanning using SIMD instructions, which should significantly improve performance even in high-entity-density cases.