Skip to content
TypeScript

perf(effect-acp): frame large ACP stdout lines in linear time (#26686)

Hieu Le4d7cb9bc2ee7
diff --git a/packages/effect-acp/src/protocol.test.ts b/packages/effect-acp/src/protocol.test.ts
index 7b2a923e7b0..eae3394a165 100644
--- a/packages/effect-acp/src/protocol.test.ts
+++ b/packages/effect-acp/src/protocol.test.ts
@@ -108,37 +108,39 @@ it.layer(NodeServices.layer)("effect-acp protocol", (it) => {
},
});
- yield* Queue.offer(
- input,
- yield* encodeJsonl(SessionUpdateNotification, {
- jsonrpc: "2.0",
- method: "session/update",
- params: {
- sessionId: "session-1",
- update: {
- sessionUpdate: "plan",
- entries: [
- {
- content: "Inspect repository",
- priority: "high",
- status: "in_progress",
- },
- ],
- },
+ const updateFrame = yield* encodeJsonl(SessionUpdateNotification, {
+ jsonrpc: "2.0",
+ method: "session/update",
+ params: {
+ sessionId: "session-1",
+ update: {
+ sessionUpdate: "plan",
+ entries: [
+ {
+ content: "Inspect repository",
+ priority: "high",
+ status: "in_progress",
+ },
+ ],
},
- }),
- );
+ },
+ });
+ const completionFrame = yield* encodeJsonl(ElicitationCompleteNotification, {
+ jsonrpc: "2.0",
+ method: "elicitation/complete",
+ params: {
+ elicitationId: "elicitation-1",
+ },
+ });
- yield* Queue.offer(
- input,
- yield* encodeJsonl(ElicitationCompleteNotification, {
- jsonrpc: "2.0",
- method: "elicitation/complete",
- params: {
- elicitationId: "elicitation-1",
- },
- }),
- );
+ // Frames split across reads, with one read ending one frame and starting the next.
+ const wire = new Uint8Array([...updateFrame, ...completionFrame]);
+ const splits = [10, updateFrame.length + 10, wire.length];
+ let start = 0;
+ for (const end of splits) {
+ yield* Queue.offer(input, wire.subarray(start, end));
+ start = end;
+ }
const [update, completion] = yield* Deferred.await(notifications);
assert.equal(update?._tag, "SessionUpdate");
diff --git a/packages/effect-acp/src/protocol.ts b/packages/effect-acp/src/protocol.ts
index feb544ba12a..8763e66361e 100644
--- a/packages/effect-acp/src/protocol.ts
+++ b/packages/effect-acp/src/protocol.ts
@@ -169,9 +169,15 @@ const makeStrictNdJsonRpcParser = () => {
let buffer = "";
return {
decode: (bytes: Uint8Array | string): ReadonlyArray<unknown> => {
- buffer += typeof bytes === "string" ? bytes : decoder.decode(bytes, { stream: true });
- const lines = buffer.split("\n");
- buffer = lines.pop() ?? "";
+ const chunk = typeof bytes === "string" ? bytes : decoder.decode(bytes, { stream: true });
+ const lastNewline = chunk.lastIndexOf("\n");
+ // Splitting the whole buffer per chunk is quadratic for multi-megabyte lines.
+ if (lastNewline === -1) {
+ buffer += chunk;
+ return [];
+ }
+ const lines = (buffer + chunk.slice(0, lastNewline)).split("\n");
+ buffer = chunk.slice(lastNewline + 1);
return lines.flatMap((line) => codec.decode(line));
},
encode: (response: Parameters<typeof codec.encode>[0]) => {