[libcxx-commits] [libcxx] [libc++] Avoid using ranges::upper_bound in <format> (PR #186781)

Nikolas Klauser via libcxx-commits libcxx-commits at lists.llvm.org
Mon Mar 16 04:39:35 PDT 2026


https://github.com/philnik777 created https://github.com/llvm/llvm-project/pull/186781

The `ranges` algorithms take a significant time to instantiate. Replacing `ranges::upper_bound` with `std::upper_bound` reduces the time to parse `<format>` by ~50ms, or 10%.


>From 1c209416c7d25ad2986f2396034d6f01d1597005 Mon Sep 17 00:00:00 2001
From: Nikolas Klauser <nikolasklauser at berlin.de>
Date: Mon, 16 Mar 2026 12:38:31 +0100
Subject: [PATCH] [libc++] Avoid using ranges::upper_bound in <format>

---
 libcxx/include/__format/escaped_output_table.h | 5 +++--
 libcxx/utils/generate_escaped_output_table.py  | 5 +++--
 2 files changed, 6 insertions(+), 4 deletions(-)

diff --git a/libcxx/include/__format/escaped_output_table.h b/libcxx/include/__format/escaped_output_table.h
index 1401b4637d839..a62c715811a0c 100644
--- a/libcxx/include/__format/escaped_output_table.h
+++ b/libcxx/include/__format/escaped_output_table.h
@@ -61,9 +61,10 @@
 #ifndef _LIBCPP___FORMAT_ESCAPED_OUTPUT_TABLE_H
 #define _LIBCPP___FORMAT_ESCAPED_OUTPUT_TABLE_H
 
-#include <__algorithm/ranges_upper_bound.h>
+#include <__algorithm/upper_bound.h>
 #include <__config>
 #include <__cstddef/ptrdiff_t.h>
+#include <__iterator/access.h>
 #include <cstdint>
 
 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
@@ -868,7 +869,7 @@ _LIBCPP_HIDE_FROM_ABI inline constexpr uint32_t __entries[735] = {
   if (__code_point >= 0x000323b0)
     return true;
 
-  ptrdiff_t __i = std::ranges::upper_bound(__entries, (__code_point << 14) | 0x3fffu) - __entries;
+  ptrdiff_t __i = std::upper_bound(std::begin(__entries), std::end(__entries), (__code_point << 14) | 0x3fffu) - __entries;
   if (__i == 0)
     return false;
 
diff --git a/libcxx/utils/generate_escaped_output_table.py b/libcxx/utils/generate_escaped_output_table.py
index 59dd707ae6126..402b8eefed622 100755
--- a/libcxx/utils/generate_escaped_output_table.py
+++ b/libcxx/utils/generate_escaped_output_table.py
@@ -142,7 +142,7 @@ def compactPropertyRanges(input: list[PropertyRange]) -> list[PropertyRange]:
   if (__code_point >= 0x{unallocated:08x})
     return true;
 
-  ptrdiff_t __i = std::ranges::upper_bound(__entries, (__code_point << 14) | 0x3fffu) - __entries;
+  ptrdiff_t __i = std::upper_bound(__entries, (__code_point << 14) | 0x3fffu) - __entries;
   if (__i == 0)
     return false;
 
@@ -216,9 +216,10 @@ def compactPropertyRanges(input: list[PropertyRange]) -> list[PropertyRange]:
 #ifndef _LIBCPP___FORMAT_ESCAPED_OUTPUT_TABLE_H
 #define _LIBCPP___FORMAT_ESCAPED_OUTPUT_TABLE_H
 
-#include <__algorithm/ranges_upper_bound.h>
+#include <__algorithm/upper_bound.h>
 #include <__config>
 #include <__cstddef/ptrdiff_t.h>
+#include <__iterator/access.h>
 #include <cstdint>
 
 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)



More information about the libcxx-commits mailing list