From: Timo Sirainen Date: Fri, 13 Mar 2026 11:36:47 +0000 (+0200) Subject: lib: ostream-file - Shrink buffer to optimal size after it is flushed X-Git-Tag: 2.4.3~75 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=116ca47dc83bc8a23014ba3bfae41f187cb7b010;p=thirdparty%2Fdovecot%2Fcore.git lib: ostream-file - Shrink buffer to optimal size after it is flushed If the ostream buffer was temporarily grown large, it was never shrunk back to a small value, causing memory to be wasted for long-lived streams. --- diff --git a/src/lib/ostream-file.c b/src/lib/ostream-file.c index df1ea27364..2bc66098fa 100644 --- a/src/lib/ostream-file.c +++ b/src/lib/ostream-file.c @@ -360,7 +360,16 @@ static int buffer_flush(struct file_ostream *fstream) update_buffer(fstream, ret); } - return IS_STREAM_EMPTY(fstream) ? 1 : 0; + if (!IS_STREAM_EMPTY(fstream)) + return 0; + + if (fstream->buffer_size > fstream->optimal_block_size) { + fstream->buffer = i_realloc(fstream->buffer, + fstream->buffer_size, + fstream->optimal_block_size); + fstream->buffer_size = fstream->optimal_block_size; + } + return 1; } static void o_stream_tcp_flush_via_nodelay(struct file_ostream *fstream)