Skip to content

iOS: measure/optimize EPUB repaginate(for:) cost on large books #336

Closed

Description

@libravault-xyz

Problem

ReaderView.repaginate(for:) (iOS EPUB reader, landed in #331/PR #333) re-lays-out every chapter of the book synchronously on the main thread, via TextPaginator.paginate (TextKit NSLayoutManager/NSTextContainer), every time it's triggered: device rotation, Split View/Slide Over resize on iPad, or a font size / line spacing / font design change in Reading Settings.

For a typical-length book this is presumably fine, but there's currently zero test coverage or measurement against a realistically large book (a long novel, an omnibus, a technical reference with many long chapters). Worst case this is a visible main-thread hang/jank on rotation or when adjusting the font-size slider (which can fire repaginate on every intermediate value if not debounced — worth checking).

Suggested scope

  • Measure real-world repagination cost against a large EPUB (e.g. time repaginate(for:) for a several-hundred-page book on a real device or Simulator).
  • If it's actually slow: consider debouncing font/line-spacing slider changes before repaginating, paginating chapters lazily/incrementally (e.g. only the current + adjacent chapters up front, rest on demand) instead of the whole book up front, or moving the TextKit layout work off the main thread.
  • Add a test or benchmark that would catch a regression here.

Origin

Flagged as a non-blocking observation in principal review of PR #333 (real EPUB pagination): #333 (comment)

Not a correctness bug — no CONFIRMED issue, just an unverified performance risk worth checking on-device.

Activity

  1. added
    status:triage
    on Aug 20, 2026
  2. added
    status:ready-for-dev
    and removed
    status:triage
    on Aug 20, 2026
  3. github-actions commented on Aug 20, 2026

    @github-actions
    Contributor

    Triage: reproduced the concern by reading ReaderView.repaginate(for:) and TextPaginator.paginate. Confirmed: repaginate(for:) does call chapters.map { TextPaginator.paginate(...) } — every chapter, synchronously, on the main thread — and the font-size/line-spacing sliders in ReaderSettingsSheet use step: 0.1 (not continuous), so a full drag can still fire repagination up to ~7 times in a row, each doing a whole-book layout.

    Scoping this: I can't run a real device/Simulator measurement from this environment directly, but this repo's iOS CI (ios-app-build.yml) does run LibraVaultTests on a macOS runner with a real Simulator, so I'm going to add a regression/benchmark test against a realistically large synthetic book (~30 chapters, ~900k characters total) that exercises the actual TextPaginator.paginate path, open the PR, and read the real timing back from that CI run before deciding whether an optimization (debounce / lazy pagination / background layout) is actually warranted, rather than guessing blind. Proceeding.

  4. added
    status:in-progress
    and removed
    status:ready-for-dev
    on Aug 20, 2026
  5. libravault-xyz commented on Aug 21, 2026

    @libravault-xyz
    CollaboratorAuthor

    Automated pipeline health check: Not one of the 5 known failure patterns — flagging a stale in-progress state caused by its linked PR going away, not crashing.

    This issue's linked PR, #339 (Closes #336, off-main-thread TextPaginator fix), was just closed unmerged (2026-08-21T04:42:14Z) as superseded: PR #369 (merged 2026-08-20T19:41:09Z, after #339 was opened) replaced the entire pagination mechanism ReaderView uses — it now paginates via BlockPaginator instead of TextPaginator, synchronously on the main thread. TextPaginator is dead code from ReaderView's perspective as of dev, so #339's diff no longer applies (hence its merge conflict) and its whole approach targets a code path that doesn't run anymore.

    So #336 is stuck on status:in-progress with no active PR and no crash to retry — the original perf concern (main-thread repagination cost on large books) is still real, but needs to be re-measured and re-scoped against the current BlockPaginator path rather than resuming #339's approach. That's a fresh triage/scoping call, not something in this routine's authorized self-heal set (capacity-cap retry, confirmed-infra retry, stale-branch rebase, pattern-5 near-miss requeue, pattern-4 retry-once).

    Recommend a human re-apply status:ready-for-dev (or status:triage) to get a fresh dev-agent pass scoped against BlockPaginator.


    Generated by Claude Code

  6. removed
    status:in-progress
    on Aug 21, 2026
  7. libravault-xyz commented on Aug 22, 2026

    @libravault-xyz
    CollaboratorAuthor

    PR #339 (this issue's fix) was closed as architecturally obsolete — dev replaced TextPaginator with BlockPaginator (#369) after #339's branch was cut, and ReaderView.repaginate(for:) now runs BlockPaginator.paginate synchronously on the main thread with no off-thread/detached path. #336's underlying perf concern is still real and unaddressed against the current code — re-applying status:ready-for-dev for a fresh dev-agent attempt scoped to BlockPaginator.paginate, not the old TextPaginator path #339 optimized.

  8. added
    status:ready-for-dev
    on Aug 22, 2026
  9. added
    status:in-progress
    on Aug 22, 2026
  10. github-actions commented on Aug 22, 2026

    @github-actions
    Contributor

    Re-scoped and implemented against the current `BlockPaginator` path (per the last comment) in #411: ported forward the actual fix from the closed/obsoleted PR #339 (which had already measured a real ~4.3s main-thread stall for a ~900k-character, 30-chapter book on CI's macOS Simulator runner, back when `ReaderView` still used `TextPaginator`) — moved `repaginate(for:)`'s `BlockPaginator.paginate` loop onto a detached background task with a generation-counter guard, plus a large-book regression-guard test and an off-main-thread-safety test against `BlockPaginator` directly.

    I have no Simulator/device in this environment to re-measure `BlockPaginator`'s specific cost (it lays out one TextKit graph per block rather than one per chapter, a different cost shape than what #339 measured), so the new regression test's threshold is a deliberately generous judgment call pending a real CI run — called out explicitly in the PR description for QA/review to revisit if the actual number comes back much lower.

    PR: #411 (draft, marking ready once CI passes).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    status:in-progressstatus:ready-for-dev

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions