Skip to content

perf(reader): read books as lines, no join and re-split on open - #5876

Closed
KleiKodesh wants to merge 2 commits into
Otzaria:devfrom
KleiKodesh:book-lines-direct
Closed

KleiKodesh wants to merge 2 commits into
Otzaria:devfrom
KleiKodesh:book-lines-direct

Conversation

@KleiKodesh

Copy link
Copy Markdown
Member

Problem

Opening a book from seforim.db read its rows, joined them with \n into one string (BookTextReader / getBookTextFromDb / DatabaseLibraryProvider.getBookText), and the viewer then split that string on \n again (splitContentLines, called from TextBookBloc initial load, background full load, and from TextBookScreen). The join and the re-split are wasted work on every book open.

Design

One format for every book source: a list of lines.

  • Database books hand their rows over as lines: readBookContentLines / BookTextReader.lines (worker method bookLines, same book slot as bookText), SqliteDataProvider.getBookLinesFromDb. This covers the official DB, the user-books DB and attached libraries (DatabaseLibraryProvider.getBookLines).
  • Text files are split once when read (FileSystemLibraryProvider.getBookLines, splitBookText).
  • New optional interface BookLinesProvider (same pattern as LinkLinesProvider). LibraryProviderManager.getBookLines uses it, or splits a provider's text once. TextBookRepository.getBookLines mirrors getBookContent, including the version overlay and the DB fallback.
  • The viewer (bloc and screen) takes getBookLines. getBookText / getBookContent are now joins of the lines, built on demand. They are kept only for consumers that genuinely need one string: export, print, the plugin API library.getBookContent, tikkun korim. Indexing and personal notes keep getBookTextFromDb / getBookTextBytesFromDb unchanged.

Behaviour unchanged

Lines equal the old text split on \n on every path:

  • one leading BOM dropped per row, as before
  • a row that holds \n becomes several lines, as the split made it
  • the UTF-16 DB path
  • NULL content
  • an empty book gives []
  • the empty-text fallbacks in TextBookRepository

book_text_reader_test now checks that the lines equal the legacy joined text split on \n in every existing case: the official worker, the untrusted isolate, the main connection, and getBookLinesFromDb. New book_lines_test covers a text-file book (lines == its text split), the manager with a text-only provider and with a lines provider, and the helpers. On the real seforim.db, every measured book's lines equal its row count and the old split output.

Measurements

Real seforim.db on the official worker path. Old = BookTextReader.text + split in an isolate, as the viewer did. New = BookTextReader.lines. Median of 7 runs, on a desktop that was also running other load:

book (lines) old new
12 21.4 ms 22.0 ms
199 20.9 ms 19.5 ms
2,009 50.5 ms 46.7 ms
11,885 159.4 ms 153.0 ms
190,453 (largest) 2577 ms 2932 ms (min 2439 vs 2339)

A second run gave 4144 vs 4104 ms for the largest book. Read directly on one connection (no isolates), the largest book takes 2500 ms for the joined read, 336 ms for the split, and 2734 ms for the lines read. So the join + split was about 10 % of the open. Decoding each row separately costs about the same as that split, and reading the rows (decompression and SQL) dominates. The timing difference is within noise. The win is structural: one format, no re-split, and no full-book string held next to its lines on the viewer path. Peak memory was not measured.

Tests

  • flutter analyze: clean for this change.
  • Passing: test/data_providers, test/data, test/text_book/bloc, test/text_book/utils/link_processing_test.dart, test/text_book/view/combined_view, simple_text_viewer_test, and every test file touched here.
  • Failing on this machine, and failing the same way on dev without this change (Windows paths and file locks):
    • custom_folder_add_worker_test
    • sqlite_schema_gate_index_test
    • links_load_coalescing_test
    • one export test in text_book_screen_actions_test

Overlapping open PRs

Checked with git merge-tree.

A seforim.db book was read as rows, joined with newlines into one string
and split again on newlines for the viewer. Each source now hands over a
list of lines: database rows directly (BookTextReader.lines, worker method
bookLines, getBookLinesFromDb; official, user-books and attached
databases), a text file split once (BookLinesProvider,
LibraryProviderManager.getBookLines, TextBookRepository.getBookLines).
getBookText / getBookContent are joins of the lines, built on demand for
consumers that need one string. Lines equal the old text split on newlines
in every path (BOM per row, rows holding a newline, UTF-16, empty books).
The text book bloc (initial load, background full load) and the screen
(in-book search loader, context-menu line lookup) take
TextBookRepository.getBookLines instead of splitting getBookContent.
splitContentLines is gone (splitBookText replaces it). Test repositories
that override getBookContent also serve getBookLines from it.
@Y-PLONI

Y-PLONI commented Nov 23, 2026

Copy link
Copy Markdown
Member

סוגר את ה־PR לאחר סקירת שרשרת הקריאה. במסלול הרגיל לפתיחת ספר רשמי הקורא מתחיל בטווח שורות, והמדידה כאן משווה טעינה מלאה. בטעינה המלאה עצמה המדידות בתיאור אינן מראות שיפור עקבי, ושיא הזיכרון טרם נמדד.

ה־API שמחזיר מחרוזת מעביר כעת רשימה של ספר שלם מה־worker ומחבר אותה ב־UI isolate. למשל, צורת הדף עושה זאת בטעינה מלאה של מפרש ממסד מצורף, ואז מפצלת את התוצאה שוב. גם ייצוא, הדפסה ותוספים עוברים בנתיב המחרוזת שהשתנה. התקבל כאן מעבר עבודה ל־UI במסלולים שמשתמשים בהם בפועל, בלי שיפור מדוד שמצדיק את היקף השינוי.

תיקון המשך מתאים יכול לשמר את נתיב המחרוזת הישיר, להוסיף קריאת שורות רק לצרכנים שצריכים אותה, ולמדוד זמן ושיא זיכרון בפתיחת ספר ובטעינת מפרש ממסד מצורף.

@Y-PLONI Y-PLONI closed this Nov 23, 2026
@Y-PLONI

Y-PLONI commented Nov 23, 2026

Copy link
Copy Markdown
Member

@KleiKodesh
סלח לי, אבל codex טוען שאין שיפורים בגלל הריפקטור.
אם אתה טוען שהא טועה, אקבל כפי שהוא, בלי לשאול שאלות... סומך עליך.

@Y-PLONI

Y-PLONI commented Nov 23, 2026

Copy link
Copy Markdown
Member

נאום קודקס:
יש חלק שעשוי להיות שווה להציל: להחזיר שורות ישירות לקורא כשהוא ממילא צריך אותן, ולשמר את מסלול המחרוזת הקיים לייצוא, להדפסה ולתוספים. לא בניתי ומדדתי את הגרסה הממוקדת הזאת לפני הסגירה. לכן יש בסיס לסגור את הריפקטור כפי שהוגש, והשאלה אם כדאי לתקן אותו בגרסה מצומצמת עדיין פתוחה.

@KleiKodesh

KleiKodesh commented Nov 23, 2026 via email

Copy link
Copy Markdown
Member Author

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