-
-
Can we make this an argument to
_diff_line
? I don't like using 'self' to pass arguments. Same with_cur_meta
, actually. -
I'm not sure I like the new code. It's very verbose. If you wanted to avoid unnecessary string compares, you could chain the state:
is_equal = (change == 'equal') is_replace = (not is_equal) and (change == 'replace') is_insert = (not is_replace) and (change == 'insert') is_delete = (not is_insert) and (change == 'delete')
Alternatively, we could have an enum for the chunk type, and use a dict to map from strings to that. Then the code could change from
if is_equal:
to something likeif chunk_type is EQUAL:
chipx86 got a fish trophy!
Improve performance when rendering diff lines.
Review Request #5555 — Created Feb. 27, 2014 and submitted
This makes a number of optimizations to our diff rendering process:
Some unnecessary string comparisons from the diff_lines template tag
have been simplified, so that we're only ever comparing one string.
Small change, but one that should add up over the course of diffs.
From performance testing, this appears to be roughly 30% faster
overall.We were checking if we needed to highlight regions for every
single line, not just replace lines. This meant we did empty string
checks, length checks, and string comparisons for every single line.
These also added up, even though it only ever triggered the
highlight region code for replace lines.Instead of all this work, we now check if it's a replace line
up-front, preventing all the other checks.
- Highlighting regions for a replace line is now twice as fast.
Instead of parsing every character and keeping track of state,
we're making use ofstr.find
, which is much faster. We're also
grabbing ranges of a string at a time, and reducing accesses to
indexes of regions.
All unit tests pass. Viewed some diffs, and they worked.
Ran a series of performance tests. The following correspond to the itemized
tasks above:
Made a test script for the comparisons and ran it 2,000,000 times.
The old code took 1.2 seconds. The new code took 0.84 seconds.Made a test script simulating comparisons for 200 equal lines, 50 inserts,
20 deletes, and 40 replaces. Ran this 20,000 times. Without the "replace"
check, it took 2.65 seconds. With the check, it took 1.85 seconds.Another test script for
highlightregion
. Ran it on a fairly standard set
of replaces, 200,000 times. The old code took 6.52 seconds. The new code
took 3.38 seconds.