Eager on pathologically oversized string literals (#478)#479
Open
mgajda wants to merge 1 commit intohaskell-suite:masterfrom
Open
Eager on pathologically oversized string literals (#478)#479mgajda wants to merge 1 commit intohaskell-suite:masterfrom
mgajda wants to merge 1 commit intohaskell-suite:masterfrom
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.
Summary
Addresses #478 — heap-usage explosion when
parseModuleWithCommentsencounters a multi-MB single-line string literal of\NN-escaped bytes (reported as ~95× source size).Three surgical changes in
InternalLexer.hs, no API or dependency changes:lexString's accumulator tuple and add bang patterns (loop !s !raw). The originalloop (s,raw)built a chain of lazy tuple thunks, one per input character.reverse xs ++ yswith a local tail-recursiverevAppendin the two escape-handling branches. Avoids allocating an intermediate reversed list plus a++thunk.parseInteger. Thefoldl1 (\n d -> n*radix + d)built a deep chain of Integer-multiplication thunks for long numeric literals; rewritten asgo !acc.Measurements
Pathological input: 2.9 MB Haskell source, one string literal of 1 M
\25escapes. GHC 9.10.3,+RTS -s,/usr/bin/time -v, best of 3:−20 % max residency, −24 % RSS, −17 % wall time. 15 insertions / 8 deletions, one file.
This does not fully close #478 — the remaining 304 MB peak is dominated by
(:)cons cells inherent to theStringrepresentation of the AST (~168 MB at peak by-hTprofile). The principled fix is to carryText/ByteStringinStringTok/Literal, which is a breaking change and properly belongs to a future major release. This PR is the non-breaking portion worth shipping now.