Repository navigation
BUG: Avoid excessive temporary memory in StringDType slice and replace - #33241
Merged
Merged
Conversation
ngoldbaum
force-pushed
the
strings-slice-replace-memory
branch
from
November 7, 2026 00:50
595fe04 to
b8482ec
Compare
MaanasArora
approved these changes
Nov 16, 2026
MaanasArora
left a comment
Contributor
There was a problem hiding this comment.
Thanks Nathan, LGTM!
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.
PR summary
Slicing a
StringDTypestring builds a temporary array containing one pointer per character, even when selecting just one character. On a 64-bit system, this requires roughly eight bytes per character before accounting for allocation overhead.This PR walks the string directly instead of building the temporary array. This also removes the uncaught
std::bad_allocfailure caused by growing the temporary vector.Replacing an empty pattern in a
StringDTypestring allocates a temporary large enough to insert the replacement at every position, even withcount=1. For example, prepending"prefix: "to a 1 MiB ASCII string reserves about 9 MiB for that temporary. The result grows by just eight bytes.This PR calculates the actual result size using the replacement count and writes directly into the output when possible. The new implementation keeps a temporary only when needed to handle shared storage safely.
I refactored both operations to move the slicing and replace logic into helpers. The algorithms are completely re-written so this ends up being a clearer diff too. I think the resulting structure is clearer.
I also added a memory-limit test that exercises the string operations in the existing unary, binary, and comparison tables with a ~1 MB string operand. Each operation runs in a separate child process with a small additional virtual-memory allowance after input creation. This catches excessive peak allocations. A coverage check keeps the function tables in sync with the
np.stringsAPI. Operations with known excessive allocations through Python string methods are explicitly excluded from the memory test.towards fixing #33240
AI Disclosure
I used an AI model to work on this.