Skip to content

Performance optimizations#183

Open
fabpot wants to merge 2 commits intotempestphp:mainfrom
fabpot:perf-optimization
Open

Performance optimizations#183
fabpot wants to merge 2 commits intotempestphp:mainfrom
fabpot:perf-optimization

Conversation

@fabpot
Copy link

@fabpot fabpot commented Feb 6, 2026

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 $seen hash map keyed by offset: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.

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