]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib: ostream-file - Shrink buffer to optimal size after it is flushed
authorTimo Sirainen <timo.sirainen@open-xchange.com>
Fri, 13 Mar 2026 11:36:47 +0000 (13:36 +0200)
committeraki.tuomi <aki.tuomi@open-xchange.com>
Sat, 14 Mar 2026 19:06:15 +0000 (19:06 +0000)
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.

src/lib/ostream-file.c

index df1ea273649a58f7d2840327c412f909e2b913dc..2bc66098fa372921b78b46f2f4e31c12a896f320 100644 (file)
@@ -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)