Rev 4452: We can pre-allocate the output lines as well. in http://bazaar.launchpad.net/~jameinel/bzr/1.17-annotate-bug387952

John Arbash Meinel john at arbash-meinel.com
Tue Jun 16 22:24:53 BST 2009


At http://bazaar.launchpad.net/~jameinel/bzr/1.17-annotate-bug387952

------------------------------------------------------------
revno: 4452
revision-id: john at arbash-meinel.com-20090616212448-be52l7nlaewoch7f
parent: john at arbash-meinel.com-20090616212044-gdm6tfrrg230j72v
committer: John Arbash Meinel <john at arbash-meinel.com>
branch nick: 1.17-annotate-bug387952
timestamp: Tue 2009-06-16 16:24:48 -0500
message:
  We can pre-allocate the output lines as well.
  However timing shows that it doesn't seem specifically faster, yet.
-------------- next part --------------
=== modified file 'bzrlib/annotate.py'
--- a/bzrlib/annotate.py	2009-06-16 21:20:44 +0000
+++ b/bzrlib/annotate.py	2009-06-16 21:24:48 +0000
@@ -405,36 +405,34 @@
     right_parent_subset = right_parent_lines[parent_idx:parent_idx + match_len]
     if annotated_match_subset == right_parent_subset:
         # Shortcut the case when the two sides match exactly
-        output_lines.extend(annotated_match_subset)
+        output_lines[new_idx:new_idx + match_len] = annotated_match_subset
         return
-    assert len(annotated_match_subset) == len(right_parent_subset)
-    resolved = [None] * len(annotated_match_subset)
     for idx in xrange(len(annotated_match_subset)):
+        out_idx = new_idx + idx
         new_line = annotated_match_subset[idx]
         right_line = right_parent_subset[idx]
         assert new_line[1] == right_line[1]
         if new_line[0] == right_line[0] or new_line[0] == new_revision_id:
             # These have identical annotations, or the right parent supersedes
             # the 'new' status
-            resolved[idx] = right_line
+            output_lines[out_idx] = right_line
         else:
             # Both new and right parent lay claim to this line, resolve
             if heads_provider is None:
-                resolved[idx] = (new_revision_id, right_line[1])
+                output_lines[out_idx] = (new_revision_id, right_line[1])
             else:
                 heads = heads_provider.heads((new_line[0], right_line[0]))
                 if len(heads) == 1:
                     for head in heads:
                         break
-                    resolved[idx] = (head, right_line[1])
+                    output_lines[out_idx] = (head, right_line[1])
                 else:
                     # Both claim different origins, get a stable result.
                     # If the result is not stable, there is a risk a
                     # performance degradation as criss-cross merges will
                     # flip-flop the attribution.
-                    resolved[idx] = _break_annotation_tie(
+                    output_lines[out_idx] = _break_annotation_tie(
                                         [new_line, right_line])
-    output_lines.extend(resolved)
 
 
 def _reannotate_annotated(right_parent_lines, new_lines, new_revision_id,
@@ -457,14 +455,13 @@
     plain_parent_lines = [l for r, l in right_parent_lines]
     matching_parent_and_new = _get_matching_blocks(plain_parent_lines,
                                                    new_lines)
-    lines = []
-    lines_extend = lines.extend
+    lines = [None] * len(annotated_lines)
     last_new_idx = 0
     for parent_idx, new_idx, match_len in matching_parent_and_new:
         # The lines from last_new_idx to new_idx did not match anything in
         # plain_parent_lines
         # For these lines, we just carry across the annotations from parent
-        lines_extend(annotated_lines[last_new_idx:new_idx])
+        lines[last_new_idx:new_idx] = annotated_lines[last_new_idx:new_idx]
         # for the lines which *did* match, we need to determine if we have
         # conflicts between what the right_parent_lines would claim as the
         # annotation, and what the current annotated_lines claims is the
@@ -474,6 +471,5 @@
                                  right_parent_lines, parent_idx,
                                  match_len, new_revision_id, heads_provider)
 
-        last_parent_idx = parent_idx + match_len
         last_new_idx = new_idx + match_len
     return lines



More information about the bazaar-commits mailing list