EPUB parsing optimizations#314
Merged
Merged
Conversation
Contributor
There was a problem hiding this comment.
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.IdentifyEPUBProtectionto optionally return a parsedencryption.xmldocument 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 on lines
+66
to
68
| if _, lerr := res.Length(ctx); lerr != nil { | ||
| return nil, false | ||
| } |
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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