#2385 Make patch_to_diff use lists instead of string concatenation
Merged a year ago by pingou. Opened a year ago by puiterwijk.
puiterwijk/pagure fixslowness  into  master

file modified
+7 -6

@@ -420,25 +420,26 @@ 

  @APP.template_filter('patch_to_diff')

  def patch_to_diff(patch):

      """Render a hunk as a diff"""

-     content = ""

+     content = []

      for hunk in patch.hunks:

-         content = content + "@@ -%i,%i +%i,%i @@\n" % (

-             hunk.old_start, hunk.old_lines, hunk.new_start, hunk.new_lines)

+         content.append("@@ -%i,%i +%i,%i @@\n" % (

+             hunk.old_start, hunk.old_lines, hunk.new_start, hunk.new_lines))

+ 

          for line in hunk.lines:

              if hasattr(line, 'content'):

                  origin = line.origin

                  if line.origin in ['<', '>', '=']:

                      origin = ''

-                 content = content + origin + ' ' + line.content

+                 content.append(origin + ' ' + line.content)

              else:

                  # Avoid situation where at the end of a file we get:

                  # + foo<

                  # \ No newline at end of file

                  if line[0] in ['<', '>', '=']:

                      line = ('', line[1])

-                 content = content + ' '.join(line)

+                 content.append(' '.join(line))

  

-     return content

+     return ''.join(content)

  

  

  @APP.template_filter('author2user')

String concatenation is very inefficient in Python (and most languages) due to
the constant reallocation of the same memory block.
Instead, let's just build up the full diff in a list of strings, which we then
concatenate in a single time.

On my laptop, this speeds up the viewing of a particular semi-large diff from
over three minutes to just two seconds.

Signed-off-by: Patrick Uiterwijk puiterwijk@redhat.com

Pull-Request has been merged by pingou

a year ago
Metadata