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

via libcxx-commits libcxx-commits at lists.llvm.org
Wed Sep 30 03:27:12 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-libcxx

Author: Nikolas Klauser (philnik777)

<details>
<summary>Changes</summary>



---
Full diff: https://github.com/llvm/llvm-project/pull/227645.diff


2 Files Affected:

- (modified) libcxx/src/filesystem/path.cpp (+1-1) 
- (modified) libcxx/src/filesystem/path_parser.h (+10-11) 


``````````diff
diff --git a/libcxx/src/filesystem/path.cpp b/libcxx/src/filesystem/path.cpp
index 5790039f66426..3b0e62cfc6fe9 100644
--- a/libcxx/src/filesystem/path.cpp
+++ b/libcxx/src/filesystem/path.cpp
@@ -67,7 +67,7 @@ string_view_t path::__root_path_raw() const {
   auto PP = PathParser::CreateBegin(__pn_);
   if (PP.State_ == PathParser::PS_InRootName) {
     auto NextCh = PP.peek();
-    if (NextCh && isSeparator(*NextCh)) {
+    if (NextCh && *NextCh == separator) {
       ++PP;
       return createView(__pn_.data(), &PP.RawEntry.back());
     }
diff --git a/libcxx/src/filesystem/path_parser.h b/libcxx/src/filesystem/path_parser.h
index ae91dc7f1cf26..0ae34c32764bb 100644
--- a/libcxx/src/filesystem/path_parser.h
+++ b/libcxx/src/filesystem/path_parser.h
@@ -9,6 +9,8 @@
 #ifndef PATH_PARSER_H
 #define PATH_PARSER_H
 
+#include <__algorithm/ranges_find.h>
+#include <__algorithm/ranges_find_last.h>
 #include <__config>
 #include <__utility/unreachable.h>
 #include <cstddef>
@@ -19,15 +21,11 @@
 
 _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 separator = '\\';
+#else
+constexpr path::value_type separator = '/';
 #endif
-  return false;
-}
 
 inline bool isDriveLetter(path::value_type C) { return (C >= 'a' && C <= 'z') || (C >= 'A' && C <= 'Z'); }
 
@@ -257,11 +255,11 @@ struct PathParser {
 
   // Consume all consecutive separators.
   PosPtr consumeAllSeparators(PosPtr P, PosPtr End) const noexcept {
-    if (P == nullptr || P == End || !isSeparator(*P))
+    if (P == nullptr || P == End || *P != separator)
       return nullptr;
     const int Inc = P < End ? 1 : -1;
     P += Inc;
-    while (P != End && isSeparator(*P))
+    while (P != End && *P == separator)
       P += Inc;
     return P;
   }
@@ -283,11 +281,12 @@ struct PathParser {
 
   PosPtr consumeName(PosPtr P, PosPtr End) const noexcept {
     PosPtr Start = P;
-    if (P == nullptr || P == End || isSeparator(*P))
+    if (P == nullptr || P == End || *P == separator)
       return nullptr;
     const int Inc = P < End ? 1 : -1;
     P += Inc;
-    while (P != End && !isSeparator(*P))
+    P = Inc < 0 ? std::ranges::find_last(End, P + 1, separator).begin() : std::ranges::find(P, End, separator);
+    while (P != End && *P != separator)
       P += Inc;
     if (P == End && Inc < 0) {
       // Iterating backwards and consumed all the rest of the input.

``````````

</details>


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


More information about the libcxx-commits mailing list