# HG changeset patch # User Marcin Kuzminski # Date 2019-07-17 21:37:51 # Node ID 7b87073eb18593d1439d6b0de0fb57f5e7c24a31 # Parent 5f8c0244e1af19121f613228adafe3450a7493c9 diffs: switched bz2 into gzip since it can be 10x faster in some cases with only slight size penalty diff --git a/rhodecode/lib/diffs.py b/rhodecode/lib/diffs.py --- a/rhodecode/lib/diffs.py +++ b/rhodecode/lib/diffs.py @@ -26,6 +26,7 @@ Set of diffing helpers, previously part import os import re import bz2 +import gzip import time import collections @@ -1157,8 +1158,17 @@ def _cleanup_cache_file(cached_diff_file log.exception('Failed to cleanup path %s', cached_diff_file) +def _get_compression_mode(cached_diff_file): + mode = 'bz2' + if 'mode:plain' in cached_diff_file: + mode = 'plain' + elif 'mode:gzip' in cached_diff_file: + mode = 'gzip' + return mode + + def cache_diff(cached_diff_file, diff, commits): - mode = 'plain' if 'mode:plain' in cached_diff_file else '' + compression_mode = _get_compression_mode(cached_diff_file) struct = { 'version': CURRENT_DIFF_VERSION, @@ -1168,9 +1178,12 @@ def cache_diff(cached_diff_file, diff, c start = time.time() try: - if mode == 'plain': + if compression_mode == 'plain': with open(cached_diff_file, 'wb') as f: pickle.dump(struct, f) + elif compression_mode == 'gzip': + with gzip.GzipFile(cached_diff_file, 'wb') as f: + pickle.dump(struct, f) else: with bz2.BZ2File(cached_diff_file, 'wb') as f: pickle.dump(struct, f) @@ -1182,7 +1195,7 @@ def cache_diff(cached_diff_file, diff, c def load_cached_diff(cached_diff_file): - mode = 'plain' if 'mode:plain' in cached_diff_file else '' + compression_mode = _get_compression_mode(cached_diff_file) default_struct = { 'version': CURRENT_DIFF_VERSION, @@ -1199,9 +1212,12 @@ def load_cached_diff(cached_diff_file): start = time.time() try: - if mode == 'plain': + if compression_mode == 'plain': with open(cached_diff_file, 'rb') as f: data = pickle.load(f) + elif compression_mode == 'gzip': + with gzip.GzipFile(cached_diff_file, 'rb') as f: + data = pickle.load(f) else: with bz2.BZ2File(cached_diff_file, 'rb') as f: data = pickle.load(f) @@ -1245,6 +1261,7 @@ def diff_cache_exist(cache_storage, *arg """ Based on all generated arguments check and return a cache path """ + args = list(args) + ['mode:gzip'] cache_key = generate_diff_cache_key(*args) cache_file_path = os.path.join(cache_storage, cache_key) # prevent path traversal attacks using some param that have e.g '../../'