From 939a8f121f765ce02fffefab73a9c6c157058dbc Mon Sep 17 00:00:00 2001 From: tsoolgee <291792265+tsoolgee@users.noreply.github.com> Date: Tue, 17 Nov 2026 20:13:57 +0200 Subject: [PATCH] perf: cut the live typeset round trip from 7.4 s to 2.1 s on 100 pages 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. --- crates/tok-cli/src/main.rs | 17 ++++++++++++----- crates/tok-typeset/src/geometry.rs | 6 +++++- packages/tok-electron/src/main.ts | 9 ++++----- packages/tok-electron/src/preload.ts | 2 +- packages/tok-ui/src/app.ts | 9 +++++---- packages/tok-ui/src/engine/typesetBridge.ts | 16 ++++++++++++++++ 6 files changed, 43 insertions(+), 16 deletions(-) diff --git a/crates/tok-cli/src/main.rs b/crates/tok-cli/src/main.rs index 070e1d7..37ea1a7 100644 --- a/crates/tok-cli/src/main.rs +++ b/crates/tok-cli/src/main.rs @@ -1044,6 +1044,13 @@ fn handle_typeset_document( } } + // Compact output, serialized straight from the structs: this runs on every edit, and + // json!() would first copy every glyph into a serde_json::Value and widen f32 to f64. + #[derive(serde::Serialize)] + struct TypesetWithPreflight<'a> { + pages: &'a [tok_typeset::geometry::PageLayoutBox], + preflight: &'a tok_pdf::preflight::PreflightReport, + } let with_preflight = extra_args.iter().any(|a| a == "--with-preflight"); let json_bytes = if with_preflight { let mut opts = PdfExportOptions::default(); @@ -1053,12 +1060,12 @@ fn handle_typeset_document( } } let report = PreflightEngine::evaluate(doc.root(), &pages, &engine.font_manager, &opts); - serde_json::to_vec_pretty(&serde_json::json!({ - "pages": pages, - "preflight": report, - }))? + serde_json::to_vec(&TypesetWithPreflight { + pages: &pages, + preflight: &report, + })? } else { - serde_json::to_vec_pretty(&pages)? + serde_json::to_vec(&pages)? }; fs::write(output, json_bytes)?; println!(" [SUCCESS] Written layout JSON to: {}", output); diff --git a/crates/tok-typeset/src/geometry.rs b/crates/tok-typeset/src/geometry.rs index cec46e7..6dfa464 100644 --- a/crates/tok-typeset/src/geometry.rs +++ b/crates/tok-typeset/src/geometry.rs @@ -1,5 +1,9 @@ use serde::{Deserialize, Serialize}; +fn is_zero_u16(v: &u16) -> bool { + *v == 0 +} + #[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)] pub struct PhysicalPoint { pub x: f32, // in DTP points (pt) @@ -61,7 +65,7 @@ pub struct GlyphBox { #[serde(default)] pub font_index: u16, /// Index into the owning line's [`LineBox::colors`]; `0` is the paragraph's base color. - #[serde(default)] + #[serde(default, skip_serializing_if = "is_zero_u16")] pub color_index: u16, #[serde(default, skip_serializing_if = "std::ops::Not::not")] pub underline: bool, diff --git a/packages/tok-electron/src/main.ts b/packages/tok-electron/src/main.ts index 51679c2..76f39d2 100644 --- a/packages/tok-electron/src/main.ts +++ b/packages/tok-electron/src/main.ts @@ -850,11 +850,10 @@ handle('tok:typeset-document', async (_, payload: { document?: unknown; inputPat throw new Error(`Typeset failed: output file was not created at ${tempOutFile}`); } - const rawContent = await fs.promises.readFile(tempOutFile, 'utf-8'); - const parsed = JSON.parse(rawContent); - const pages = Array.isArray(parsed) ? parsed : (Array.isArray(parsed?.pages) ? parsed.pages : []); - const preflight = Array.isArray(parsed) ? null : (parsed?.preflight ?? null); - return { success: true, pages, preflight }; + // Sent as the raw string and parsed in the renderer: cloning a string over IPC is a copy, + // while parsing here and cloning the object tree cost as much as typesetting itself. + const layoutJson = await fs.promises.readFile(tempOutFile, 'utf-8'); + return { success: true, layoutJson }; } finally { if (tempDocFile) { try { diff --git a/packages/tok-electron/src/preload.ts b/packages/tok-electron/src/preload.ts index a21a5b8..c52e413 100644 --- a/packages/tok-electron/src/preload.ts +++ b/packages/tok-electron/src/preload.ts @@ -45,7 +45,7 @@ export interface TokIpcBridge { openDocument: (filePath: string) => Promise; renameDocumentFile: (payload: { oldPath: string; newPath: string }) => Promise<{ success: boolean; filePath?: string; error?: string }>; getProjectsDir: () => Promise; - typesetDocument: (payload: { document?: unknown; inputPath?: string; assetsFrom?: string; withPreflight?: boolean } | unknown) => Promise<{ success: boolean; pages: any[]; preflight?: unknown }>; + typesetDocument: (payload: { document?: unknown; inputPath?: string; assetsFrom?: string; withPreflight?: boolean } | unknown) => Promise<{ success: boolean; layoutJson?: string; pages?: any[]; preflight?: unknown }>; runPreflight: (payload: { document?: unknown; inputPath?: string; assetsFrom?: string; standard?: string; bleed?: boolean }) => Promise<{ success: boolean; report?: unknown; error?: string }>; // Font Inspection & Installation (Rust Core) diff --git a/packages/tok-ui/src/app.ts b/packages/tok-ui/src/app.ts index 750d657..c503465 100644 --- a/packages/tok-ui/src/app.ts +++ b/packages/tok-ui/src/app.ts @@ -45,7 +45,7 @@ import { } from './engine/documentBridge'; import type { LineRunLookup, LineRunSource } from './engine/lineRuns'; import { importDocument, parseDocxXml, ImportResult } from './engine/textImporter'; -import { layoutToPageDescriptors, PreflightReport } from './engine/typesetBridge'; +import { layoutToPageDescriptors, PreflightReport, readTypesetResponse } from './engine/typesetBridge'; import { toHebrewGematria } from './gematria'; import { normalizeHebrew, normalizeQuotes, findHolyNames } from './plugins/hebrew-utils'; @@ -2365,9 +2365,10 @@ export class TypesetOkApp { if (requestId !== this.latestTypesetRequestId || abortCtrl.signal.aborted) { return; } - if (res && res.success && Array.isArray(res.pages)) { - newPages = layoutToPageDescriptors(res.pages, this.buildLineRunLookup()); - const pfReport = (res.preflight ?? null) as PreflightReport | null; + const layout = res && res.success ? readTypesetResponse(res) : null; + if (layout) { + newPages = layoutToPageDescriptors(layout.pages, this.buildLineRunLookup()); + const pfReport = layout.preflight; this.latestPreflightReport = pfReport; this.inspector?.setPreflightReport(pfReport); if (pfReport) { diff --git a/packages/tok-ui/src/engine/typesetBridge.ts b/packages/tok-ui/src/engine/typesetBridge.ts index 84a23e7..6ae1e38 100644 --- a/packages/tok-ui/src/engine/typesetBridge.ts +++ b/packages/tok-ui/src/engine/typesetBridge.ts @@ -150,6 +150,22 @@ export interface PageLayoutBox { diagnostics?: LayoutDiagnostic[]; } +/** + * Reads a typeset IPC response: `layoutJson` is the engine output as text (either a page + * array or `{ pages, preflight }`); older shells sent already-parsed `pages`/`preflight`. + */ +export function readTypesetResponse(res: { layoutJson?: string; pages?: unknown; preflight?: unknown }): { + pages: PageLayoutBox[]; + preflight: PreflightReport | null; +} { + const parsed: any = typeof res.layoutJson === 'string' ? JSON.parse(res.layoutJson) : res; + if (Array.isArray(parsed)) return { pages: parsed, preflight: null }; + return { + pages: Array.isArray(parsed?.pages) ? parsed.pages : [], + preflight: (parsed?.preflight ?? null) as PreflightReport | null, + }; +} + function escapeHtml(str: string): string { return str .replace(/&/g, '&')