fix(string.rep): allocate proportionally to result, not repeat count#376
Open
smn wants to merge 1 commit into
Open
fix(string.rep): allocate proportionally to result, not repeat count#376smn wants to merge 1 commit into
smn wants to merge 1 commit into
Conversation
`string.rep` built its result with `Enum.map_join(1..n, sep, fn _ -> str end)`, which materializes an n-element list (plus its iolist) as transient garbage. For a large repeat count that intermediate is tens of times the size of the result itself, so producing an otherwise-modest string spikes the process heap. This is what issue tv-labs#373 observes: returning a 16 MB string built by `string.rep` through `Lua.call_function!/3` left ~1 GB of reachable garbage (call_function returns before the next GC sweeps it), and on a heap-limited process the spike trips `max_heap_size` and kills it. The same spike occurs on the `Lua.eval!/2` path; it is just masked there because the surrounding decode/wrap work triggers a GC before the caller measures. Build the binary in a single pass with `:binary.copy/2` so allocation is proportional to the output. The pre-build `Limits.check_string_size!` guard is unchanged, so oversized requests still raise catchably without allocating. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01NvaxgWQcfipbKBaU4KxnuW
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.
Fixes #373
full disclosure: Claude helped here