Skip to content

Commit daeb4ee

Browse files
committed
Retain a reading of an event, and read a history through one buffer
Sealing treated any frozen plain container as safe to share. Freezing keeps whatever accessors a container was declared with, so a caller's frozen object with one getter stayed a live reading of the caller's own state inside the retained history: after the record was written, changing that state changed what readAll answered while the file kept what it wrote. A container is now shared only when every own property is a plain value, and is copied otherwise, which is what turns a live object into the fixed reading a retained event is. Shared graphs, primitive-string sharing, fresh membership arrays and the order of persistence before observation are unchanged. Reading a history allocated a fresh megabyte buffer for every read, so opening a 500 MB history allocated the whole of it again a megabyte at a time, on top of reading it. The reader owns one buffer and hands out views of it; every consumer reads a chunk at once, and the one that keeps bytes copies them. Across five runs of the same ten-entry journey over a 508 MB history, the compiled REPL's maximum resident size fell from a mean of 3.40 GB to 2.64 GB. A summary of a binding no longer writes the whole value out to keep twenty columns of it: strings are cut to more than any summary can show before the value is written, which leaves every summary exactly as it was. That bounds a per-frame allocation proportional to the value; it did not move the measured peak, because the right-hand pane shows the Sidekick unless a reader selects the bindings.
1 parent 6e55b81 commit daeb4ee

4 files changed

Lines changed: 281 additions & 9 deletions

File tree

‎packages/cli/src/repl/application.ts‎

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3656,13 +3656,55 @@ function shortened(text: string): string {
36563656

36573657
/** How a value reads in one line of a list. */
36583658
function summarize(value: Json): string {
3659-
const written = JSON.stringify(value) ?? "null";
3659+
const written = JSON.stringify(cut(value)) ?? "null";
36603660
return written.length <= SUMMARY_WIDTH ? written : `${written.slice(0, SUMMARY_WIDTH - 1)}…`;
36613661
}
36623662

36633663
/** How much of a value one line of a list shows. */
36643664
const SUMMARY_WIDTH = 20;
36653665

3666+
/**
3667+
* The same value with every long string cut to more than a summary can show.
3668+
*
3669+
* A binding is as long as whatever the run bound to it, and a captured command
3670+
* output is megabytes. Writing one out to keep its first twenty columns
3671+
* allocated the whole of it, escapes and all, every time a frame was drawn —
3672+
* which is on every keystroke as well as on every record.
3673+
*
3674+
* What a summary shows is unchanged, and that is a property of where the cut
3675+
* is rather than of the shape of the value. Writing a string escapes each
3676+
* character into one or more characters and never into none, so a string cut
3677+
* to {@link SUMMARY_KEEP} characters writes at least that many — and
3678+
* {@link SUMMARY_KEEP} is beyond the columns a summary keeps. A cut therefore
3679+
* moves no character a reader sees, and a value that holds nothing long enough
3680+
* to cut is written exactly as it was.
3681+
*/
3682+
function cut(value: Json): Json {
3683+
if (typeof value === "string") {
3684+
return value.length <= SUMMARY_KEEP ? value : value.slice(0, SUMMARY_KEEP);
3685+
}
3686+
if (value === null || typeof value !== "object") {
3687+
return value;
3688+
}
3689+
if (Array.isArray(value)) {
3690+
return value.map(cut);
3691+
}
3692+
const shorter: { [key: string]: Json } = {};
3693+
for (const [key, member] of Object.entries(value)) {
3694+
shorter[key] = cut(member);
3695+
}
3696+
return shorter;
3697+
}
3698+
3699+
/**
3700+
* How much of one string a summary may need.
3701+
*
3702+
* Twice the columns it keeps, which is more than escaping can consume: the
3703+
* summary keeps `SUMMARY_WIDTH - 1` characters of written text, and this many
3704+
* characters of a string write as at least this many.
3705+
*/
3706+
const SUMMARY_KEEP = SUMMARY_WIDTH * 2;
3707+
36663708
/**
36673709
* One long string as rows that fit, in order, losing nothing.
36683710
*

‎packages/cli/src/repl/record-file.ts‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,16 @@ export const READ_CHUNK = 1 << 20;
2929

3030
const NEWLINE = 0x0a;
3131

32-
/** One file's bytes, in order, a chunk at a time. */
32+
/**
33+
* One file's bytes, in order, a chunk at a time.
34+
*
35+
* Each chunk is a view of the one buffer this reader owns, so a chunk is what
36+
* the file held until the next one is asked for. Every consumer here reads it
37+
* at once — hashing it, scanning it for a record boundary, decoding it — and
38+
* one that needs to keep bytes copies them. A buffer per read would allocate
39+
* the whole file again in a megabyte at a time, which is what reading a
40+
* half-gigabyte history used to do on top of reading it.
41+
*/
3342
interface FileChunks {
3443
/** The next chunk, or nothing once the file has been read to its end. */
3544
next(): Operation<Uint8Array | undefined>;
@@ -57,9 +66,9 @@ function useFileChunks(path: string): Operation<FileChunks> {
5766
});
5867
opening = open(path, "r");
5968
const handle = yield* until(opening);
69+
const buffer = new Uint8Array(READ_CHUNK);
6070
yield* provide({
6171
*next(): Operation<Uint8Array | undefined> {
62-
const buffer = new Uint8Array(READ_CHUNK);
6372
const read = handle.read(buffer, 0, READ_CHUNK, null);
6473
reading = read;
6574
const { bytesRead } = yield* until(read);

‎packages/cli/src/repl/retained-event.ts‎

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -158,12 +158,32 @@ function sealJson(value: Json, share: StringSharing | undefined): Json {
158158
/**
159159
* Whether this container can be shared as it is.
160160
*
161-
* Frozen, and a plain object or array: a class instance can answer its members
162-
* from accessors, so freezing it says nothing about what it will read as next.
161+
* Frozen is not enough on its own, and this is the whole of why sharing is
162+
* decided here rather than at the call sites. Freezing an object keeps whatever
163+
* accessors it was declared with, and an accessor answers from wherever its
164+
* getter reads — so a frozen container holding one getter is a live reading of
165+
* somebody else's state rather than a record of what was retained, and the
166+
* value it reads as can change after the record it came from was written. A
167+
* class instance answers the same way from a field nothing here can see.
168+
*
169+
* So a container is shared only when it is frozen, its prototype is the
170+
* ordinary one, and every own property is a plain value. Anything else is
171+
* copied: reading each member once is what turns a live object into the fixed
172+
* reading a retained event is.
163173
*/
164174
function reusable(value: object): boolean {
175+
if (!Object.isFrozen(value)) {
176+
return false;
177+
}
165178
const prototype = Object.getPrototypeOf(value);
166-
return (
167-
Object.isFrozen(value) && (prototype === Object.prototype || prototype === Array.prototype)
168-
);
179+
if (prototype !== Object.prototype && prototype !== Array.prototype) {
180+
return false;
181+
}
182+
for (const key of Reflect.ownKeys(value)) {
183+
const descriptor = Object.getOwnPropertyDescriptor(value, key);
184+
if (descriptor === undefined || !("value" in descriptor)) {
185+
return false;
186+
}
187+
}
188+
return true;
169189
}

‎packages/cli/tests/repl-retained-history.test.ts‎

Lines changed: 202 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
* supported length is refused by a sentence naming the limit.
1919
* - **Exact bytes.** The Sidekick's prefix bindings and the single-file-to-pair
2020
* upgrade measure and copy the file's own bytes through the same reader.
21+
* - **Bounded reading of a long value.** What the screen says a binding is
22+
* stays what it said, without writing out a value to show twenty columns of
23+
* it.
2124
*
2225
* Files are real, in a per-row temporary directory, because what is under test
2326
* is how bytes on disk are read; the chunk size comes from the reader itself so
@@ -36,14 +39,25 @@ import type { Operation } from "effection";
3639
import { parseDurableEvent, serializeDurableEvent } from "@executablemd/durable-streams";
3740
import type { DurableEvent } from "@executablemd/durable-streams";
3841

42+
import { INLINE_SOURCE_PATH } from "@executablemd/core";
43+
import type { Json } from "@executablemd/durable-streams";
44+
45+
import { describeApplication, initialState, NO_AGENT, viewFor } from "../src/repl/application.ts";
46+
import type { ReplLive } from "../src/repl/application.ts";
47+
import { NO_LIFECYCLE } from "../src/repl/lifecycle.ts";
48+
import { fields, readDescription } from "../src/repl/description.ts";
49+
import type { ReplRoute } from "../src/repl/route.ts";
50+
import type { ReplRenderer } from "../src/repl/renderer.ts";
51+
import type { ReplTerminalSize } from "../src/repl/terminal.ts";
52+
import { committedContext, useMeasuringRenderer } from "./fixtures/repl/presentation.ts";
3953
import { ReplHost } from "../src/repl/host.ts";
4054
import {
4155
HISTORY_LIMIT,
4256
openExecutionFile,
4357
readRecords,
4458
RetainedReplStream,
4559
} from "../src/repl/journal.ts";
46-
import { EntrySegmentStream } from "../src/repl/entries.ts";
60+
import { EntrySegmentStream, ROOT_IMPORT } from "../src/repl/entries.ts";
4761
import { projectRepl } from "../src/repl/model.ts";
4862
import { READ_CHUNK } from "../src/repl/record-file.ts";
4963
import {
@@ -247,6 +261,64 @@ describe("#992 — retained events are kept once and shared", () => {
247261
expect(held[0]).toBe(before[0]);
248262
});
249263

264+
it("H1: a frozen accessor operand is retained as the reading that was written", function* () {
265+
const directory = yield* useTempDirectory("xmd-retained-");
266+
yield* useHost("h1f");
267+
const path = join(directory, "h1f.jsonl");
268+
const physical = new RetainedReplStream(path);
269+
const observed: DurableEvent[] = [];
270+
physical.onAppend = (event) => {
271+
observed.push(event);
272+
};
273+
const segment = new EntrySegmentStream(physical);
274+
275+
// The caller's own state, read through getters, inside frozen containers.
276+
// Frozen says nothing about what a getter will answer next, so a retained
277+
// event that kept these objects would be a live reading of this state
278+
// rather than a record of what was written.
279+
const caller = { answer: "acknowledged", held: ["kept"] };
280+
const value: { readonly nested: { readonly answer: string } } = Object.freeze({
281+
nested: Object.freeze({
282+
get answer(): string {
283+
return caller.answer;
284+
},
285+
}),
286+
});
287+
yield* segment.append({
288+
type: "yield",
289+
coroutineId: "root",
290+
description: Object.freeze({
291+
type: "test_effect",
292+
name: "accessor",
293+
get held(): string[] {
294+
return caller.held;
295+
},
296+
}),
297+
result: { status: "ok", value },
298+
});
299+
const written = yield* until(readFile(path, "utf8"));
300+
301+
caller.answer = "changed after acknowledgement";
302+
caller.held.push("added later");
303+
304+
// Every reader, every time, still describes the record the file holds.
305+
for (const read of [
306+
yield* physical.readAll(),
307+
yield* segment.readAll(),
308+
yield* physical.readAll(),
309+
yield* segment.readAll(),
310+
observed,
311+
]) {
312+
expect(read).toHaveLength(1);
313+
expect(read.map(serializeDurableEvent).join("")).toBe(written);
314+
}
315+
// And they are still one shared graph rather than a copy each.
316+
expect((yield* physical.readAll())[0]).toBe((yield* segment.readAll())[0]);
317+
// The caller's own objects are untouched and still answer for the caller.
318+
expect(value.nested.answer).toBe("changed after acknowledgement");
319+
expect(caller.held).toEqual(["kept", "added later"]);
320+
});
321+
250322
it("H1: sealing keeps member order, absent and undefined members, and an own __proto__", function* () {
251323
const directory = yield* useTempDirectory("xmd-retained-");
252324
yield* useHost("h1d");
@@ -492,3 +564,132 @@ describe("#992 — every saved history reads as it did", () => {
492564
}
493565
});
494566
});
567+
568+
describe("#992 — a long value is summarized without being written out", () => {
569+
const EXECUTION = "summary";
570+
const SCREEN: ReplTerminalSize = { columns: 160, rows: 36 };
571+
const NOTHING_LIVE: ReplLive = Object.freeze({
572+
output: "",
573+
question: undefined,
574+
expansion: "playing",
575+
pausable: false,
576+
running: false,
577+
agent: NO_AGENT,
578+
lifecycle: NO_LIFECYCLE,
579+
});
580+
581+
/** One settled entry that published `held`, as a run records one. */
582+
function publishing(value: Json): DurableEvent[] {
583+
const content = "```js eval\nconst held = 1;\n```\n";
584+
return [
585+
{
586+
type: "yield",
587+
coroutineId: "root",
588+
description: { type: "import_component", name: ROOT_IMPORT },
589+
result: {
590+
status: "ok",
591+
value: { kind: "repository", path: INLINE_SOURCE_PATH, content },
592+
},
593+
},
594+
{
595+
type: "yield",
596+
coroutineId: "root",
597+
description: {
598+
type: "eval",
599+
name: "eval:eval:root:0",
600+
language: "js",
601+
"executablemd.source-position": {
602+
path: INLINE_SOURCE_PATH,
603+
offset: 0,
604+
line: 1,
605+
column: 1,
606+
},
607+
},
608+
result: { status: "ok", value: { value: { held: value } } },
609+
},
610+
{
611+
type: "close",
612+
coroutineId: "root",
613+
result: {
614+
status: "ok",
615+
value: { status: "ok", output: "", value: "", bindings: { held: value } },
616+
},
617+
},
618+
];
619+
}
620+
621+
/** What the binding rows of this reading say, as a reader reads them. */
622+
function* bindingRows(value: Json, engine: ReplRenderer): Operation<string[]> {
623+
const projected = projectRepl(publishing(value));
624+
if (!projected.ok) {
625+
throw projected.error;
626+
}
627+
const opening = initialState(EXECUTION);
628+
// The bindings pane explicitly: the right-hand column defaults to the
629+
// Sidekick, so this asks for the pane the way a person does.
630+
const route: ReplRoute = { ...opening.route, scopes: ["entry-1"], right: "bindings" };
631+
const view = viewFor(
632+
Object.freeze({ ...opening, route }),
633+
projected.value,
634+
NOTHING_LIVE,
635+
SCREEN,
636+
);
637+
if (!view.ok) {
638+
throw view.error;
639+
}
640+
const context = yield* committedContext(engine, view.value);
641+
const rows: string[] = [];
642+
for (const description of describeApplication(view.value, context)) {
643+
const read = readDescription(description);
644+
const label = fields(read.input)?.["label"];
645+
if (read.key.startsWith("binding:") && typeof label === "string") {
646+
rows.push(label.trim());
647+
}
648+
}
649+
return rows;
650+
}
651+
652+
/**
653+
* The summary writing the whole value out produced.
654+
*
655+
* Twenty columns, with the twentieth given to an ellipsis where there was
656+
* more — stated here as the contract rather than read from the screen's own
657+
* constant, so a changed constant fails this row instead of agreeing with it.
658+
*/
659+
function whole(value: Json): string {
660+
const written = JSON.stringify(value) ?? "null";
661+
return written.length <= 20 ? written : `${written.slice(0, 19)}\u2026`;
662+
}
663+
664+
it("H6: a value too long to show reads exactly as writing it out whole did", function* () {
665+
const engine = yield* useMeasuringRenderer(SCREEN);
666+
const values: Json[] = [
667+
// Long enough that a summary is a cut, in the shapes a cut could move:
668+
// plain text, text whose every character escapes into two, quotes at the
669+
// front, characters outside the basic plane, and long text nested in a
670+
// container and in a list.
671+
"a".repeat(5000),
672+
"\n".repeat(5000),
673+
`${'"'.repeat(10)}${"b".repeat(5000)}`,
674+
"\u{1f600}".repeat(3000),
675+
`\u2014${"c".repeat(5000)}`,
676+
{ log: "d".repeat(5000) },
677+
["e".repeat(5000)],
678+
{ first: "f".repeat(5000), second: "g".repeat(5000) },
679+
// And the lengths either side of the twenty columns a summary keeps, so
680+
// the row that is shown whole is shown whole.
681+
"h".repeat(17),
682+
"i".repeat(18),
683+
"j".repeat(19),
684+
"alpha",
685+
42,
686+
null,
687+
true,
688+
{ a: 1 },
689+
[],
690+
];
691+
for (const value of values) {
692+
expect(yield* bindingRows(value, engine)).toEqual([`held = ${whole(value)}`]);
693+
}
694+
});
695+
});

0 commit comments

Comments
 (0)