xonecas.io
64875
Mc

mini-coder / commit b2c63b8

perf: fix O(n²) rendering in large reasoning blocks

Three issues that made reasoning blocks lag for minutes:

1. flushCompleteLines was O(n²): each iteration did
   rawBuffer = rawBuffer.slice(boundary + 1), copying all remaining
   content after every line. A 1000-line block caused ~25M chars of
   redundant copying in a single flush call. Fixed by tracking a start
   cursor and doing one rawBuffer.slice(start) at the end.

2. flushCompleteLines called process.stdout.write once per line,
   causing thousands of individual terminal syscalls for large blocks.
   Fixed by accumulating all rendered lines into batchOutput and
   emitting a single write() per flush invocation.

3. renderInline built output character-by-character (out += text[i])
   and allocated a new 2-char string on every iteration for the bold
   check (text.slice(i, i+2)). Fixed with segment-based building:
   plain-text runs are flushed via a single text.slice(segStart, i),
   and bold detection uses text[i+1] === '*' comparisons directly.

xonecas2026-03-13 b2c63b85b77fd063830a7432dcc3b155553d6f8b parent 70dbd9a

2 files changed

src/cli/markdown.ts+41−27
@@ -6,6 +6,11 @@ import * as c from "yoctocolors";
66 function renderInline(text: string): string {
77 let out = "";
88 let i = 0;
9+ // segStart marks the beginning of the current plain-text segment that has
10+ // not yet been flushed to `out`. When we hit a recognised span we flush the
11+ // accumulated plain text in one slice instead of appending character by
12+ // character.
13+ let segStart = 0;
914 // Track whether the previous character in the *source* was consumed as part
1015 // of a bold closing **. This lets the italic branch distinguish "the * right
1116 // after **bold**" (safe to start italic) from "the second * of an unclosed **"
@@ -13,51 +18,60 @@ function renderInline(text: string): string {
1318 let prevWasBoldClose = false;
1419
1520 while (i < text.length) {
21+ const ch = text[i] as string;
22+
1623 // Inline code: `...`
17- if (text[i] === "`") {
24+ if (ch === "`") {
1825 const end = text.indexOf("`", i + 1);
1926 if (end !== -1) {
27+ if (i > segStart) out += text.slice(segStart, i);
2028 out += c.yellow(text.slice(i, end + 1));
2129 i = end + 1;
30+ segStart = i;
2231 prevWasBoldClose = false;
2332 continue;
2433 }
2534 }
2635
27- // Bold: **...** (opening ** must not be followed by a third *)
28- if (text.slice(i, i + 2) === "**" && text[i + 2] !== "*") {
29- const end = text.indexOf("**", i + 2);
30- if (end !== -1) {
31- out += c.bold(text.slice(i + 2, end));
32- i = end + 2;
33- prevWasBoldClose = true;
34- continue;
36+ if (ch === "*") {
37+ // Bold: **...** (opening ** must not be followed by a third *)
38+ // Compare individual chars instead of allocating a 2-char slice.
39+ if (text[i + 1] === "*" && text[i + 2] !== "*") {
40+ const end = text.indexOf("**", i + 2);
41+ if (end !== -1) {
42+ if (i > segStart) out += text.slice(segStart, i);
43+ out += c.bold(text.slice(i + 2, end));
44+ i = end + 2;
45+ segStart = i;
46+ prevWasBoldClose = true;
47+ continue;
48+ }
3549 }
36- }
3750
38- // Italic: *...* — opening * must not be part of ** (next char is *).
39- // We also require that the previous source character was NOT a * UNLESS
40- // we just closed a bold span (in which case the adjacent * is a legitimate
41- // italic opener, not the second star of a ** pair).
42- if (
43- text[i] === "*" &&
44- text[i + 1] !== "*" &&
45- (prevWasBoldClose || text[i - 1] !== "*")
46- ) {
47- const end = text.indexOf("*", i + 1);
48- if (end !== -1 && text[end - 1] !== "*") {
49- out += c.dim(text.slice(i + 1, end));
50- i = end + 1;
51- prevWasBoldClose = false;
52- continue;
51+ // Italic: *...* — opening * must not be part of ** (next char is *).
52+ // We also require that the previous source character was NOT a * UNLESS
53+ // we just closed a bold span (in which case the adjacent * is a legitimate
54+ // italic opener, not the second star of a ** pair).
55+ if (text[i + 1] !== "*" && (prevWasBoldClose || text[i - 1] !== "*")) {
56+ const end = text.indexOf("*", i + 1);
57+ if (end !== -1 && text[end - 1] !== "*") {
58+ if (i > segStart) out += text.slice(segStart, i);
59+ out += c.dim(text.slice(i + 1, end));
60+ i = end + 1;
61+ segStart = i;
62+ prevWasBoldClose = false;
63+ continue;
64+ }
5365 }
5466 }
5567
56- out += text[i];
57- i++;
5868 prevWasBoldClose = false;
69+ i++;
5970 }
6071
72+ // Flush any remaining plain-text segment in one shot.
73+ if (segStart < text.length) out += text.slice(segStart);
74+
6175 return out;
6276 }
6377
src/cli/stream-render.ts+40−14
@@ -39,25 +39,33 @@ export async function renderTurn(
3939 let contextTokens = 0;
4040 let newMessages: CoreMessage[] = [];
4141
42- function renderAndWrite(raw: string, endWithNewline: boolean): void {
43- // Defensive stop: some providers stream tiny chunks and spinner ticks can
44- // interleave with stdout writes unless we stop right before each render.
45- spinner.stop();
46-
42+ /**
43+ * Render a single raw line, updating shared fence/blank-run state.
44+ * Returns the rendered string, or null if the line should be suppressed
45+ * (e.g. a second consecutive blank line inside a reasoning block).
46+ */
47+ function renderSingleLine(raw: string): string | null {
4748 const source = inReasoning ? raw.replace(/[ \t]+$/g, "") : raw;
4849 if (inReasoning && source.trim() === "") {
4950 reasoningBlankLineRun += 1;
50- if (reasoningBlankLineRun > 1) return;
51+ if (reasoningBlankLineRun > 1) return null;
5152 } else if (inReasoning) {
5253 reasoningBlankLineRun = 0;
5354 }
5455
5556 const rendered = renderLine(source, inFence);
5657 inFence = rendered.inFence;
57- const finalOutput = inReasoning
58- ? `${c.dim("│ ")}${rendered.output}`
59- : rendered.output;
60- write(finalOutput);
58+ return inReasoning ? `${c.dim("│ ")}${rendered.output}` : rendered.output;
59+ }
60+
61+ function renderAndWrite(raw: string, endWithNewline: boolean): void {
62+ // Defensive stop: some providers stream tiny chunks and spinner ticks can
63+ // interleave with stdout writes unless we stop right before each render.
64+ spinner.stop();
65+
66+ const out = renderSingleLine(raw);
67+ if (out === null) return;
68+ write(out);
6169 if (endWithNewline) {
6270 write("\n");
6371 }
@@ -74,12 +82,30 @@ export async function renderTurn(
7482 }
7583
7684 function flushCompleteLines(): void {
77- let boundary = rawBuffer.indexOf("\n");
85+ // Use an index cursor instead of repeatedly slicing rawBuffer after each
86+ // line — the original `rawBuffer = rawBuffer.slice(boundary + 1)` pattern
87+ // is O(n²) for large blocks because it copies all remaining content on
88+ // every iteration. We do a single slice at the end instead.
89+ // All rendered lines are also batched into one write() call to avoid the
90+ // overhead of thousands of individual stdout syscalls.
91+ let start = 0;
92+ let boundary = rawBuffer.indexOf("\n", start);
93+ if (boundary === -1) return;
94+
95+ spinner.stop();
96+ let batchOutput = "";
97+
7898 while (boundary !== -1) {
79- renderAndWrite(rawBuffer.slice(0, boundary), true);
80- rawBuffer = rawBuffer.slice(boundary + 1);
81- boundary = rawBuffer.indexOf("\n");
99+ const raw = rawBuffer.slice(start, boundary);
100+ start = boundary + 1;
101+ boundary = rawBuffer.indexOf("\n", start);
102+
103+ const out = renderSingleLine(raw);
104+ if (out !== null) batchOutput += `${out}\n`;
82105 }
106+
107+ rawBuffer = start > 0 ? rawBuffer.slice(start) : rawBuffer;
108+ if (batchOutput) write(batchOutput);
83109 }
84110
85111 function flushAll(): void {