[draft] discuss: ListProxy.reverse patch minimality#39
Closed
berendkleinhaneveld wants to merge 1 commit into
Closed
[draft] discuss: ListProxy.reverse patch minimality#39berendkleinhaneveld wants to merge 1 commit into
berendkleinhaneveld wants to merge 1 commit into
Conversation
Adds tests that lock in the current N-patch shape (and N-1 for odd N,
since record_replace already filters the middle no-op), plus round-trip
coverage for n in {0,1,2,3,4,5,10}. No behavior change.
Opening this as a draft to surface the design question: a comment in
the existing code calls the per-position replaces "redundant", but with
JSON Patch replace semantics each position genuinely needs its own
patch — dropping half leaves those slots untouched on apply. See PR
body for the analysis.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
Collaborator
Author
|
Closing as won't-fix per author decision. Recap of the analysis (full detail in the PR body):
Leaving the implementation as-is. Branch will be deleted. |
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
ListProxy.reverse()currently emits up toNreplacepatches for anN-element list. The original code comment frames this as 2× the patches needed (with positionsiandN-1-itreated as a swap pair). This PR pins the current semantics with tests and asks: keep, change, or close?This PR is tests only — no production behavior change — to keep the discussion focused on the design call.
Patch count, current vs. alternatives
N=4(distinct values)N=5range(N), singlereplaceper slotrange(N // 2), tworeplaces per swapThe "drop half" row is the one the existing in-code comment seems to imagine, but I couldn't make it work. Quick proof on
master:With
replacesemantics each destination slot needs its own patch — there's no way to make positionsiandN-1-icover for each other. RFC 6902 hasmove, which could swap with twomoves via a scratch slot, butapply.pyhere doesn't implementmoveand the plan explicitly excludes new op types.So the realistic options are both
Npatches, just in different iteration orders. The plan's "minimal" sketch (range(N // 2)with tworecord_replacecalls per iteration) ends up emitting the same count — the parenthetical in the plan acknowledges this: "Both positions in the swap need a patch — you can't represent the swap with one replace because the value at j is the missing piece for the patch at i."Consequences for
apply/ memorySince the patch count is unchanged regardless of which loop shape we pick, there's no measurable
applyruntime or memory delta between the two iteration styles. The forward and reverse patch lists are the same length and touch the same slots.What this PR does
test_list_reverse_emits_n_patches_for_even_n— pinsNpatches forN=4with distinct values.test_list_reverse_emits_n_minus_one_patches_for_odd_n— documents that the odd-Nmiddle is filtered to a no-op byrecord_replace.test_list_reverse_round_tripparametrized overn ∈ {0, 1, 2, 3, 4, 5, 10}— forward and reverse both round-trip.applyin the test module.No changes to
patchdiff/produce.py.Question for the author
Three reasonable directions:
Npatches is the floor forreplace-only output, and the current loop already hits it. The pinning tests make that explicit.range(N // 2)with two emits per iteration. Same patch count, arguably more readable ("reverse isN//2swaps"). Cosmetic.My weak recommendation is (1): the tests are worth keeping either way, and they document the floor so future readers don't go chasing the same "redundancy" thread.
Land minimal, keep current, or close as won't-fix?
Test plan
uv run pytest tests/test_produce_list.py -v— 84 passeduv run pytest -x -q— 277 passeduv run ruff check— cleanuv run ruff format --check— clean🤖 Generated with Claude Code