Skip to content

EPUB parsing optimizations#314

Merged
chocolatkey merged 5 commits into
developfrom
epub-parsing-optimizations
Jul 8, 2026
Merged

EPUB parsing optimizations#314
chocolatkey merged 5 commits into
developfrom
epub-parsing-optimizations

Conversation

@chocolatkey

Copy link
Copy Markdown
Member

AI was tasked with benchmarking (using built-in Go testing framework) the EPUB parsing, to try and find potential optimizations. This is the result. The main "optimization" left I'm not touching any time soon is the XML parsing done by xmlquery

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR focuses on improving EPUB parsing performance by reducing redundant work (notably around encryption.xml parsing), streamlining whitespace handling, adding a benchmark harness, and optimizing ZIP archive entry lookups.

Changes:

  • Extend protection.IdentifyEPUBProtection to optionally return a parsed encryption.xml document so callers can reuse it instead of re-parsing.
  • Replace regex-based whitespace normalization with strings.Fields / a custom ASCII whitespace collapser, and remove a few redundant computations.
  • Add an EPUB parser benchmark and speed up ZIP entry lookup via a lazily-built name index.

Reviewed changes

Copilot reviewed 10 out of 10 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
pkg/protection/epub.go Changes DRM detection to probe known paths via Get()+Length(), and returns an optional parsed encryption.xml document.
pkg/protection/epub_test.go Updates tests for the new IdentifyEPUBProtection return signature.
pkg/parser/epub/property_data_type.go Simplifies property tokenization and introduces collapseWhitespace to replace regex usage.
pkg/parser/epub/parser.go Reuses the encryption.xml document from protection detection to avoid re-reading/re-parsing in some cases.
pkg/parser/epub/parser_test.go Updates encryption parsing test to pass through the optional document.
pkg/parser/epub/parser_packagedoc.go Avoids re-parsing the same properties attribute value twice.
pkg/parser/epub/parser_ncx.go Switches title normalization to collapseWhitespace.
pkg/parser/epub/parser_navdoc.go Switches title normalization to collapseWhitespace and tweaks href defaulting logic.
pkg/parser/epub/parser_bench_test.go Adds a benchmark that parses EPUBs found in a local publications directory.
pkg/archive/archive_zip.go Adds a lazily-built cleaned-path index for O(1) ZIP entry lookup and shares entry wrappers via sync.Map.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread pkg/protection/epub.go Outdated
Comment thread pkg/protection/epub.go
Comment thread pkg/protection/epub.go
Comment on lines +66 to 68
if _, lerr := res.Length(ctx); lerr != nil {
return nil, false
}
Comment thread pkg/parser/epub/parser_bench_test.go
chocolatkey and others added 4 commits July 4, 2026 16:43
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
@chocolatkey
chocolatkey merged commit 00f9452 into develop Jul 8, 2026
3 checks passed
@chocolatkey
chocolatkey deleted the epub-parsing-optimizations branch July 8, 2026 00:27
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.

2 participants