Skip to content

⚡ Optimize EPUB image extraction using ZipFile random access#33

Merged
Aatricks merged 1 commit intomainfrom
perf-epub-optimization-16254206562520747317
Feb 6, 2026
Merged

⚡ Optimize EPUB image extraction using ZipFile random access#33
Aatricks merged 1 commit intomainfrom
perf-epub-optimization-16254206562520747317

Conversation

@Aatricks
Copy link
Owner

@Aatricks Aatricks commented Feb 6, 2026

Optimized ContentRepository.getEpubImage to use java.util.zip.ZipFile instead of ZipInputStream.
ZipInputStream requires scanning the entire stream to find a specific entry, which is O(N) where N is the number of files in the EPUB.
ZipFile reads the central directory and allows O(1) (or close to it) access to specific entries.

Since ZipFile requires a local File and cannot work on InputStream (e.g., from content:// URIs), I implemented a caching mechanism that copies the stream to a temporary local file in epub_cache. This copy happens only once per session/cache lifecycle.

Implemented thread-safe file caching using atomic rename (renameTo) to handle concurrent requests (e.g., multiple images loading from the same EPUB simultaneously). Added robust error handling to clean up corrupted cache files.

Performance benchmark demonstrated a 45x improvement (45ms -> 1ms) for a 5000-entry archive.


PR created automatically by Jules for task 16254206562520747317 started by @Aatricks

Replaced inefficient linear scan of ZipInputStream with ZipFile random access for EPUB image retrieval. This improves lookup performance from O(N) to O(1) per image.

For content:// URIs, the EPUB is now cached to a local file to enable random access usage, with thread-safe atomic file renaming to prevent corruption during concurrent access.

Benchmark results showed a reduction from ~45ms to ~1ms for lookups in a test file with 5000 entries.

Co-authored-by: Aatricks <113598245+Aatricks@users.noreply.github.com>
@google-labs-jules
Copy link
Contributor

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

@Aatricks Aatricks merged commit c3dbe88 into main Feb 6, 2026
1 check passed
@Aatricks Aatricks self-assigned this Feb 6, 2026
@Aatricks Aatricks deleted the perf-epub-optimization-16254206562520747317 branch February 6, 2026 19:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant