EPUB Input: Speed up reading of the book spine from the OPF file for books with a very large number of entries in the spine

Converts an O(n^2) algorithm to O(n)
This commit is contained in:
Kovid Goyal 2016-07-05 09:56:14 +05:30
parent 415ebb7a6c
commit 4ab707e60b

View File

@ -301,10 +301,11 @@ class Spine(ResourceCollection): # {{{
def from_opf_spine_element(itemrefs, manifest): def from_opf_spine_element(itemrefs, manifest):
s = Spine(manifest) s = Spine(manifest)
seen = set() seen = set()
path_map = {i.id:i.path for i in s.manifest}
for itemref in itemrefs: for itemref in itemrefs:
idref = itemref.get('idref', None) idref = itemref.get('idref', None)
if idref is not None: if idref is not None:
path = s.manifest.path_for_id(idref) path = path_map.get(idref)
if path and path not in seen: if path and path not in seen:
r = Spine.Item(lambda x:idref, path, is_path=True) r = Spine.Item(lambda x:idref, path, is_path=True)
r.is_linear = itemref.get('linear', 'yes') == 'yes' r.is_linear = itemref.get('linear', 'yes') == 'yes'