Cache results of flycheck-goto-line to speed up overlay creation #1750
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Overlay creation can have significant costs in large buffers, even with a relatively small numbers of errors. The previous implementation of
flycheck-goto-line
counted lines from(point-min)
for each error, while this new implementation starts counting from the last position returned byflycheck-goto-line
, when possible. This is particularly valuable when errors are sorted by line, because we end up scanning the whole buffer at most once, instead of once per error.I wrote the following benchmark to measure the impact of this:
It reports a configurable number of overlays (default: 120) in a buffer containing the contents of
flycheck.el
(which we can hopefully agree is "reasonably" large, at ~12k lines ^^).Running the benchmark I get a fairly consistent 290ms with one GC (15ms) for the old version, and a fairly consistent 27-28ms with one GC (15ms) for the old version, so this patch provides a roughly 10x speedup on this example.
With a smaller number of errors (57, the number of errors currently in subr.el in the Emacs distribution) I get 160ms vs 27ms, and even with 15 errors I get 58ms vs 27ms. With a larger number of errors (400, the current threshold), I get 850ms vs 16ms (50x).
I'm not sure the fluctuations in the faster code (16 vs 27ms) are significant, but the speedup definitely is :)
I'm not a fan of the added complexity, but since this is a case where we're causing measurable Emacs pauses on even moderately-sized files I think it's worth considering.