Open
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
I'm using this library to highlight code in a Terminal app.
As my use case uses it extensively, and sometimes on very big PHP files (~5000 lines), I noticed that this was the main bottleneck of my app. So, I've used Blackfire to investigate the code that contributed the most to the slowness and found two pretty significant potential improvements.
On a 5000 line PHP file, I went from 5.5s to 150ms on my laptop!
Commit 1: Hash-based token dedup in ParseTokens
tokenAlreadyPresent()did an O(n) linear scan through all existing tokens for each new token, resulting in O(n²) behavior. I've replaced with a$seenhash map keyed byoffset:tokenTypeValue:value, making each dedup check O(1).Commit 2: Sorted-scan token grouping in GroupTokens
The grouping algorithm compared every token against every other token (O(n²)), resulting in many
containsOrOverlaps()/equals()calls. Since tokens are already sorted by start position, the inner loop now only scans forward and breaks when compareToken->start >= token->end (no more overlaps possible). This turns O(n²) into effectively O(n) for typical non-overlapping token distributions.