mirror of
https://github.com/morluto/rea.git
synced 2026-11-21 22:15:50 +00:00
perf(desktop): bound retained text lines while counting full files (#10207)
This commit is contained in:
1 parent
ce3506e298
commit
71a7e0d9ff
4 files changed
+65
-9
No files matched your search
@@ -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"]}
|
||||
|
||||
|
||||
|
||||
@@ -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"],
|
||||
|
||||
@@ -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,
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
@@ -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"],
|
||||
|
||||
Reference in new issue
Block a user