Mercurial > public > mercurial-scm > hg
comparison tests/test-annotate.py @ 37064:434e520adb8c
annotate: do not construct attr.s object per line while computing history
Unfortunately, good abstraction has a cost. It's way slower to construct
an annotateline() object than creating a plain tuple or a list. This patch
changes the internal data structure from row-based to columnar, so the
decorate() function can be instant (i.e. no Python in hot loop.)
For code readability, the outermost tuple is switched to an attr.s object
instead.
(original, row-based attr.s)
$ hg annot mercurial/commands.py --time > /dev/null
time: real 11.470 secs (user 11.400+0.000 sys 0.070+0.000)
$ hg annot mercurial/commands.py --time --line-number > /dev/null
time: real 39.590 secs (user 39.500+0.000 sys 0.080+0.000)
(this patch, columnar)
$ hg annot mercurial/commands.py --time > /dev/null
time: real 11.780 secs (user 11.710+0.000 sys 0.070+0.000)
$ hg annot mercurial/commands.py --time --line-number > /dev/null
time: real 12.240 secs (user 12.170+0.000 sys 0.090+0.000)
(cf. 4.3.3, row-based tuple)
$ hg annot mercurial/commands.py --time --line-number > /dev/null
time: real 19.540 secs (user 19.460+0.000 sys 0.080+0.000)
author | Yuya Nishihara <yuya@tcha.org> |
---|---|
date | Mon, 12 Mar 2018 20:45:10 +0900 |
parents | ec46b0ee2e3c |
children | 2372284d9457 |
comparison
equal
deleted
inserted
replaced
37063:39304dd63589 | 37064:434e520adb8c |
---|---|
3 | 3 |
4 import unittest | 4 import unittest |
5 | 5 |
6 from mercurial import ( | 6 from mercurial import ( |
7 mdiff, | 7 mdiff, |
8 pycompat, | |
8 ) | 9 ) |
9 from mercurial.dagop import ( | 10 from mercurial.dagop import ( |
10 annotateline, | 11 annotateline, |
12 _annotatedfile, | |
11 _annotatepair, | 13 _annotatepair, |
12 ) | 14 ) |
15 | |
16 def tr(a): | |
17 return [annotateline(fctx, lineno, skip) | |
18 for fctx, lineno, skip in zip(a.fctxs, a.linenos, a.skips)] | |
13 | 19 |
14 class AnnotateTests(unittest.TestCase): | 20 class AnnotateTests(unittest.TestCase): |
15 """Unit tests for annotate code.""" | 21 """Unit tests for annotate code.""" |
16 | 22 |
17 def testannotatepair(self): | 23 def testannotatepair(self): |
24 p2data = b'a\nc\nd\n' | 30 p2data = b'a\nc\nd\n' |
25 childdata = b'a\nb2\nc\nc2\nd\n' | 31 childdata = b'a\nb2\nc\nc2\nd\n' |
26 diffopts = mdiff.diffopts() | 32 diffopts = mdiff.diffopts() |
27 | 33 |
28 def decorate(text, fctx): | 34 def decorate(text, fctx): |
29 return ([annotateline(fctx=fctx, lineno=i) | 35 n = text.count(b'\n') |
30 for i in range(1, text.count(b'\n') + 1)], | 36 linenos = pycompat.rangelist(1, n + 1) |
31 text) | 37 return _annotatedfile([fctx] * n, linenos, [False] * n, text) |
32 | 38 |
33 # Basic usage | 39 # Basic usage |
34 | 40 |
35 oldann = decorate(olddata, oldfctx) | 41 oldann = decorate(olddata, oldfctx) |
36 p1ann = decorate(p1data, p1fctx) | 42 p1ann = decorate(p1data, p1fctx) |
37 p1ann = _annotatepair([oldann], p1fctx, p1ann, False, diffopts) | 43 p1ann = _annotatepair([oldann], p1fctx, p1ann, False, diffopts) |
38 self.assertEqual(p1ann[0], [ | 44 self.assertEqual(tr(p1ann), [ |
39 annotateline(b'old', 1), | 45 annotateline(b'old', 1), |
40 annotateline(b'old', 2), | 46 annotateline(b'old', 2), |
41 annotateline(b'p1', 3), | 47 annotateline(b'p1', 3), |
42 ]) | 48 ]) |
43 | 49 |
44 p2ann = decorate(p2data, p2fctx) | 50 p2ann = decorate(p2data, p2fctx) |
45 p2ann = _annotatepair([oldann], p2fctx, p2ann, False, diffopts) | 51 p2ann = _annotatepair([oldann], p2fctx, p2ann, False, diffopts) |
46 self.assertEqual(p2ann[0], [ | 52 self.assertEqual(tr(p2ann), [ |
47 annotateline(b'old', 1), | 53 annotateline(b'old', 1), |
48 annotateline(b'p2', 2), | 54 annotateline(b'p2', 2), |
49 annotateline(b'p2', 3), | 55 annotateline(b'p2', 3), |
50 ]) | 56 ]) |
51 | 57 |
52 # Test with multiple parents (note the difference caused by ordering) | 58 # Test with multiple parents (note the difference caused by ordering) |
53 | 59 |
54 childann = decorate(childdata, childfctx) | 60 childann = decorate(childdata, childfctx) |
55 childann = _annotatepair([p1ann, p2ann], childfctx, childann, False, | 61 childann = _annotatepair([p1ann, p2ann], childfctx, childann, False, |
56 diffopts) | 62 diffopts) |
57 self.assertEqual(childann[0], [ | 63 self.assertEqual(tr(childann), [ |
58 annotateline(b'old', 1), | 64 annotateline(b'old', 1), |
59 annotateline(b'c', 2), | 65 annotateline(b'c', 2), |
60 annotateline(b'p2', 2), | 66 annotateline(b'p2', 2), |
61 annotateline(b'c', 4), | 67 annotateline(b'c', 4), |
62 annotateline(b'p2', 3), | 68 annotateline(b'p2', 3), |
63 ]) | 69 ]) |
64 | 70 |
65 childann = decorate(childdata, childfctx) | 71 childann = decorate(childdata, childfctx) |
66 childann = _annotatepair([p2ann, p1ann], childfctx, childann, False, | 72 childann = _annotatepair([p2ann, p1ann], childfctx, childann, False, |
67 diffopts) | 73 diffopts) |
68 self.assertEqual(childann[0], [ | 74 self.assertEqual(tr(childann), [ |
69 annotateline(b'old', 1), | 75 annotateline(b'old', 1), |
70 annotateline(b'c', 2), | 76 annotateline(b'c', 2), |
71 annotateline(b'p1', 3), | 77 annotateline(b'p1', 3), |
72 annotateline(b'c', 4), | 78 annotateline(b'c', 4), |
73 annotateline(b'p2', 3), | 79 annotateline(b'p2', 3), |
76 # Test with skipchild (note the difference caused by ordering) | 82 # Test with skipchild (note the difference caused by ordering) |
77 | 83 |
78 childann = decorate(childdata, childfctx) | 84 childann = decorate(childdata, childfctx) |
79 childann = _annotatepair([p1ann, p2ann], childfctx, childann, True, | 85 childann = _annotatepair([p1ann, p2ann], childfctx, childann, True, |
80 diffopts) | 86 diffopts) |
81 self.assertEqual(childann[0], [ | 87 self.assertEqual(tr(childann), [ |
82 annotateline(b'old', 1), | 88 annotateline(b'old', 1), |
83 annotateline(b'old', 2, True), | 89 annotateline(b'old', 2, True), |
84 # note that this line was carried over from earlier so it is *not* | 90 # note that this line was carried over from earlier so it is *not* |
85 # marked skipped | 91 # marked skipped |
86 annotateline(b'p2', 2), | 92 annotateline(b'p2', 2), |
89 ]) | 95 ]) |
90 | 96 |
91 childann = decorate(childdata, childfctx) | 97 childann = decorate(childdata, childfctx) |
92 childann = _annotatepair([p2ann, p1ann], childfctx, childann, True, | 98 childann = _annotatepair([p2ann, p1ann], childfctx, childann, True, |
93 diffopts) | 99 diffopts) |
94 self.assertEqual(childann[0], [ | 100 self.assertEqual(tr(childann), [ |
95 annotateline(b'old', 1), | 101 annotateline(b'old', 1), |
96 annotateline(b'old', 2, True), | 102 annotateline(b'old', 2, True), |
97 annotateline(b'p1', 3), | 103 annotateline(b'p1', 3), |
98 annotateline(b'p1', 3, True), | 104 annotateline(b'p1', 3, True), |
99 annotateline(b'p2', 3), | 105 annotateline(b'p2', 3), |