Skip to content

Commit 6bf2cca

Browse files
committed
perf(diff): let the patch header regex skip to diff --git lines
`Diff._index_from_patch_format()` finds file headers with `Diff.re_header.finditer()` over the whole patch text. The pattern started with `^`, so CPython's regex engine had no literal prefix and tried the full header pattern at every byte of the patch, including all hunk content. On large patches this scan was a large part of the Python time of `Diffable.diff(..., create_patch=True)`. The pattern now starts with the literal `diff --git` and checks the line start with the lookbehind `(?<=^diff[ ]--git)`. A match still needs `diff --git` at the start of a line, so the matches, their spans and all groups stay the same, but the engine now searches for the literal and tries the pattern only there. No Git command changes, and the Gix backend is not affected. A new test pins that `diff --git` inside hunk lines does not start a file. The change only affects speed, so no test can fail without it. Validation on macOS (Apple M5), CPython 3.12, Git 2.54.0, 15 interleaved rounds and a Mann-Whitney test: - `_index_from_patch_format()` on a 7.7 MB patch of 10,001 new files: 60.3 ms -> 45.9 ms; on a patch that adds a file of `diff --git` lines: 521 us -> 179 us. Empty, one-line, CRLF, mode-only and binary patches showed no significant change (at most +0.5%, p > 0.05, 30 rounds). CPython 3.8 and 3.15 showed the same gains and no significant slowdown. - `Commit.diff(NULL_TREE, create_patch=True)` for the 10,001 files: 166.5 ms -> 151.4 ms (-9%); GitPython 3.1.40 to `main` (343 files, 3.1 MB): 107.8 ms -> 99.9 ms (-7%); one commit of 61 files (78 KB): 18.9 ms -> 18.7 ms (-1.3%, not significant), because Git dominates small diffs. - `import git` time did not change; compiling the pattern takes about 8 us more. - Old and new code gave the same `Diff` objects and errors for 1,023,000 random, malformed and real inputs. - `pytest -m 'not net'` on the CLI backend, `test/test_diff.py` on the Gix backend, `ruff check`, `ruff format --check` and mypy pass. Agent-Model: claude-opus-5-5
1 parent d6337df commit 6bf2cca

2 files changed

Lines changed: 26 additions & 2 deletions

File tree

‎git/diff.py‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -413,10 +413,12 @@ class Diff:
413413
the version in the index or tree, and hence has been modified.
414414
"""
415415

416-
# Precompiled regex.
416+
# Precompiled regex. It starts with the literal and checks the line start with a
417+
# lookbehind, so the regex engine can skip to each "diff --git" instead of trying
418+
# the pattern at every position of the patch.
417419
re_header = re.compile(
418420
rb"""
419-
^diff[ ]--git
421+
diff[ ]--git(?<=^diff[ ]--git)
420422
[ ](?P<header_names>(?P<a_path_fallback>"?[ab]/.+?"?)
421423
[ ](?P<b_path_fallback>"?[ab]/.+?"?))\n
422424
(?:^old[ ]mode[ ](?P<old_mode>\d+)\n

‎test/test_diff.py‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,28 @@ def test_empty_diff(self):
336336
Diff._handle_diff_line(b"", None, res)
337337
self.assertEqual(res, [])
338338

339+
def test_patch_headers_start_only_at_line_starts(self):
340+
blob_id, null_id = b"1" * 40, b"0" * 40
341+
output = StringProcessAdapter(
342+
b"diff --git a/fix.patch b/fix.patch\n"
343+
b"new file mode 100644\n"
344+
b"index " + null_id + b".." + blob_id + b"\n"
345+
b"--- /dev/null\n"
346+
b"+++ b/fix.patch\n"
347+
b"@@ -0,0 +1,2 @@\n"
348+
b"+diff --git a/x b/x\n"
349+
b"+see diff --git a/y b/y\n"
350+
b"diff --git a/z b/z\n"
351+
b"deleted file mode 100644\n"
352+
b"index " + blob_id + b".." + null_id + b"\n"
353+
)
354+
diffs = Diff._index_from_patch_format(None, output)
355+
self.assertEqual(
356+
[(d.a_path, d.b_path, d.new_file, d.deleted_file) for d in diffs],
357+
[(None, "fix.patch", True, False), ("z", "z", False, True)],
358+
)
359+
self.assertEqual(diffs[0].diff, b"@@ -0,0 +1,2 @@\n+diff --git a/x b/x\n+see diff --git a/y b/y\n")
360+
339361
def test_diff_initial_commit(self):
340362
initial_commit = self.rorepo.commit("root")
341363

0 commit comments

Comments
 (0)