Skip to content

Commit 47b01a1

Browse files
committed
Code optimization and UnitTest for performance
1 parent 700175d commit 47b01a1

File tree

2 files changed

+26
-5
lines changed

2 files changed

+26
-5
lines changed

tests/test_patch.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from whatthepatch.patch import Change, diffobj, header as headerobj
55

66

7+
import time
78
import unittest
89
import os
910

@@ -1420,6 +1421,27 @@ def test_svn_mixed_line_ends(self):
14201421
results = list(wtp.patch.parse_patch(text))
14211422
self.assertEqual(results[0].header, expected_header)
14221423

1424+
def test_huge_path(self):
1425+
start_time = time.time()
1426+
text = """diff --git a/huge.file b/huge.file
1427+
index 0000000..1111111 100644
1428+
--- a/huge.file
1429+
+++ a/huge.file
1430+
@@ -3,13 +3,1000007 @@
1431+
00000000
1432+
11111111
1433+
22222222
1434+
-33333333
1435+
-44444444
1436+
+55555555
1437+
+66666666
1438+
"""
1439+
for n in range(0, 1000000):
1440+
text += "+" + hex(n) + "\n"
1441+
result = list(wtp.patch.parse_patch(text))
1442+
self.assertEqual(1, len(result))
1443+
self.assertEqual(1000007, len(result[0].changes))
1444+
self.assertGreater(10, time.time() - start_time)
14231445

14241446
if __name__ == "__main__":
14251447
unittest.main()

whatthepatch/patch.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -582,6 +582,8 @@ def parse_unified_diff(text):
582582
new = 0
583583
r = 0
584584
i = 0
585+
old_len = 0
586+
new_len = 0
585587

586588
changes = list()
587589

@@ -609,12 +611,11 @@ def parse_unified_diff(text):
609611
h = None
610612
break
611613

612-
while len(hunk) > 0:
613-
c = unified_change.match(hunk[0])
614+
for n in hunk:
615+
c = unified_change.match(n)
614616
if c:
615617
kind = c.group(1)
616618
line = c.group(2)
617-
c = None
618619

619620
if kind == "-" and (r != old_len or r == 0):
620621
changes.append(Change(old + r, None, line, hunk_n))
@@ -628,8 +629,6 @@ def parse_unified_diff(text):
628629
r += 1
629630
i += 1
630631

631-
del hunk[0]
632-
633632
if len(changes) > 0:
634633
return changes
635634

0 commit comments

Comments
 (0)