caxy / php-htmldiff

A library for comparing two HTML files/snippets and highlighting the differences using simple HTML. Includes support for comparing complex lists and tables
http://php-htmldiff.caxy.com
GNU General Public License v2.0
202 stars 51 forks source link

Performance enhancements. Rebuild the word parser and replace the whitespace checker in the match finder. #102

Closed SavageTiger closed 3 years ago

SavageTiger commented 3 years ago

Description

While investigating if I could improve the performance for this PR https://github.com/caxy/php-htmldiff/issues/101 I stumbled upon two bottlenecks.

1. The html to word parser was a big for loop that walked over every character individually

The code was really complex, and hard to comprehend. It was a huge loop with loads of code-flow inside that was affected by what character was being processed, what character was processed previously, and what process was comming up.

I replace it by mostly regex parsing that does the heavy lifting, speeding this method up by 98%

2. Whitespace checking was resource intensive

While finding blocks we have todo a whitespace check of part of the old sentence, this is done loads of times. This is done by referencing the oldWords array, and temporarily making a string from part of that array, and then checking if that string is only whitespace.

A couple of years ago I already added caching here, to speed up the algorithm allot, but I took some more time to investigate if this can be improved further.

I have replaced this by a loop that iterates over the part of the sentence item by item, and when one of the items is not a space (usually the first item), it immediately reports false and caching the result, speeding up the algorithm in some cases by up to 50%