| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
Some diffs result in the semantic alignment loop being run many times. This happens when comparing a file containing a long chunk of characters with a similar file containing the same long chunk of characters twice in succession. Manipulating indexes rather than creating new strings at each iteration makes the loop run much more quickly.
| Back | FazBrowse Home | New Git URL |
In the Javascript version, when using diff_cleanupSemantic(), some diffs result in the semantic alignment loop being run many times. This happens when comparing a file containing a long chunk of characters with a similar file containing the same long chunk of characters twice in succession, i.e.:
File 1: <chunk A><chunk B><chunk C>
FIle 2: <chunk A><chunk B><chunk B><chunk C>
When this happens, the loop runs as many times as there are characters in chunk B. This can get quite expensive because three new strings are created in every iteration. This PR replaces these with faster index manipulations.
I tried to translate the algorithm line by line with the objective of changing nothing in its behaviour. Basically, instead of tracking 3 strings (equality1, edit, equality2), I track 1 string (buffer) and 2 indices (editStart, editEnd). They are related this way:
buffer: | equality1 | edit | equality2 | ^ ^ editStart ---+ +---- editEndThe other change I made was to change the loop condition. The original code shifts the edit left as much as possible (using the common suffix between equality1 and edit) and shifts right until the first character of edit and equality2 are different. I changed that to counting the common prefix between edit and equality2 and adding it to the amount of right shift to get the total number of shifts required.
I used the following benchmark to force the loop to run an arbitrary number of times:
Here are the timings I got for diff_cleanupSemantic() before and after this PR.