From 35b54fd064a353426229201d3c73f6b49c8a0445 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Sat, 27 Apr 2024 17:30:42 +0800 Subject: [PATCH] Separator: add fast path for ascii strings --- src/modules/separator/separator.c | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/src/modules/separator/separator.c b/src/modules/separator/separator.c index 538b651f4..7f8ac41d3 100644 --- a/src/modules/separator/separator.c +++ b/src/modules/separator/separator.c @@ -12,9 +12,20 @@ static inline uint32_t max(uint32_t a, uint32_t b) static inline uint32_t getWcsWidth(const FFstrbuf* mbstr, wchar_t* wstr, mbstate_t* state) { + int result = 1; + for (uint32_t i = 0; i < mbstr->length; i++) + { + if (!isascii(mbstr->chars[i])) + { + result = 0; + break; + } + } + if (__builtin_expect(result, 1)) return mbstr->length; + const char* str = mbstr->chars; uint32_t wstrLength = (uint32_t) mbsrtowcs(wstr, &str, mbstr->length, state); - int result = mk_wcswidth(wstr, wstrLength); + result = mk_wcswidth(wstr, wstrLength); return result > 0 ? (uint32_t) result : mbstr->length; } @@ -32,9 +43,9 @@ void ffPrintSeparator(FFSeparatorOptions* options) + (fqdn ? platform->hostName.length : ffStrbufFirstIndexC(&platform->hostName, '.')); // host name ffLogoPrintLine(); - if(options->string.length == 0) + if(__builtin_expect(options->string.length == 1, 1)) { - ffPrintCharTimes('-', titleLength); + ffPrintCharTimes(options->string.chars[0], titleLength); } else { @@ -131,7 +142,7 @@ void ffInitSeparatorOptions(FFSeparatorOptions* options) NULL, ffGenerateSeparatorJsonConfig ); - ffStrbufInit(&options->string); + ffStrbufInitStatic(&options->string, "-"); } void ffDestroySeparatorOptions(FFSeparatorOptions* options)