]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
OPTIM: log: speedup date printing in sess_build_logline() when no encoding is used
authorAurelien DARRAGON <adarragon@haproxy.com>
Thu, 2 May 2024 15:04:28 +0000 (17:04 +0200)
committerWilly Tarreau <w@1wt.eu>
Sat, 4 May 2024 08:13:05 +0000 (10:13 +0200)
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

index d817a58366f3014ab624549e58ff4a16217a3529..c384ab5113445794014260cf44029c6ad261f6c6 100644 (file)
--- 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;