From d26a1601336a7f345af96548bc18edd202ef2557 Mon Sep 17 00:00:00 2001 From: Aurelien DARRAGON Date: Thu, 2 May 2024 17:04:28 +0200 Subject: [PATCH] OPTIM: log: speedup date printing in sess_build_logline() when no encoding is used In sess_build_logline(), we have multiple fieds such as '%t' that build a fixed-length string out of a date struct and then print it using lf_rawtext(). In fact, printing it using lf_rawtext() is only mandatory to deal with encoding options, but when no encoding is used we can output the result to tmplog directly. Since most dates generate between 25 and 30 chars, doing so spares us from writing them twice and could help make sess_build_logline() a bit faster when no encoding is used. (to match with pre-encoding patch series performance). --- src/log.c | 62 +++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 44 insertions(+), 18 deletions(-) diff --git a/src/log.c b/src/log.c index d817a5836..c384ab511 100644 --- a/src/log.c +++ b/src/log.c @@ -3915,9 +3915,13 @@ int sess_build_logline(struct session *sess, struct stream *s, char *dst, size_t // "26/Apr/2024:09:39:58.774" get_localtime(logs->accept_date.tv_sec, &tm); - if (!date2str_log(ctx->_buf, &tm, &logs->accept_date, sizeof(ctx->_buf))) - goto out; - ret = lf_rawtext(tmplog, ctx->_buf, dst + maxsize - tmplog, ctx); + if (ctx->options & LOG_OPT_ENCODE) { + if (!date2str_log(ctx->_buf, &tm, &logs->accept_date, sizeof(ctx->_buf))) + goto out; + ret = lf_rawtext(tmplog, ctx->_buf, dst + maxsize - tmplog, ctx); + } + else // speedup + ret = date2str_log(tmplog, &tm, &logs->accept_date, dst + maxsize - tmplog); if (ret == NULL) goto out; tmplog = ret; @@ -3931,9 +3935,13 @@ int sess_build_logline(struct session *sess, struct stream *s, char *dst, size_t /* Note that the timers are valid if we get here */ tv_ms_add(&tv, &logs->accept_date, logs->t_idle >= 0 ? logs->t_idle + logs->t_handshake : 0); get_localtime(tv.tv_sec, &tm); - if (!date2str_log(ctx->_buf, &tm, &tv, sizeof(ctx->_buf))) - goto out; - ret = lf_rawtext(tmplog, ctx->_buf, dst + maxsize - tmplog, ctx); + if (ctx->options & LOG_OPT_ENCODE) { + if (!date2str_log(ctx->_buf, &tm, &tv, sizeof(ctx->_buf))) + goto out; + ret = lf_rawtext(tmplog, ctx->_buf, dst + maxsize - tmplog, ctx); + } + else // speedup + ret = date2str_log(tmplog, &tm, &tv, dst + maxsize - tmplog); if (ret == NULL) goto out; tmplog = ret; @@ -3945,9 +3953,13 @@ int sess_build_logline(struct session *sess, struct stream *s, char *dst, size_t // "26/Apr/2024:07:41:11 +0000" get_gmtime(logs->accept_date.tv_sec, &tm); - if (!gmt2str_log(ctx->_buf, &tm, sizeof(ctx->_buf))) - goto out; - ret = lf_rawtext(tmplog, ctx->_buf, dst + maxsize - tmplog, ctx); + if (ctx->options & LOG_OPT_ENCODE) { + if (!gmt2str_log(ctx->_buf, &tm, sizeof(ctx->_buf))) + goto out; + ret = lf_rawtext(tmplog, ctx->_buf, dst + maxsize - tmplog, ctx); + } + else // speedup + ret = gmt2str_log(tmplog, &tm, dst + maxsize - tmplog); if (ret == NULL) goto out; tmplog = ret; @@ -3960,9 +3972,13 @@ int sess_build_logline(struct session *sess, struct stream *s, char *dst, size_t tv_ms_add(&tv, &logs->accept_date, logs->t_idle >= 0 ? logs->t_idle + logs->t_handshake : 0); get_gmtime(tv.tv_sec, &tm); - if (!gmt2str_log(ctx->_buf, &tm, sizeof(ctx->_buf))) - goto out; - ret = lf_rawtext(tmplog, ctx->_buf, dst + maxsize - tmplog, ctx); + if (ctx->options & LOG_OPT_ENCODE) { + if (!gmt2str_log(ctx->_buf, &tm, sizeof(ctx->_buf))) + goto out; + ret = lf_rawtext(tmplog, ctx->_buf, dst + maxsize - tmplog, ctx); + } + else // speedup + ret = gmt2str_log(tmplog, &tm, dst + maxsize - tmplog); if (ret == NULL) goto out; tmplog = ret; @@ -3974,9 +3990,15 @@ int sess_build_logline(struct session *sess, struct stream *s, char *dst, size_t // "26/Apr/2024:09:42:32 +0200" get_localtime(logs->accept_date.tv_sec, &tm); - if (!localdate2str_log(ctx->_buf, logs->accept_date.tv_sec, &tm, sizeof(ctx->_buf))) - goto out; - ret = lf_rawtext(tmplog, ctx->_buf, dst + maxsize - tmplog, ctx); + if (ctx->options & LOG_OPT_ENCODE) { + if (!localdate2str_log(ctx->_buf, logs->accept_date.tv_sec, + &tm, sizeof(ctx->_buf))) + goto out; + ret = lf_rawtext(tmplog, ctx->_buf, dst + maxsize - tmplog, ctx); + } + else // speedup + ret = localdate2str_log(tmplog, logs->accept_date.tv_sec, + &tm, dst + maxsize - tmplog); if (ret == NULL) goto out; tmplog = ret; @@ -3989,9 +4011,13 @@ int sess_build_logline(struct session *sess, struct stream *s, char *dst, size_t tv_ms_add(&tv, &logs->accept_date, logs->t_idle >= 0 ? logs->t_idle + logs->t_handshake : 0); get_localtime(tv.tv_sec, &tm); - if (!localdate2str_log(ctx->_buf, tv.tv_sec, &tm, sizeof(ctx->_buf))) - goto out; - ret = lf_rawtext(tmplog, ctx->_buf, dst + maxsize - tmplog, ctx); + if (ctx->options & LOG_OPT_ENCODE) { + if (!localdate2str_log(ctx->_buf, tv.tv_sec, &tm, sizeof(ctx->_buf))) + goto out; + ret = lf_rawtext(tmplog, ctx->_buf, dst + maxsize - tmplog, ctx); + } + else // speedup + ret = localdate2str_log(tmplog, tv.tv_sec, &tm, dst + maxsize - tmplog); if (ret == NULL) goto out; tmplog = ret;