Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update prism code to not do all matching recursively #197

Merged
merged 1 commit into from
Jul 4, 2016

Conversation

ccampbell
Copy link
Owner

@ccampbell ccampbell commented Jul 4, 2016

Most browsers have a limit to the number of recursive function calls that can exist in the stack. That can lead to problems when processing very large code blocks. This is also true when using global regex patterns.

See https://bugs.chromium.org/p/v8/issues/detail?id=3878 for more information about that.

This change updates most of the regex matching to no longer be recursive.

Previously inside of the _processPattern function it would call itself recursively until all the regex matches were complete. This works pretty well for small blocks of code, but does not work at all for large blocks of code. Regexp.exec with global regex patterns also has to keep track of its own internal state and what it is matching against.

This change updates it to work like this:

  1. Regex pattern is cloned to create a new non-global regex pattern
  2. Regex pattern is matched against the code string
  3. If there is no match the function returns false
  4. If there is a match the function chops off what was matched from the beginning of the string and returns back the remaining part of the string and the offset position.
  5. The function is called from outside of itself using a while loop until there are no more matches

This is basically simulating a global regex without using a global regex and it allows Rainbow to process much larger blocks of code than previously. It also seems to make all highlighting a lot faster, but I have not benchmarked it.

Fixes #123
Fixes #57

@ccampbell ccampbell merged commit a0ffcf6 into master Jul 4, 2016
@ccampbell ccampbell deleted the recursion-fix branch July 4, 2016 16:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant