From 71a7e0d9ff20cfd79b90ff94d57b6380eb5fec4f Mon Sep 17 00:00:00 2001 From: Afloat Date: Sun, 22 Nov 2026 04:33:01 +0800 Subject: [PATCH] perf(desktop): bound retained text lines while counting full files (#10207) --- src/domain/desktop/appExportPython.ts | 8 +++---- src/domain/desktop/stateFormats.test.ts | 22 ++++++++++++++++++ src/domain/desktop/stateFormats.ts | 21 +++++++++++++---- .../providers/desktopExportPython.test.ts | 23 +++++++++++++++++++ 4 files changed, 65 insertions(+), 9 deletions(-) diff --git a/src/domain/desktop/appExportPython.ts b/src/domain/desktop/appExportPython.ts index 15c35c3f6f..bd4fc39c9f 100644 --- a/src/domain/desktop/appExportPython.ts +++ b/src/domain/desktop/appExportPython.ts @@ -29,6 +29,7 @@ export const PYTHON_READY_CHECK = ` export const PYTHON_IMPORTS = `from __future__ import annotations from array import array +from itertools import islice import binascii import contextlib import copy @@ -381,10 +382,9 @@ def _sqlite(path: Path) -> dict: def _text(text: str) -> dict: - lines = LINE_BREAK.split(text) - if lines[-1] == "": - lines.pop() - view = {"lines": lines[:MAX_LINES], "line_count": len(lines), "truncated": len(lines) > MAX_LINES} + count = text.count("\n") + int(bool(text) and not text.endswith("\n")) + lines = list(islice(_each_line(text), min(MAX_LINES, count))) + view = {"lines": lines, "line_count": count, "truncated": count > MAX_LINES} return {"format": "text", "view": view, "truncated": view["truncated"]} diff --git a/src/domain/desktop/stateFormats.test.ts b/src/domain/desktop/stateFormats.test.ts index 07c02f474e..2c0d646761 100644 --- a/src/domain/desktop/stateFormats.test.ts +++ b/src/domain/desktop/stateFormats.test.ts @@ -117,6 +117,28 @@ describe("jsonView", () => { }); describe("views", () => { + it.each([ + ["", []], + ["\n", [""]], + ["one\r\ntwo\n\n", ["one", "two", ""]], + ["one\rtwo\r", ["one\rtwo\r"]], + ["one\u2028two\u0085three", ["one\u2028two\u0085three"]], + ["one\r\r\ntwo", ["one\r", "two"]], + ] as const)( + "counts all lines and retains the requested prefix: %j", + (text, expected) => { + for (const limit of [1, 2, 5]) { + expect( + textView(text, desktopViewPolicy({ text_lines: limit })), + ).toEqual({ + lines: expected.slice(0, limit), + line_count: expected.length, + truncated: expected.length > limit, + }); + } + }, + ); + it("splits text into lines without a trailing empty line", () => { expect(textView("a\nb\n")).toEqual({ lines: ["a", "b"], diff --git a/src/domain/desktop/stateFormats.ts b/src/domain/desktop/stateFormats.ts index 63e99c6070..f84d3e4547 100644 --- a/src/domain/desktop/stateFormats.ts +++ b/src/domain/desktop/stateFormats.ts @@ -370,12 +370,23 @@ export const textView = ( text: string, policy: DesktopViewPolicy = DESKTOP_VIEW_DEFAULTS, ): JsonValue => { - const lines = text.split(desktopLineBreak); - if (lines.at(-1) === "") lines.pop(); + const lines: string[] = []; + let lineCount = 0; + let start = 0; + for (const delimiter of text.matchAll(desktopLineBreak)) { + if (lines.length < policy.text_lines) + lines.push(text.slice(start, delimiter.index)); + lineCount += 1; + start = delimiter.index + delimiter[0].length; + } + if (start < text.length) { + if (lines.length < policy.text_lines) lines.push(text.slice(start)); + lineCount += 1; + } return { - lines: lines.slice(0, policy.text_lines), - line_count: lines.length, - truncated: lines.length > policy.text_lines, + lines, + line_count: lineCount, + truncated: lineCount > policy.text_lines, }; }; diff --git a/tests/boundary/providers/desktopExportPython.test.ts b/tests/boundary/providers/desktopExportPython.test.ts index 1e62fe5c70..484a426cea 100644 --- a/tests/boundary/providers/desktopExportPython.test.ts +++ b/tests/boundary/providers/desktopExportPython.test.ts @@ -645,6 +645,29 @@ for name, text in json.load(sys.stdin): return { python, rea }; }; +it("retains the same bounded text prefix and complete count in both readers", async () => { + const files = [ + ["empty.txt", ""], + ["blank.txt", "\n"], + ["mixed.txt", "one\r\ntwo\n\n"], + ["literal.txt", "one\rtwo\u2028three\u0085four\r"], + ["many.txt", "event\r\n".repeat(20_000) + "last\r"], + ] as const; + const views = await textViews(files, desktopViewPolicy({ text_lines: 2 })); + expect(views.python).toEqual(views.rea); + expect(views.rea.map((view) => view.view)).toEqual([ + { lines: [], line_count: 0, truncated: false }, + { lines: [""], line_count: 1, truncated: false }, + { lines: ["one", "two"], line_count: 3, truncated: true }, + { + lines: ["one\rtwo\u2028three\u0085four\r"], + line_count: 1, + truncated: false, + }, + { lines: ["event", "event"], line_count: 20_001, truncated: true }, + ]); +}); + it("applies the selected desktop view budgets equally in TypeScript and emitted Python", async () => { const files = [ ["lines.txt", "one\ntwo\nthree"],