Lazy code colorization - #1792
Open
skatkov wants to merge 17 commits into
Open
Conversation
skatkov
requested a deployment
to
fork-preview-protection
August 28, 2026 11:26 — with
GitHub Actions
Waiting
Mutating token_stream before materialization will raise NoMethodError. To avoid that we're routing all mutators through token_stream, this ensure that object is always materialized before mutation.
skatkov
requested a deployment
to
fork-preview-protection
August 28, 2026 11:40 — with
GitHub Actions
Waiting
…ource release, and thread safety
skatkov
requested a deployment
to
fork-preview-protection
August 28, 2026 12:14 — with
GitHub Actions
Waiting
skatkov
requested a deployment
to
fork-preview-protection
August 28, 2026 12:46 — with
GitHub Actions
Waiting
skatkov
requested a deployment
to
fork-preview-protection
August 28, 2026 13:43 — with
GitHub Actions
Waiting
skatkov
requested a deployment
to
fork-preview-protection
August 28, 2026 13:57 — with
GitHub Actions
Waiting
skatkov
requested a deployment
to
fork-preview-protection
August 28, 2026 14:27 — with
GitHub Actions
Waiting
skatkov
requested a deployment
to
fork-preview-protection
August 28, 2026 14:41 — with
GitHub Actions
Waiting
skatkov
marked this pull request as ready for review
August 28, 2026 14:49
Contributor
Author
|
@tompng this is available for review. What's your opinion on this approach? |
skatkov
requested a deployment
to
fork-preview-protection
August 28, 2026 22:35 — with
GitHub Actions
Waiting
skatkov
requested a deployment
to
fork-preview-protection
August 28, 2026 22:37 — with
GitHub Actions
Waiting
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.
Please see the prior PR #1788
In that PR it was correctly established that eager generation of
RDoc::Parser::RubyColorizer::ColoredTokenobjects creates a heavy performance/system burden for all generators, but not all generators need these objects.My suggested approach in that PR was to establish a generator property that would stop this eager code colorization process. This approach is not functional, because rdoc itself can switch generators, but use the same @store object.
In this PR I want to suggest an alternative solution:
Solution
This change stores a copied method-source slice in a deferred token stream and materializes the existing mutable token array only when token_stream is accessed. It also switches the initial file scan from Prism.parse_lex to Prism.parse, preserves indentation and heredoc boundaries, and keeps the existing public token-stream behavior for HTML and third-party generators.
Benchmark
I have done a benchmark on google-client-api gem (one of the biggest and popular gems in the ecosystem). This could be found here:
https://gist.github.com/skatkov/7e509742651586c1b993a8b15b10a8da
RI
After Parse
After Generate
Parse RSS change: -50.1%
Generated RSS change: -47.0%
Peak RSS change: -45.7%
Parse time change: -39.8%
Generate time change: -5.9%
Total time change: -21.9%
Outputs identical: yes
Aliki, 100 files
After Parse
After Generate
Parse RSS changed by -32.1%, generated RSS by -4.0%, peak RSS by -4.9%,
parse time by -35.7%, generation time by +1.1%, and total time by -0.1%.
Outputs were identical.
Results
According to the benchmark, we can see that the lazy approach brings significant memory improvements even for generators that actually output highlighted code.
In cases of 'RI', generator that has no use for highlighted code, we can see significant memory improvements (~50%), but also ~20% speed improvements.