Skip to content

perf: cut the live typeset round trip from 7.4 s to 2.1 s on 100 pages - #41

Merged
Amlaach merged 1 commit into
TypesetOK:mainfrom
tsoolgee:perf/live-typeset-roundtrip
Nov 17, 2026
Merged

Amlaach merged 1 commit into
TypesetOK:mainfrom
tsoolgee:perf/live-typeset-roundtrip

Conversation

@tsoolgee

Copy link
Copy Markdown
Member

הבעיה

ב-#37 עלה שכל התוכנה ב-0.36.1 איטית יותר. אחרי כל עריכה (100–150ms) האפליקציה מעמדת מחדש את כל המסמך ב-tok-cli ושולחת את התוצאה לחלון. מדדתי שהסבב הזה איטי ב-55%–60% מ-0.35.2.

מה מדדתי

הרצתי ב-windows-latest את אותו מסלול שהאפליקציה מריצה: documentStateToDocumentRoot, כתיבת קובץ, tok-cli typeset-document עם אותם דגלים, פענוח, העברה בין התהליכים (structured clone), ואז layoutToPageDescriptors. כל גרסה רצה עם הקוד שלה. התוצאה היא ה-median של 7 הרצות על מסמך עברי מנוקד עם עיצוב בחלק מהפסקאות.

זמן מהקלדה עד עדכון העמוד 0.35.2 0.36.1 ה-PR הזה
40 עמודים 1.9 שנ' 2.8 שנ' 0.75 שנ'
100 עמודים 5.0 שנ' 7.4 שנ' 2.1 שנ'
גודל הפלט, 100 עמודים 156 MB 201 MB 64 MB

הגורמים והתיקונים

  1. --with-preflight (חדש ב-0.36) בנה את הפלט עם json!(). זה מעתיק כל עמוד וכל גליף ל-serde_json::Value, וממיר כל f32 ל-f64 (מספרים ארוכים יותר בפלט). כאן זה מסודר ישירות מהמבנים. אחרי התיקון ה-preflight מוסיף בערך 35ms ב-100 עמודים, ולכן הוא ממשיך לרוץ בכל עריכה כמו קודם.
  2. JSON דחוס במקום מעוצב. הפלט המעוצב היה בערך 1.5MB לעמוד.
  3. GlyphBox.color_index לא נכתב כשהוא 0. הממשק לא קורא את השדה, ו-#[serde(default)] כבר מחזיר 0 בקריאה.
  4. הפלט עובר לחלון כמחרוזת. קודם התהליך הראשי פענח את ה-JSON, ואחר כך Electron העתיק את כל עץ האובייקטים לחלון, ושני השלבים יחד עלו בערך כמו העימוד עצמו. עכשיו tok:typeset-document מחזיר layoutJson, והפענוח קורה ב-readTypesetResponse שב-typesetBridge.ts. הפונקציה מקבלת גם את הצורה הישנה (pages/preflight).

בדיקות

על הענף הזה עברו cargo fmt --check, cargo clippy --workspace --all-targets -D warnings, cargo test --workspace, npm run build ו-npm test (273 עברו, 0 נכשלו).

מה לא נבדק

  • המדידה רצה ב-runner של Actions, לא באפליקציה עצמה במחשב רגיל. ציור העמודים בדפדפן אחרי layoutToPageDescriptors לא נכלל בה.
  • המסמך הוא טקסט בלבד, בלי תמונות וקישוטים ובלי גופנים מותקנים בתיקיית המשתמש.

Every edit typesets the whole document in tok-cli and ships the layout to
the renderer. Since 0.36 that round trip is ~55% slower than 0.35.2:

- typeset-document --with-preflight built the output with json!(), which
  first copies every page and glyph into a serde_json::Value and widens
  each f32 to f64. Serialize the structs directly instead.
- Write compact JSON; the pretty-printed layout was ~1.5 MB per page.
- Skip GlyphBox.color_index when it is 0 (the UI does not read it).
- Pass the layout to the renderer as the raw JSON string. Parsing it in
  the main process and cloning the object tree over IPC cost about as
  much as typesetting.

Measured on windows-latest (live round trip, median of 7):
40 pages 2.8 s -> 0.75 s, 100 pages 7.4 s -> 2.1 s, output 201 MB -> 64 MB.
Preflight now adds ~35 ms on 100 pages, so it still runs on every edit.
@Amlaach
Amlaach merged commit 1b4efcc into TypesetOK:main Nov 17, 2026
5 checks passed
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