git: d5c6c2eaacba - stable/12 - sbuf(9): Microoptimize sbuf_put_byte()

From: Alexander Motin <mav_at_FreeBSD.org>
Date: Tue, 19 Oct 2021 15:21:16 UTC
The branch stable/12 has been updated by mav:

URL: https://cgit.FreeBSD.org/src/commit/?id=d5c6c2eaacba9e4272dcc334e2f6bbfc779c5de7

commit d5c6c2eaacba9e4272dcc334e2f6bbfc779c5de7
Author:     Alexander Motin <mav@FreeBSD.org>
AuthorDate: 2021-10-05 18:42:47 +0000
Commit:     Alexander Motin <mav@FreeBSD.org>
CommitDate: 2021-10-19 15:20:20 +0000

    sbuf(9): Microoptimize sbuf_put_byte()
    
    This function is actively used by sbuf_vprintf(), so this simple
    inlining in half reduces time of kern.geom.confxml generation.
    
    MFC after:      2 weeks
    Sponsored by:   iXsystem, Inc.
    
    (cherry picked from commit 7835b2cb4a1ae57f403739a2f1076ec7188f18c9)
---
 sys/kern/subr_sbuf.c | 23 +++++++++++++++++++++--
 1 file changed, 21 insertions(+), 2 deletions(-)

diff --git a/sys/kern/subr_sbuf.c b/sys/kern/subr_sbuf.c
index 4a046cc41787..039d96c9b739 100644
--- a/sys/kern/subr_sbuf.c
+++ b/sys/kern/subr_sbuf.c
@@ -464,7 +464,26 @@ static void
 sbuf_put_byte(struct sbuf *s, char c)
 {
 
-	sbuf_put_bytes(s, &c, 1);
+	assert_sbuf_integrity(s);
+	assert_sbuf_state(s, 0);
+
+	if (__predict_false(s->s_error != 0))
+		return;
+	if (__predict_false(SBUF_FREESPACE(s) <= 0)) {
+		/*
+		 * If there is a drain, use it, otherwise extend the
+		 * buffer.
+		 */
+		if (s->s_drain_func != NULL)
+			(void)sbuf_drain(s);
+		else if (sbuf_extend(s, 1) < 0)
+			s->s_error = ENOMEM;
+		if (s->s_error != 0)
+			return;
+	}
+	s->s_buf[s->s_len++] = c;
+	if (SBUF_ISSECTION(s))
+		s->s_sect_len++;
 }
 
 /*
@@ -607,7 +626,7 @@ static void
 sbuf_putc_func(int c, void *arg)
 {
 
-	if (c != '\0')
+	if (__predict_true(c != '\0'))
 		sbuf_put_byte(arg, c);
 }