Skip to content

Commit 4d7cb9b

Browse files
hieu-leeclaude
andauthored
perf(effect-acp): frame large ACP stdout lines in linear time (pingdotgg#26686)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent b65a64b commit 4d7cb9b

2 files changed

Lines changed: 40 additions & 32 deletions

File tree

‎packages/effect-acp/src/protocol.test.ts‎

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -108,37 +108,39 @@ it.layer(NodeServices.layer)("effect-acp protocol", (it) => {
108108
},
109109
});
110110

111-
yield* Queue.offer(
112-
input,
113-
yield* encodeJsonl(SessionUpdateNotification, {
114-
jsonrpc: "2.0",
115-
method: "session/update",
116-
params: {
117-
sessionId: "session-1",
118-
update: {
119-
sessionUpdate: "plan",
120-
entries: [
121-
{
122-
content: "Inspect repository",
123-
priority: "high",
124-
status: "in_progress",
125-
},
126-
],
127-
},
111+
const updateFrame = yield* encodeJsonl(SessionUpdateNotification, {
112+
jsonrpc: "2.0",
113+
method: "session/update",
114+
params: {
115+
sessionId: "session-1",
116+
update: {
117+
sessionUpdate: "plan",
118+
entries: [
119+
{
120+
content: "Inspect repository",
121+
priority: "high",
122+
status: "in_progress",
123+
},
124+
],
128125
},
129-
}),
130-
);
126+
},
127+
});
128+
const completionFrame = yield* encodeJsonl(ElicitationCompleteNotification, {
129+
jsonrpc: "2.0",
130+
method: "elicitation/complete",
131+
params: {
132+
elicitationId: "elicitation-1",
133+
},
134+
});
131135

132-
yield* Queue.offer(
133-
input,
134-
yield* encodeJsonl(ElicitationCompleteNotification, {
135-
jsonrpc: "2.0",
136-
method: "elicitation/complete",
137-
params: {
138-
elicitationId: "elicitation-1",
139-
},
140-
}),
141-
);
136+
// Frames split across reads, with one read ending one frame and starting the next.
137+
const wire = new Uint8Array([...updateFrame, ...completionFrame]);
138+
const splits = [10, updateFrame.length + 10, wire.length];
139+
let start = 0;
140+
for (const end of splits) {
141+
yield* Queue.offer(input, wire.subarray(start, end));
142+
start = end;
143+
}
142144

143145
const [update, completion] = yield* Deferred.await(notifications);
144146
assert.equal(update?._tag, "SessionUpdate");

‎packages/effect-acp/src/protocol.ts‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,9 +169,15 @@ const makeStrictNdJsonRpcParser = () => {
169169
let buffer = "";
170170
return {
171171
decode: (bytes: Uint8Array | string): ReadonlyArray<unknown> => {
172-
buffer += typeof bytes === "string" ? bytes : decoder.decode(bytes, { stream: true });
173-
const lines = buffer.split("\n");
174-
buffer = lines.pop() ?? "";
172+
const chunk = typeof bytes === "string" ? bytes : decoder.decode(bytes, { stream: true });
173+
const lastNewline = chunk.lastIndexOf("\n");
174+
// Splitting the whole buffer per chunk is quadratic for multi-megabyte lines.
175+
if (lastNewline === -1) {
176+
buffer += chunk;
177+
return [];
178+
}
179+
const lines = (buffer + chunk.slice(0, lastNewline)).split("\n");
180+
buffer = chunk.slice(lastNewline + 1);
175181
return lines.flatMap((line) => codec.decode(line));
176182
},
177183
encode: (response: Parameters<typeof codec.encode>[0]) => {

0 commit comments

Comments
 (0)