Skip to content

Commit 9f660d3

Browse files
perf(server): speed up large terminal clipboard sequences (pingdotgg#23819)
Co-authored-by: Julius Marminge <[email protected]>
1 parent 97be291 commit 9f660d3

2 files changed

Lines changed: 66 additions & 2 deletions

File tree

‎apps/server/src/terminal/Manager.test.ts‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -803,6 +803,62 @@ it.layer(
803803
}),
804804
);
805805

806+
it.effect("preserves large chunked OSC 52 and DCS strings across reopen", () =>
807+
Effect.gen(function* () {
808+
const { manager, ptyAdapter, logsDir } = yield* createManager();
809+
yield* manager.open(openInput());
810+
const process = ptyAdapter.processes[0]!;
811+
const outputSeen = yield* Deferred.make<void>();
812+
const unsubscribe = yield* manager.subscribe((event) =>
813+
event.type === "output" && event.data === "done\n"
814+
? Deferred.succeed(outputSeen, undefined).pipe(Effect.asVoid)
815+
: Effect.void,
816+
);
817+
yield* Effect.addFinalizer(() => Effect.sync(unsubscribe));
818+
const sequences = [
819+
{ prefix: `\u001b]52;c;${"YWJj".repeat(16_384)}\u001b`, terminator: "\\" },
820+
{ prefix: `\u0090q${"?".repeat(65_536)}`, terminator: "\u009c" },
821+
];
822+
const stripped = "\u001b]11;?\u0007\u0090$q m\u009c";
823+
const preserved = "\u001b[31m\u009d52;c;YWJj\u009c";
824+
const expected =
825+
sequences.map(({ prefix, terminator }) => prefix + terminator + preserved).join("") +
826+
"done\n";
827+
828+
// Counts reads of strings larger than one emitted chunk, so rescanning the
829+
// pending prefix on every chunk (#23817) shows up as quadratic work.
830+
const charCodeAt = String.prototype.charCodeAt;
831+
let largeStringReads = 0;
832+
yield* Effect.gen(function* () {
833+
// eslint-disable-next-line no-extend-native -- Count reads without retaining millions of spy call records.
834+
String.prototype.charCodeAt = function (this: string, index: number) {
835+
if (this.length > 4_096) largeStringReads += 1;
836+
return charCodeAt.call(this, index);
837+
};
838+
for (const { prefix, terminator } of sequences) {
839+
for (let offset = 0; offset < prefix.length; offset += 4_096) {
840+
process.emitData(prefix.slice(offset, offset + 4_096));
841+
}
842+
process.emitData(terminator + stripped + preserved);
843+
}
844+
process.emitData("done\n");
845+
yield* Deferred.await(outputSeen);
846+
}).pipe(
847+
Effect.ensuring(
848+
Effect.sync(() => {
849+
// eslint-disable-next-line no-extend-native -- Restore the counting hook even when the test fails.
850+
String.prototype.charCodeAt = charCodeAt;
851+
}),
852+
),
853+
);
854+
yield* manager.close({ threadId: "thread-1" });
855+
856+
expect(largeStringReads).toBeLessThan(2 * expected.length);
857+
expect(yield* readFileString(yield* historyLogPath(logsDir))).toBe(expected);
858+
expect((yield* manager.open(openInput())).history.startsWith(expected)).toBe(true);
859+
}),
860+
);
861+
806862
it.effect(
807863
"preserves chunk-split ESC sequences with intermediate bytes without leaking final bytes",
808864
() =>

‎apps/server/src/terminal/Manager.ts‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1147,6 +1147,8 @@ function sanitizeTerminalHistoryChunk(
11471147
}
11481148

11491149
const input = `${pendingControlSequence}${data}`;
1150+
// Only the pending prefix's final ESC can begin a terminator split across chunks.
1151+
const pendingScanStart = Math.max(0, pendingControlSequence.length - 1);
11501152
let visibleText = "";
11511153
let index = 0;
11521154

@@ -1189,7 +1191,10 @@ function sanitizeTerminalHistoryChunk(
11891191
nextCodePoint === 0x5e ||
11901192
nextCodePoint === 0x5f
11911193
) {
1192-
const terminatorIndex = findStringTerminatorIndex(input, index + 2);
1194+
const terminatorIndex = findStringTerminatorIndex(
1195+
input,
1196+
Math.max(index + 2, pendingScanStart),
1197+
);
11931198
if (terminatorIndex === null) {
11941199
return { visibleText, pendingControlSequence: input.slice(index) };
11951200
}
@@ -1235,7 +1240,10 @@ function sanitizeTerminalHistoryChunk(
12351240
}
12361241

12371242
if (codePoint === 0x9d || codePoint === 0x90 || codePoint === 0x9e || codePoint === 0x9f) {
1238-
const terminatorIndex = findStringTerminatorIndex(input, index + 1);
1243+
const terminatorIndex = findStringTerminatorIndex(
1244+
input,
1245+
Math.max(index + 1, pendingScanStart),
1246+
);
12391247
if (terminatorIndex === null) {
12401248
return { visibleText, pendingControlSequence: input.slice(index) };
12411249
}

0 commit comments

Comments
 (0)