Small perf improvement

This commit is contained in:
Kovid Goyal 2014-12-19 14:02:33 +05:30
parent f8aea31f40
commit 8d16c274fe

View File

@ -19,7 +19,7 @@ from calibre.utils.matcher import Matcher
Request = namedtuple('Request', 'id type data query') Request = namedtuple('Request', 'id type data query')
names_cache = frozenset() names_cache = {}
def control(func): def control(func):
func.function_type = 'control' func.function_type = 'control'
@ -33,7 +33,7 @@ def data(func):
def clear_caches(cache_type, data_conn): def clear_caches(cache_type, data_conn):
global names_cache global names_cache
if cache_type is None or cache_type == 'names': if cache_type is None or cache_type == 'names':
names_cache = frozenset() names_cache.clear()
@data @data
def names_data(request_data): def names_data(request_data):
@ -63,19 +63,14 @@ class Name(unicode):
@control @control
def complete_names(names_type, data_conn): def complete_names(names_type, data_conn):
global names_cache
if not names_cache: if not names_cache:
mime_map, spine_names = get_data(data_conn, 'names_data') mime_map, spine_names = get_data(data_conn, 'names_data')
names_cache = frozenset(Name(name, mt, spine_names) for name, mt in mime_map.iteritems()) names_cache[None] = frozenset(Name(name, mt, spine_names) for name, mt in mime_map.iteritems())
ans = names_cache names_cache['text_link'] = frozenset(n for n in names_cache if n.in_spine)
if names_type == 'text_link': names_cache['stylesheet'] = frozenset(n for n in names_cache if n.mime_type in OEB_STYLES)
ans = frozenset(n for n in names_cache if n.in_spine) names_cache['image'] = frozenset(n for n in names_cache if n.mime_type.startswith('image/'))
elif names_type == 'stylesheet': names_cache['font'] = frozenset(n for n in names_cache if n.mime_type in OEB_FONTS)
ans = frozenset(n for n in names_cache if n.mime_type in OEB_STYLES) ans = names_cache.get(names_type, names_cache[None])
elif names_type == 'image':
ans = frozenset(n for n in names_cache if n.mime_type.startswith('image/'))
elif names_type == 'font':
ans = frozenset(n for n in names_cache if n.mime_type in OEB_FONTS)
return ans, {} return ans, {}
_current_matcher = (None, None, None) _current_matcher = (None, None, None)