From a2de072a587d74fcc0e9af0d8df2ee5aab66988f Mon Sep 17 00:00:00 2001 From: rimathia Date: Thu, 5 Nov 2020 15:41:38 +0100 Subject: [PATCH 1/3] use memchr for searching for '%' in printf format string --- include/fmt/printf.h | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/include/fmt/printf.h b/include/fmt/printf.h index 8c28ac232727..ad35d5b82a89 100644 --- a/include/fmt/printf.h +++ b/include/fmt/printf.h @@ -474,8 +474,18 @@ OutputIt basic_printf_context::format() { const Char* end = parse_ctx_.end(); auto it = start; while (it != end) { + if (end - it < 32) { + // Use a simple linear search instead of memchr for small strings. + it = std::find(it, end, '%'); + } else { + if (!detail::find(it, end, '%', it)) { + it = end; + } + } + if (it == end) { + continue; + } char_type c = *it++; - if (c != '%') continue; if (it != end && *it == c) { out = std::copy(start, it, out); start = ++it; From e66299973b4f1d580ff1d94bdf920fbb60380ca5 Mon Sep 17 00:00:00 2001 From: rimathia Date: Sun, 8 Nov 2020 18:25:00 +0100 Subject: [PATCH 2/3] remove manual linear search clause --- include/fmt/printf.h | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/include/fmt/printf.h b/include/fmt/printf.h index ad35d5b82a89..ace4c6930975 100644 --- a/include/fmt/printf.h +++ b/include/fmt/printf.h @@ -474,13 +474,8 @@ OutputIt basic_printf_context::format() { const Char* end = parse_ctx_.end(); auto it = start; while (it != end) { - if (end - it < 32) { - // Use a simple linear search instead of memchr for small strings. - it = std::find(it, end, '%'); - } else { - if (!detail::find(it, end, '%', it)) { - it = end; - } + if (!detail::find(it, end, '%', it)) { + it = end; } if (it == end) { continue; From 0698d10953b4276e8e24fd02c5f20db9f037235c Mon Sep 17 00:00:00 2001 From: rimathia Date: Sun, 8 Nov 2020 19:12:43 +0100 Subject: [PATCH 3/3] replace unnecessary continue with break, get rid of now redundant it == end check --- include/fmt/printf.h | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/include/fmt/printf.h b/include/fmt/printf.h index ace4c6930975..b44afabac0ef 100644 --- a/include/fmt/printf.h +++ b/include/fmt/printf.h @@ -475,10 +475,8 @@ OutputIt basic_printf_context::format() { auto it = start; while (it != end) { if (!detail::find(it, end, '%', it)) { - it = end; - } - if (it == end) { - continue; + it = end; // detail::find leaves it == nullptr if it doesn't find '%' + break; } char_type c = *it++; if (it != end && *it == c) {