Skip to content
This repository was archived by the owner on Dec 15, 2022. It is now read-only.

Optimize buffer search - #72

Merged
maxbrunsfeld merged 3 commits into
masterfrom
mb-optimize-scan
Jun 9, 2015
Merged

maxbrunsfeld merged 3 commits into
masterfrom
mb-optimize-scan

Conversation

@maxbrunsfeld

Copy link
Copy Markdown
Contributor
  • Get existing tests passing against the optimized implementation.
  • Add random tests for ::scanInRange() and ::backwardsScanInRange(), since their implementation is a little more complex now.
  • Make sure that packages that uses these methods (e.g. bracket-matcher) work in all cases.

Problem

The scanInRange() method allows the caller to stop the scan by calling the stop() function from within the scan callback. Currently though, we scan the entire buffer range and collect the matches in an Array before calling the callback. This causes scan operations to be slow for large files, even if the caller is only looking for the first match.

Here's a flame graph for moving the cursor to the end of the word in a 2.3MB file. There's a bunch of garbage collection pauses due to buffering the matches. The operation took 250ms.

screen shot 2015-06-08 at 2 31 43 pm

Solution

We call the scan callback as soon as we find each match, so that if stop() is called, we can avoid any further calls to RegExp.prototype.exec().

It's a little tricky for backwardsScanInRange(). Whereas before we simply scanned the entire range and then reversed the array of matches, we now need to break the range into chunks and scan each chunk. If no matches are found within a chunk, we need to expand the chunk backwards, in case matches straddle chunk boundaries.

Here's a flame graph for the same operation as above:

screen shot 2015-06-08 at 2 32 29 pm

It took 12ms. You can now hold down alt-right in a big file without locking the UI.

@maxbrunsfeld

Copy link
Copy Markdown
Contributor Author

This also improves bracket-matcher's performance when moving the cursor around a large HTML file. For a 6300 line file (the source of this page), BracketMatcherView::updateMatch used to take about 2 ms per call. Now it's down to about 0.7 ms.

@maxbrunsfeld

Copy link
Copy Markdown
Contributor Author

/cc @nathansobo

maxbrunsfeld pushed a commit that referenced this pull request Jun 9, 2015
@maxbrunsfeld
maxbrunsfeld merged commit 350a30c into master Jun 9, 2015
@maxbrunsfeld
maxbrunsfeld deleted the mb-optimize-scan branch June 9, 2015 21:14
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant