Repository navigation
fix: repeat and join build in one pass, not quadratically - #271
Merged
Merged
Conversation
`s * n` for str, bytes and bytearray (%pb-repeat), list and tuple (python-els-repeat), and str.join and bytes.join (%pb-join) appended each copy or piece to the END of what was built so far -- and appending to a list copies it. So the cost grew with the square of the result: `'a' * 8000` copied about 32 million pairs and passed the spec guard's 6 GB, where `'a' * 800` took 559 MB. Each repeat now puts the next copy on the FRONT, which copies only the copy (every copy is the same, so the order is unchanged), and join pushes each piece onto a reversed accumulator and turns it round once. `'a' * 8000` now peaks at 760 MB, and center/ljust/rjust, which fill through %pb-repeat, are linear with it. 41-string-repeat covers each kind at a size, with output recorded from CPython 3.14. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.
What
In x-python, building a long str or sequence by repetition, or with
join, cost memory that grew with the square of the result. On main,s = 'a' * 8000alone went past the spec guard's 6 GB, while'a' * 800peaked at 559 MB. I found this while writing the spec for #270, which had to read data files because it couldn't build long strings.Cause. A str, bytes or list here is a cons list. Three helpers added each new copy or piece at the end of the result so far, and appending to a cons list copies the whole list:
%pb-repeat(lib/python/bytes.x):*for str, bytes and bytearray, and the fill incenter/ljust/rjustpython-els-repeat(lib/python/runtime-seq.x):*for list and tuple%pb-join:str.joinandbytes.joinFix.
joinnow pushes each piece and separator onto a reversed accumulator, then reverses it once at the end.Effect.
'a' * 8000now peaks at 760 MB, where it was killed past 6 GB before.'ab' * 50000peaks at 1042 MB, and an 80,000-characterjoinat 521 MB.Checking
All runs are on x-lang v0.62.0.
41-string-repeat: one case with expected output recorded from CPython 3.14. It covers str, bytes, bytearray, list and tuple repetition, including zero and negative counts and the element identity that list repetition shares. It also covers str and bytesjoinand wide padding. It passes, with a peak footprint of 1817 MB. The spec gets its own file because the existing41-string-buildfile notes that a string-heavy batch builds up toward the object ceiling.class_setname_hazard_randandunicode_urenow pass. The other 10 failures are the same as on main.Not included
s += cin a loop still costs time and memory that grow with the square of the length. With a cons list for the str, each+copies the left side, and fixing that needs a different str representation.str.count: counting through 100,000 characters peaked at 4455 MB. That cost is in the search path, not in building the string.str.zfilldoesn't exist in x-python yet.🤖 Generated with Claude Code