perf: use str.replace in Pointer escape/unescape#37
Merged
Conversation
Replaces the regex-based escape helpers in patchdiff/pointer.py with str.replace. For single-character substitutions this is ~3x faster than a compiled regex, and Pointer.__str__ calls escape() per token on every serialization. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
Korijn
approved these changes
May 26, 2026
…o perf/pointer-escape-str-replace
Merged
berendkleinhaneveld
added a commit
that referenced
this pull request
May 26, 2026
This version includes the following: * ci: Update versions of steps and let setup-uv manage the python version (#40) * perf: trim common prefix/suffix in diff_lists (#36) * perf: use str.replace in Pointer escape/unescape (#37) * fix: produce() snapshots mutable values when recording patches (#34) * perf: linear reverse-op construction in diff_dicts/diff_sets (#38) * fix: iapply raises on invalid patch paths (#35) * fix: to_json mutating caller's ops (#33)
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
Replaces the regex-based
escape/unescapehelpers inpatchdiff/pointer.pywithstr.replace. For single-character substitutions,str.replaceis ~3× faster than a compiled regex, andPointer.__str__callsescape(str(t))for every token in the pointer — so everyto_json/to_str_pathscall benefits.The four module-level
re.compileobjects (slash_re,tilde_re,tilde0_re,tilde1_re) andimport reare removed.grepconfirms no other file in the repo imports those names.Micro-bench (reproducer from plan)
escape100kunescape100kmaster(Target was ≥2× on
escape.)Macro-bench
uv run pytest benchmarks/benchmark.py --benchmark-only -k "pointer"—pointer-appendandpointer-evaluategroups unchanged, as expected (escape only runs on serialization, not append/evaluate):Correctness
test_escape_unescape_roundtripintests/test_pointer.pycovering:'','plain','has/slash','has~tilde','~/mix','///','~~~', and the tricky'~01'(which must escape to'~001', not be confused with the RFC 6901~0+'1'sequence). For each token the test asserts bothescape(t)matches RFC 6901 andunescape(escape(t)) == t.RFC 6901 escape order (
~→~0before/→~1on escape; reverse on unescape) is preserved.Test plan
uv run pytest tests/test_pointer.py -vuv run pytest -x -quv run pytest benchmarks/benchmark.py --benchmark-only -k "pointer"uv run ruff checkuv run ruff format --check🤖 Generated with Claude Code