]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib: Free unused data stack memory once per second while running ioloop
authorTimo Sirainen <timo.sirainen@open-xchange.com>
Fri, 21 May 2021 15:20:10 +0000 (18:20 +0300)
committertimo.sirainen <timo.sirainen@open-xchange.com>
Mon, 24 May 2021 11:52:01 +0000 (11:52 +0000)
This way if the data stack has grown excessively large temporarily, it
won't permanently waste memory. And if the data stack grows back to the
same large size, re-allocating it once per second doesn't cause performance
problems.

src/lib/ioloop.c

index 39ef3a30179e034c3a186a01713330a74455c6f0..7ba0fb67233abcbb77dc82cf1612c397d71a4dba 100644 (file)
@@ -19,6 +19,8 @@ static ARRAY(io_switch_callback_t *) io_switch_callbacks = ARRAY_INIT;
 static ARRAY(io_destroy_callback_t *) io_destroy_callbacks = ARRAY_INIT;
 static bool panic_on_leak = FALSE, panic_on_leak_set = FALSE;
 
+static time_t data_stack_last_free_unused = 0;
+
 static void io_loop_initialize_handler(struct ioloop *ioloop)
 {
        unsigned int initial_fd_count;
@@ -694,6 +696,17 @@ void io_loop_handle_timeouts(struct ioloop *ioloop)
        T_BEGIN {
                io_loop_handle_timeouts_real(ioloop);
        } T_END;
+
+       /* Free the unused memory in data stack once per second. This way if
+          the data stack has grown excessively large temporarily, it won't
+          permanently waste memory. And if the data stack grows back to the
+          same large size, re-allocating it once per second doesn't cause
+          performance problems. */
+       if (data_stack_last_free_unused != ioloop_time) {
+               if (data_stack_last_free_unused != 0)
+                       data_stack_free_unused();
+               data_stack_last_free_unused = ioloop_time;
+       }
 }
 
 void io_loop_call_io(struct io *io)