Repository navigation
Speed up array deletes and add_line() by updating only the affected indexes - #711
Open
deadstrobe5 wants to merge 1 commit into
Open
deadstrobe5 wants to merge 1 commit into
deadstrobe5 wants to merge 1 commit into
Conversation
|
🛡️ Human Attestation required. A human needs to confirm this PR once before it can merge. Attest this PR — sign in with GitHub, pass the challenge, and confirm one of the statements. |
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
Array.__delitem__(used bydel,pop()andremove()) andArray.add_line()rebuilt the whole position-to-group index map after every call, so emptying a long array withpop()or building one withadd_line()per item was quadratic. This applies the approach from #666 (append) to these two methods.After deleting a single item, only the entries after the removed position shift down by one. In
add_line()the new groups always land after every existing value (at the end, or just before a trailing whitespace group, which is never indexed), so only the new values get an index. Both shortcuts keep the same guard as #666: when the map length does not match the array, or for slice deletes, they fall back to_reindex().Same-process A/B on CPython 3.14, 7 alternating rounds, median:
pop()until empty, 2000 itemspop()until empty, 200 itemsadd_line()x 2000add_line()x 200pop()until empty, 10 itemsA differential fuzz of 3000 random edit sequences (pop, del with indexes and slices, remove, insert, append, add_line, setitem, extend, clear) on single-line, multiline, comment-heavy and comma-first arrays gave the same
as_string(), values, index map and exceptions after every step. The new tests delete and add lines in commented arrays and then index into them.Duplicate Check
__delitem__and Fix appends after standalone array comments #680 changes appends after comments; neither touches the index rebuild at the end of__delitem__or inadd_line().Agent Drafting Metadata