Repository navigation
Fix +highlight worker bloat on large items - #2426
Merged
Merged
Conversation
Viewing a large item with +highlight can use hundreds of MB of worker memory per request. Measured locally on 450 KB of code: 8.2 s wall time, 123 MB tracemalloc peak and +211 MB worker RSS. Under concurrent crawler hits this grows workers to GBs until the OOM-killer finds them (see issue moinwiki#2307). The view sends the whole item through pygments and builds one DOM node per token, so cost grows with page size. Highlight at most MAX_HIGHLIGHT_SIZE (102400) characters, cutting at the last newline, and note on the page that the output is truncated. Small items render exactly as before. Feeding the input through pygments in chunks does not work, because lexers lose their state at each chunk boundary, so multi-line strings and comments split across chunks highlight wrongly; and the pipeline still builds the whole tree before serializing, so memory would not drop either. CPU stays the same because the whole text is still lexed. After the cap, the same page renders in 0.95 s with 32 MB peak and +2.7 MB RSS; small-page output is byte-identical. Addresses the +highlight half of upstream issue moinwiki#2307.
Contributor
|
Assuming from your description, that the main problem is related to use of pygments, what content type caused pygments to use such excessive amounts of memory? |
Contributor
Author
|
It was some wiki text document that was well over 100kB in size. |
Contributor
|
It would be interesting to see how much memory would be used if pygments wasn't installed and the plaint text alternative converter would be used instead when rendering very large wiki content. I'd suggest turning MAX_HIGHLIGHT_SIZE into a (defaulted) configuration parameter. This might also be done in a separate pull request. |
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.
Fix +highlight worker bloat on large items
Viewing a large item with +highlight can use hundreds of MB of worker
memory per request. Measured locally on 450 KB of code: 8.2 s wall
time, 123 MB tracemalloc peak and +211 MB worker RSS. Under concurrent
crawler hits this grows workers to GBs until the OOM-killer finds them
(see issue #2307).
The view sends the whole item through pygments and builds one DOM node
per token, so cost grows with page size.
Highlight at most MAX_HIGHLIGHT_SIZE (102400) characters, cutting at
the last newline, and note on the page that the output is truncated.
Small items render exactly as before.
Feeding the input through pygments in chunks does not work, because
lexers lose their state at each chunk boundary, so multi-line strings
and comments split across chunks highlight wrongly; and the pipeline
still builds the whole tree before serializing, so memory would not
drop either. CPU stays the same because the whole text is still lexed.
After the cap, the same page renders in 0.95 s with 32 MB peak and
+2.7 MB RSS; small-page output is byte-identical.
Addresses the +highlight half of upstream issue #2307.