From b68dfe17e55ac28cd9bddd8b4d229c39eecb06bc Mon Sep 17 00:00:00 2001 From: Patrick Uiterwijk Date: Jun 29 2017 21:40:46 +0000 Subject: Make patch_to_diff use lists instead of string concatenation 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 --- diff --git a/pagure/ui/filters.py b/pagure/ui/filters.py index 3e7ef01..ae39f67 100644 --- a/pagure/ui/filters.py +++ b/pagure/ui/filters.py @@ -420,25 +420,26 @@ def html_diff(diff): @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')