From 539a117e1394aa73aa4bd6894d18ffd92e23251f Mon Sep 17 00:00:00 2001 From: qs Date: Fri, 14 Aug 2026 21:57:33 +0300 Subject: [PATCH] perf: read the pty in 64 KB chunks instead of 1 KB --- src/flutter_pty_unix.c | 13 ++++++++++++- src/flutter_pty_win.c | 6 +++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/flutter_pty_unix.c b/src/flutter_pty_unix.c index 829b8e5..8afda93 100644 --- a/src/flutter_pty_unix.c +++ b/src/flutter_pty_unix.c @@ -42,11 +42,22 @@ typedef struct ReadLoopOptions char *error_message = NULL; +// One read is one Dart port message, and each message costs a typed-data +// allocation, a stream event and the GC that follows. The read SIZE therefore +// decides throughput under heavy output far more than the byte count does: at +// 1 KB, 15 MB of output became ~15,000 messages and the isolate spent its time +// on message machinery instead of the terminal. +// +// read() returns as soon as any data is available and never waits to fill this, +// so an echoed keystroke still arrives in one small read — interactive latency +// is unaffected. +#define PTY_READ_BUFFER_SIZE (64 * 1024) + static void *read_loop(void *arg) { ReadLoopOptions *options = (ReadLoopOptions *)arg; - char buffer[1024]; + char buffer[PTY_READ_BUFFER_SIZE]; while (1) { diff --git a/src/flutter_pty_win.c b/src/flutter_pty_win.c index b1f85bc..0096117 100644 --- a/src/flutter_pty_win.c +++ b/src/flutter_pty_win.c @@ -161,11 +161,15 @@ typedef struct ReadLoopOptions } ReadLoopOptions; +// See the note in flutter_pty_unix.c — one read is one Dart port message, so the +// read size is what decides throughput under heavy output. +#define PTY_READ_BUFFER_SIZE (64 * 1024) + static DWORD WINAPI read_loop(LPVOID arg) { ReadLoopOptions *options = (ReadLoopOptions *)arg; - char buffer[1024]; + char buffer[PTY_READ_BUFFER_SIZE]; while (1) {