[libcxx-commits] [libcxx] [libc++] Optimize fs::path::iterator::operator++ (PR #227645)

Nikolas Klauser via libcxx-commits libcxx-commits at lists.llvm.org
Wed Sep 30 04:36:21 PDT 2026


https://github.com/philnik777 updated https://github.com/llvm/llvm-project/pull/227645

>From 5d63b5d58c8ba6e554215c6de723412fee1b404d Mon Sep 17 00:00:00 2001
From: Nikolas Klauser <nikolasklauser at berlin.de>
Date: Wed, 30 Sep 2026 12:25:52 +0200
Subject: [PATCH] [libc++] Optimize fs::path::iterator::operator++

---
 libcxx/include/__filesystem/path.h  |  2 +-
 libcxx/src/filesystem/path_parser.h | 17 +++++++++++------
 2 files changed, 12 insertions(+), 7 deletions(-)

diff --git a/libcxx/include/__filesystem/path.h b/libcxx/include/__filesystem/path.h
index 14ba8c8e0fd29..da4c6d0e46ade 100644
--- a/libcxx/include/__filesystem/path.h
+++ b/libcxx/include/__filesystem/path.h
@@ -891,7 +891,7 @@ class _LIBCPP_EXPORTED_FROM_ABI path {
 
 private:
   inline _LIBCPP_HIDE_FROM_ABI path& __assign_view(__string_view const& __s) {
-    __pn_ = string_type(__s);
+    __pn_ = __s;
     return *this;
   }
   string_type __pn_;
diff --git a/libcxx/src/filesystem/path_parser.h b/libcxx/src/filesystem/path_parser.h
index ae91dc7f1cf26..4b6500021807d 100644
--- a/libcxx/src/filesystem/path_parser.h
+++ b/libcxx/src/filesystem/path_parser.h
@@ -9,6 +9,7 @@
 #ifndef PATH_PARSER_H
 #define PATH_PARSER_H
 
+#include <__algorithm/ranges_contains.h>
 #include <__config>
 #include <__utility/unreachable.h>
 #include <cstddef>
@@ -19,14 +20,14 @@
 
 _LIBCPP_BEGIN_NAMESPACE_FILESYSTEM
 
-inline bool isSeparator(path::value_type C) {
-  if (C == '/')
-    return true;
 #ifdef _WIN32
-  if (C == '\\')
-    return true;
+constexpr path::value_type separators[] = {'\\', '/'};
+#else
+constexpr path::value_type separators[] = {'/'};
 #endif
-  return false;
+
+inline bool isSeparator(path::value_type C) {
+  return std::ranges::contains(separators, C);
 }
 
 inline bool isDriveLetter(path::value_type C) { return (C >= 'a' && C <= 'z') || (C >= 'A' && C <= 'Z'); }
@@ -287,6 +288,10 @@ struct PathParser {
       return nullptr;
     const int Inc = P < End ? 1 : -1;
     P += Inc;
+    if constexpr (std::size(separators) == 1) {
+      if (Inc > 0)
+        return std::find(P, End, separators[0]);
+    }
     while (P != End && !isSeparator(*P))
       P += Inc;
     if (P == End && Inc < 0) {



More information about the libcxx-commits mailing list