Repository navigation
Commit 6bf2cca
committed
perf(diff): let the patch header regex skip to
`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-5diff --git lines1 parent d6337df commit 6bf2cca
2 files changed
Lines changed: 26 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
413 | 413 | | |
414 | 414 | | |
415 | 415 | | |
416 | | - | |
| 416 | + | |
| 417 | + | |
| 418 | + | |
417 | 419 | | |
418 | 420 | | |
419 | | - | |
| 421 | + | |
420 | 422 | | |
421 | 423 | | |
422 | 424 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
336 | 336 | | |
337 | 337 | | |
338 | 338 | | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
339 | 361 | | |
340 | 362 | | |
341 | 363 | | |
| |||
0 commit comments