]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib-storage: Optimize setting mail event log prefix
authorTimo Sirainen <timo.sirainen@open-xchange.com>
Mon, 29 Nov 2021 12:26:25 +0000 (13:26 +0100)
committeraki.tuomi <aki.tuomi@open-xchange.com>
Tue, 21 Dec 2021 10:06:58 +0000 (10:06 +0000)
This was much more important before mail event creation was delayed. In one
installation the t_strdup_printf() call itself took about 4% of the total CPU
usage. Now that mail events are delayed, this is likely much less of an issue.
Still, this is easy enough of an optimization that might as well do it.

src/lib-storage/mail.c

index 272db1e1e4a74529aea40bc0561a5e930ef1d69d..412ce14c5682c3524f81259ce1e20f5e879c6283 100644 (file)
@@ -125,6 +125,19 @@ static bool index_mail_get_age_days(struct mail *mail, int *days_r)
        return TRUE;
 }
 
+static const char *mail_event_log_callback(struct mail_private *p)
+{
+       char uid_buf[MAX_INT_STRLEN];
+
+       /* Replace the log prefix callback with an explicit prefix, so this
+          callback doesn't have to be called more than once. */
+       const char *prefix = t_strconcat(
+               p->mail.saving ? "saving UID " : "UID ",
+               dec2str_buf(uid_buf, p->mail.uid), ": ", NULL);
+       event_set_append_log_prefix(p->_event, prefix);
+       return prefix;
+}
+
 void mail_event_create(struct mail *mail)
 {
        struct mail_private *p = (struct mail_private *)mail;
@@ -140,8 +153,11 @@ void mail_event_create(struct mail *mail)
        if (index_mail_get_age_days(mail, &age_days))
                event_add_int(p->_event, "mail_age_days", age_days);
 
-       event_set_append_log_prefix(p->_event, t_strdup_printf(
-               "%sUID %u: ", mail->saving ? "saving " : "", mail->uid));
+       /* The mail events may be created often, but most of the time the log
+          prefix isn't needed. We can save some CPU by delaying the prefix
+          generation until it's actually used. */
+       event_set_log_prefix_callback(p->_event, FALSE,
+                                     mail_event_log_callback, p);
 }
 
 struct event *mail_event(struct mail *mail)