From 0b11d0a70f583e6e82febd3e27e308506b4c1dce Mon Sep 17 00:00:00 2001 From: Daniel Lemire Date: Tue, 23 Dec 2025 20:18:26 -0500 Subject: [PATCH 1/3] even simpler bench_ip function --- benchmarks/bench_ip.cpp | 82 +++++++++++++++-------------------------- 1 file changed, 29 insertions(+), 53 deletions(-) diff --git a/benchmarks/bench_ip.cpp b/benchmarks/bench_ip.cpp index 761ebc11..900c6c87 100644 --- a/benchmarks/bench_ip.cpp +++ b/benchmarks/bench_ip.cpp @@ -52,61 +52,37 @@ fastfloat_really_inline const char *seek_ip_end(const char *p, return current; } -fastfloat_really_inline int parse_u8_fastfloat(const char *&p, const char *pend, - uint8_t *out) { - if (p == pend) - return 0; - auto r = fast_float::from_chars(p, pend, *out); - if (r.ec == std::errc()) { - p = r.ptr; - return 1; - } - return 0; -} +enum class parse_method { standard, fast_float }; -fastfloat_really_inline int parse_u8_fromchars(const char *&p, const char *pend, - uint8_t *out) { - if (p == pend) { - return 0; - } - auto r = std::from_chars(p, pend, *out); - if (r.ec == std::errc()) { - p = r.ptr; - return 1; - } - return 0; -} -template +template fastfloat_really_inline std::pair -simple_parse_ip_line(const char *p, const char *pend, Parser parse_uint8) { - uint8_t v1; - if (!parse_uint8(p, pend, &v1)) { - return {false, 0}; - } - if (p == pend || *p++ != '.') { - return {false, 0}; - } - uint8_t v2; - if (!parse_uint8(p, pend, &v2)) { - return {false, 0}; - } - if (p == pend || *p++ != '.') { - return {false, 0}; - } - uint8_t v3; - if (!parse_uint8(p, pend, &v3)) { - return {false, 0}; - } - if (p == pend || *p++ != '.') { - return {false, 0}; - } - uint8_t v4; - if (!parse_uint8(p, pend, &v4)) { - return {false, 0}; +simple_parse_ip_line(const char *p, const char *pend) { + const char *current = p; + uint32_t ip = 0; + for (int i = 0; i < 4; ++i) { + uint8_t value; + if constexpr (use_standard == parse_method::standard) { + auto r = std::from_chars(current, pend, value); + if (r.ec != std::errc()) { + return {false, 0}; + } + current = r.ptr; + } else if constexpr (use_standard == parse_method::fast_float) { + auto r = fast_float::from_chars(current, pend, value); + if (r.ec != std::errc()) { + return {false, 0}; + } + current = r.ptr; + } + ip = (ip << 8) | value; + if (i < 3) { + if (current == pend || *current++ != '.') { + return {false, 0}; + } + } } - return {true, (uint32_t(v1) << 24) | (uint32_t(v2) << 16) | - (uint32_t(v3) << 8) | uint32_t(v4)}; + return {true, ip}; } static std::string make_ip_line(uint8_t a, uint8_t b, uint8_t c, uint8_t d) { @@ -176,7 +152,7 @@ int main() { int ok = 0; for (size_t i = 0; i < N; ++i) { auto [ok, ip] = - simple_parse_ip_line(p, pend, parse_u8_fromchars); + simple_parse_ip_line(p, pend); sum += ip; if (!ok) { std::abort(); @@ -193,7 +169,7 @@ int main() { int ok = 0; for (size_t i = 0; i < N; ++i) { auto [ok, ip] = - simple_parse_ip_line(p, pend, parse_u8_fastfloat); + simple_parse_ip_line(p, pend); sum += ip; if (!ok) { std::abort(); From f9ddc75c69d3d935ec700ffa9116d11215df2cf7 Mon Sep 17 00:00:00 2001 From: Daniel Lemire Date: Tue, 23 Dec 2025 20:20:00 -0500 Subject: [PATCH 2/3] removing space --- benchmarks/bench_ip.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/benchmarks/bench_ip.cpp b/benchmarks/bench_ip.cpp index 900c6c87..825a6b0a 100644 --- a/benchmarks/bench_ip.cpp +++ b/benchmarks/bench_ip.cpp @@ -54,7 +54,6 @@ fastfloat_really_inline const char *seek_ip_end(const char *p, enum class parse_method { standard, fast_float }; - template fastfloat_really_inline std::pair simple_parse_ip_line(const char *p, const char *pend) { From a6a229a325452ae927e9b2e16bf01b3d69e03703 Mon Sep 17 00:00:00 2001 From: Daniel Lemire Date: Wed, 24 Dec 2025 13:59:12 -0500 Subject: [PATCH 3/3] bumping tag --- benchmarks/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/benchmarks/CMakeLists.txt b/benchmarks/CMakeLists.txt index 791cf612..4ee57895 100644 --- a/benchmarks/CMakeLists.txt +++ b/benchmarks/CMakeLists.txt @@ -3,7 +3,7 @@ include(FetchContent) FetchContent_Declare( counters GIT_REPOSITORY https://github.com/lemire/counters.git - GIT_TAG v2.1.0 + GIT_TAG v2.2.0 ) FetchContent_MakeAvailable(counters)