From 6bf2cca49d5aaa8cd71472e8daf0fc5ee67b7b0a Mon Sep 17 00:00:00 2001 From: g4lb Date: Thu, 19 Nov 2026 11:04:08 +0200 Subject: [PATCH] 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 --- git/diff.py | 6 ++++-- test/test_diff.py | 22 ++++++++++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/git/diff.py b/git/diff.py index 969ee027e..e83b18f1f 100644 --- a/git/diff.py +++ b/git/diff.py @@ -413,10 +413,12 @@ class Diff: the version in the index or tree, and hence has been modified. """ - # Precompiled regex. + # Precompiled regex. It starts with the literal and checks the line start with a + # lookbehind, so the regex engine can skip to each "diff --git" instead of trying + # the pattern at every position of the patch. re_header = re.compile( rb""" - ^diff[ ]--git + diff[ ]--git(?<=^diff[ ]--git) [ ](?P(?P"?[ab]/.+?"?) [ ](?P"?[ab]/.+?"?))\n (?:^old[ ]mode[ ](?P\d+)\n diff --git a/test/test_diff.py b/test/test_diff.py index abe41ac72..76f31369b 100644 --- a/test/test_diff.py +++ b/test/test_diff.py @@ -336,6 +336,28 @@ def test_empty_diff(self): Diff._handle_diff_line(b"", None, res) self.assertEqual(res, []) + def test_patch_headers_start_only_at_line_starts(self): + blob_id, null_id = b"1" * 40, b"0" * 40 + output = StringProcessAdapter( + b"diff --git a/fix.patch b/fix.patch\n" + b"new file mode 100644\n" + b"index " + null_id + b".." + blob_id + b"\n" + b"--- /dev/null\n" + b"+++ b/fix.patch\n" + b"@@ -0,0 +1,2 @@\n" + b"+diff --git a/x b/x\n" + b"+see diff --git a/y b/y\n" + b"diff --git a/z b/z\n" + b"deleted file mode 100644\n" + b"index " + blob_id + b".." + null_id + b"\n" + ) + diffs = Diff._index_from_patch_format(None, output) + self.assertEqual( + [(d.a_path, d.b_path, d.new_file, d.deleted_file) for d in diffs], + [(None, "fix.patch", True, False), ("z", "z", False, True)], + ) + 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") + def test_diff_initial_commit(self): initial_commit = self.rorepo.commit("root")