Skip to content

Commit fa596ce

Browse files
committed
perf(cli): buffer scan stdout writes
1 parent 8535f39 commit fa596ce

1 file changed

Lines changed: 32 additions & 3 deletions

File tree

cli/stream_output.mbt

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,47 @@ priv struct ScanOutputState {
33
mut hit_count : Int
44
}
55

6+
///|
7+
priv struct BufferedStdout {
8+
mut contents : StringBuilder
9+
mut length : Int
10+
}
11+
612
///|
713
/// Scans source files and writes results as they are found.
814
///
915
/// Match records are written to standard output. Verbose traversal messages
1016
/// and parse warnings are written to standard error.
1117
pub async fn run_scan_command(options : CliOptions) -> Unit {
12-
run_scan_command_with_writers(options, write_scan_stdout, write_scan_stderr)
18+
let stdout : BufferedStdout = { contents: StringBuilder(), length: 0 }
19+
run_scan_command_with_writers(
20+
options,
21+
output => write_buffered_scan_stdout(stdout, output),
22+
write_scan_stderr,
23+
)
24+
flush_buffered_scan_stdout(stdout)
1325
}
1426

1527
///|
16-
async fn write_scan_stdout(output : String) -> Unit {
17-
@stdio.stdout.write(output)
28+
async fn write_buffered_scan_stdout(
29+
stdout : BufferedStdout,
30+
output : String,
31+
) -> Unit {
32+
stdout.contents.write_string(output)
33+
stdout.length += output.length()
34+
if stdout.length >= 65536 {
35+
flush_buffered_scan_stdout(stdout)
36+
}
37+
}
38+
39+
///|
40+
async fn flush_buffered_scan_stdout(stdout : BufferedStdout) -> Unit {
41+
if stdout.length == 0 {
42+
return
43+
}
44+
@stdio.stdout.write(stdout.contents.to_string())
45+
stdout.contents = StringBuilder()
46+
stdout.length = 0
1847
}
1948

2049
///|

0 commit comments

Comments
 (0)