From 4e8b96ac13f0a6a101544cf4c7c91e33b32fcb14 Mon Sep 17 00:00:00 2001 From: Vidar Tonaas Fauske Date: Thu, 20 Apr 2017 14:04:58 +0200 Subject: [PATCH] Fixes test error + further optimization We can only cache string type mime data comparisons (hashable). Also added a cache on compare_text_approximate, as profiling showed that a hash there gives a reasonable amount of hits. --- nbdime/diffing/notebooks.py | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/nbdime/diffing/notebooks.py b/nbdime/diffing/notebooks.py index ae5d011a..adc78107 100644 --- a/nbdime/diffing/notebooks.py +++ b/nbdime/diffing/notebooks.py @@ -53,6 +53,7 @@ # an argument instead of separate functions. +@lru_cache(maxsize=1024, typed=False) def compare_text_approximate(x, y): # Fast cutoff when one is empty if bool(x) != bool(y): @@ -94,7 +95,15 @@ def compare_base64_strict(x, y): return x == y -@lru_cache(maxsize=1024, typed=False) +@lru_cache(maxsize=128, typed=False) +def _compare_mimedata_strings(x, y, comp_text, comp_base64): + # Most likely base64 encoded data + if _base64.match(x): + return comp_base64(x, y) + else: + return comp_text(x, y) + + def _compare_mimedata(mimetype, x, y, comp_text, comp_base64): mimetype = mimetype.lower() @@ -113,12 +122,7 @@ def _compare_mimedata(mimetype, x, y, comp_text, comp_base64): # TODO: Compare binary images? #if mimetype.startswith("image/"): if isinstance(x, string_types) and isinstance(y, string_types): - # Most likely base64 encoded data - if _base64.match(x): - return comp_base64(x, y) - else: - return comp_text(x, y) - + _compare_mimedata_strings(x, y, comp_text, comp_base64) # Fallback to exactly equal return x == y