gotson / komga

Media server for comics/mangas/BDs/magazines/eBooks with API, OPDS and Kobo Sync support
https://komga.org
MIT License
4.07k stars 240 forks source link

perf: faster zip entry extraction #1661

Closed Snd-R closed 2 months ago

Snd-R commented 2 months ago

Currently ZipFile read will scan entire file to read each entry's local file header on each request for book page image. This can be quite a slow operation depending on archive file size. In my tests 800mb archive with ~400 entries stored on hdd takes ~3 seconds on initial read on Linux system. After initial read OS will cache file and subsequent reads become really fast ~50-100ms, but in my opinion it's better to not rely on OS for caching

If we only read central directory header entry retrieval takes ~100-150ms instead of 3 seconds which is about 30 times faster The downside to only reading central directory is that filename can use Unicode extra field and that field is (for some reason) only added to local file header. In that case retrieving entry might fail and we fallback to iterating each entry local file header and set name from Unicode extra field if it exists

I've also updated Apache Compress library version which deprecated ZipFile constructor calls and replaced it with Builders

gotson commented 2 months ago

thanks for the PR, i'll have a look when i have time, and will run some tests too