Skip to content

Commit c31a5e5

Browse files
author
Eric Wheeler
committed
perf: optimize truncateOutput for large inputs
Use string indices to find line boundaries instead of splitting into array. This avoids creating large arrays in memory when truncating big inputs. - Replace split/join with indexOf/lastIndexOf for line counting - Use slice to extract start/end sections directly from string - Maintain same 20/80 ratio for before/after content Signed-off-by: Eric Wheeler <[email protected]>
1 parent 8b8c4fd commit c31a5e5

File tree

1 file changed

+33
-7
lines changed

1 file changed

+33
-7
lines changed

src/integrations/misc/extract-text.ts

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -110,16 +110,42 @@ export function truncateOutput(content: string, lineLimit?: number): string {
110110
return content
111111
}
112112

113-
const lines = content.split("\n")
114-
if (lines.length <= lineLimit) {
113+
// Count total lines
114+
let totalLines = 0
115+
let pos = -1
116+
while ((pos = content.indexOf("\n", pos + 1)) !== -1) {
117+
totalLines++
118+
}
119+
totalLines++ // Account for last line without newline
120+
121+
if (totalLines <= lineLimit) {
115122
return content
116123
}
117124

118125
const beforeLimit = Math.floor(lineLimit * 0.2) // 20% of lines before
119126
const afterLimit = lineLimit - beforeLimit // remaining 80% after
120-
return [
121-
...lines.slice(0, beforeLimit),
122-
`\n[...${lines.length - lineLimit} lines omitted...]\n`,
123-
...lines.slice(-afterLimit),
124-
].join("\n")
127+
128+
// Find start section end position
129+
let startEndPos = -1
130+
let lineCount = 0
131+
pos = 0
132+
while (lineCount < beforeLimit && (pos = content.indexOf("\n", pos)) !== -1) {
133+
startEndPos = pos
134+
lineCount++
135+
pos++
136+
}
137+
138+
// Find end section start position
139+
let endStartPos = content.length
140+
lineCount = 0
141+
pos = content.length
142+
while (lineCount < afterLimit && (pos = content.lastIndexOf("\n", pos - 1)) !== -1) {
143+
endStartPos = pos + 1 // Start after the newline
144+
lineCount++
145+
}
146+
147+
const omittedLines = totalLines - lineLimit
148+
const startSection = content.slice(0, startEndPos + 1)
149+
const endSection = content.slice(endStartPos)
150+
return startSection + `\n[...${omittedLines} lines omitted...]\n\n` + endSection
125151
}

0 commit comments

Comments
 (0)