Skip to content

Split Components iterator to prefixed and non-prefixed versions and optimize Components for non-prefix path based platforms - #156496

Open
asder8215 wants to merge 19 commits into
rust-lang:mainfrom
asder8215:components_rewrite
Open

asder8215 wants to merge 19 commits into
rust-lang:mainfrom
asder8215:components_rewrite

Conversation

@asder8215

@asder8215 asder8215 commented May 12, 2026 •

Copy link
Copy Markdown
Contributor

View all comments

View all comments

This PR fixes #154521 specifically for Unix/non-prefixed platforms. By non-prefixed filepath platforms this includes:

  • all(target_vendor = "fortanix", target_env = "sgx")
  • not(any(target_os = "cygwin", target_os = "uefi", target_os = "solid_asp3", target_os = "windows") (the default/fallback _ path in sys/path/mod.rs, which notably includes Unix platforms)

For every other prefixed filepath platforms (notably Windows), the Components iterator implementation remains largely unchanged (just some minor refactoring).

The point of the non-prefixed Components implementation is to:

  1. Gravitate away from accounting for the Prefix components that Windows/other prefixed-based filepath platform use as Unix-based platforms do not have those prefix components in their filepaths;
  2. Optimize the whole Components iterator traversal algorithm as well as improve Path equality and Path comparison

From benchmarking, I noticed that all the Prefix checking/returning code specific for Windows affects how Components perform on Unix, though to be fair, part of the reason that is the case is because the Prefix enum takes up 40 bytes to represent (which we shouldn't want Unix to take the consequence of handling Prefix). There are other factors in the current Components::next/Components::next_back implementation that could've been optimized better or written in a way that would enable the compiler to optimize that code better (e.g. autovectorization optimizations). It's certainly possible that the prefixed/current Components implementation could be enhanced to make performance better on Windows, but I will leave that to someone who can verifiably test and benchmark those changes on Windows.

In terms of the optimizations I made for the non-prefixed Components, a smaller one I could talk about first is that the size of Components has been reduced. In the current/original Components we have this:

#[derive(Copy, Clone, PartialEq, PartialOrd, Debug)]
enum State {
    Prefix = 0,   // c:
    StartDir = 1, // / or . or nothing
    Body = 2,     // foo/bar/baz
    Done = 3,
}

#[derive(Clone)]
pub struct Components<'a> {
    // The path left to parse components from
    path: &'a [u8],

    // The prefix as it was originally parsed, if any
    prefix: Option<Prefix<'a>>,

    // true if path *physically* has a root separator; for most Windows
    // prefixes, it may have a "logical" root separator for the purposes of
    // normalization, e.g., \\server\share == \\server\share\.
    has_physical_root: bool,

    // The iterator is double-ended, and these two states keep track of what has
    // been produced from either end
    front: State,
    back: State,
}

This overall takes up 64 bytes to represents Components

Now, without the need to account for Prefix, we can simplify the Components struct to:

#[derive(Copy, Clone, PartialEq, PartialOrd, Debug)]
enum State {
    Absolute = 1, // A root component (i.e. '/')
    Relative = 2, // A relative component ('foo')
    Done = 3,     // Iterator is fully consumed
}

#[derive(Clone)]
pub struct Components<'a> {
    // The path left to parse components from
    path: &'a [u8],
    // The current state of the iterator
    state: State,
}

This prefix-less Components takes up 24 bytes to represent.

Implementation-wise, Components::next and Components::next_back takes a similar approach to the original Components iterator by subslicing the path field to present the left over unconsumed path. However, one of the key differences with this Components::next and Components::next_back is that we normalize away in between and trailing separator + cur dir bytes after parsing the component (e.g. "foo///bar", when we parse "foo" via Components::next or "bar" via Components::next_back", the in between separator bytes will be subsliced from the path field in preparation for the next Components::next/Components::next_back). Normalizing away these bytes in preparation for the next Components::next/Components::next_back were not done in the original implementation, which meant that while !self.is_finished loop in the original ran through multiple iterations to actually normalize away those separator bytes and find the next component to parse. Moreover, I relied on iterator methods to make that normalization process really fast, as it appears to be autovectorized and optimized really well by the compiler. Also noteworthy is that the optimization made to normalizing away in between/trailing separator/cur dir bytes using iterator methods enhances the performance of Components::as_path significantly.

As for Path equality and Path comparison, which largely rely on Components underneath the hood, there are many optimizations we introduce there to increase performance.

Starting with Path equality, we check for the following:

  • Introduced by @Lfan-ke, we check directly if the two Paths when converted as an OsStrs are equal. If so, we can just return true immediately. Otherwise, we have to hinge on Components (e.g. of "/foo" is equivalent to "/foo/").
  • We have a fast path in checking if either the left path or right path an empty path? We can easily use Components state field to determine if we're looking at an empty path (reading 1 byte via the State::Done enum rather than directly looking at each path's length)
  • We use iterator methods to check for the first mismatching byte between the left and right path. So long as the mismatching byte is not a separator or cur dir byte on either end, we can directly return false.
  • Otherwise, we find the nearest separator byte from the right (also using iterator methods) for both to readjust their Components path field and just rely on Components::next_back to handle the rest on determining whether the two paths are equivalent or not.

As for Path comparison, we do the following:

  • Check if the two paths are equal. If so, we can return Ordering::Equal immediately.
  • We then use iterator methods to check for the first mismatching byte between the left and right path. So long as the mismatching byte is not a separator or cur dir byte on either end, we can directly compare the byte and return Ordering::Greater or Ordering::Less as appropriate).
  • Otherwise, we find the nearest separator byte from the left (also using iterator methods) for both to readjust their Components path field and just rely on Components::next to handle the rest on determining whether to return Ordering::Equal, Ordering::Less, or Ordering::Greater.
    • Note: this was already done from the original Components implementation, so this code/idea was just taken from there.

All of these enhances the performance of Path equality and comparison significantly, which you can see from benchmarking results.

Regarding benchmarking, I did all benchmarking and experimentation on this Components implementation within a separate repository. You can take a look here to see the raw and markdown formatted results and the methodology I chose to go about benchmarking this Components iterator implementation.

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels May 12, 2026
@rustbot

rustbot commented May 12, 2026

Copy link
Copy Markdown
Collaborator

r? @Mark-Simulacrum

rustbot has assigned @Mark-Simulacrum.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: @ChrisDenton, libs
  • @ChrisDenton, libs expanded to 8 candidates

@rustbot

This comment has been minimized.

@asder8215
asder8215 force-pushed the components_rewrite branch from 1627e2f to 33e69e1 Compare May 12, 2026 09:09
@rustbot

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@asder8215
asder8215 force-pushed the components_rewrite branch from 33e69e1 to ed9d33d Compare May 12, 2026 17:05
@rust-log-analyzer

This comment has been minimized.

@asder8215
asder8215 force-pushed the components_rewrite branch from ed9d33d to 0b0f84c Compare May 12, 2026 17:19
@rust-log-analyzer

This comment has been minimized.

@asder8215
asder8215 force-pushed the components_rewrite branch from 0b0f84c to 8ed33ea Compare May 12, 2026 22:05
@asder8215

This comment was marked as outdated.

@asder8215
asder8215 force-pushed the components_rewrite branch from 2151b8f to 83cdbed Compare May 13, 2026 22:21
@asder8215

This comment was marked as outdated.

@asder8215
asder8215 force-pushed the components_rewrite branch from 83cdbed to 3921fff Compare May 15, 2026 00:30
@asder8215
asder8215 marked this pull request as draft May 16, 2026 12:22
@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels May 16, 2026
@asder8215
asder8215 force-pushed the components_rewrite branch from 0a25dda to 92e0132 Compare May 17, 2026 16:09
@asder8215

asder8215 commented May 17, 2026 •

Copy link
Copy Markdown
Contributor Author

New benchmarking results. You can see what the benchmark code looks like here and run it yourself to see if there are any difference in measurements on your end:

This is the measurement of the current implementation of Components<'_> (without black box):

Std Components (No BB)  time:   [21.546 µs 21.800 µs 22.096 µs]
Found 5 outliers among 100 measurements (5.00%)
  4 (4.00%) high mild
  1 (1.00%) high severe

Std Components Next (No BB)
                        time:   [20.434 µs 20.482 µs 20.538 µs]
Found 7 outliers among 100 measurements (7.00%)
  5 (5.00%) high mild
  2 (2.00%) high severe

Std Components Next Back (No BB)
                        time:   [38.367 µs 38.757 µs 39.199 µs]
Found 4 outliers among 100 measurements (4.00%)
  3 (3.00%) high mild
  1 (1.00%) high severe

Std Path Iter (No BB)   time:   [21.547 µs 21.730 µs 21.921 µs]

Std As Path Iter (No BB)
                        time:   [87.680 µs 88.439 µs 89.231 µs]
Found 6 outliers among 100 measurements (6.00%)
  2 (2.00%) high mild
  4 (4.00%) high severe

Std Eq Comps (No BB)    time:   [591.21 ns 593.35 ns 595.82 ns]
Found 16 outliers among 100 measurements (16.00%)
  1 (1.00%) low severe
  3 (3.00%) low mild
  7 (7.00%) high mild
  5 (5.00%) high severe

Std Uneq Comps (No BB)  time:   [60.953 ns 61.419 ns 61.911 ns]
Found 2 outliers among 100 measurements (2.00%)
  2 (2.00%) high mild

Std Uneq 2 Comps (No BB)
                        time:   [75.454 µs 75.734 µs 76.027 µs]
Found 1 outliers among 100 measurements (1.00%)
  1 (1.00%) high mild

Std Compare Comps (No BB)
                        time:   [46.182 µs 46.621 µs 47.192 µs]
Found 4 outliers among 100 measurements (4.00%)
  3 (3.00%) high mild
  1 (1.00%) high severe

Std Compare Uneq Comps (No BB)
                        time:   [46.679 µs 46.980 µs 47.291 µs]
Found 2 outliers among 100 measurements (2.00%)
  1 (1.00%) high mild
  1 (1.00%) high severe

Std Compare Uneq 2 Comps (No BB)
                        time:   [41.480 ns 41.827 ns 42.160 ns]
Found 3 outliers among 100 measurements (3.00%)
  2 (2.00%) high mild
  1 (1.00%) high severe

This is the measurement of the new implementation of Components<'_> I'm working on (without black box):

Components Rewrite (No BB)
                        time:   [24.982 µs 25.267 µs 25.570 µs]

Components Next Rewrite (No BB)
                        time:   [24.388 µs 24.655 µs 24.937 µs]
Found 6 outliers among 100 measurements (6.00%)
  6 (6.00%) high mild

Components Next Back Rewrite (No BB)
                        time:   [18.184 µs 18.567 µs 19.034 µs]
Found 16 outliers among 100 measurements (16.00%)
  1 (1.00%) high mild
  15 (15.00%) high severe

Path Iter Rewrite (No BB)
                        time:   [23.485 µs 23.659 µs 23.829 µs]
Found 2 outliers among 100 measurements (2.00%)
  2 (2.00%) high mild

As Path Iter Rewrite (No BB)
                        time:   [22.936 µs 23.066 µs 23.208 µs]
Found 7 outliers among 100 measurements (7.00%)
  3 (3.00%) high mild
  4 (4.00%) high severe

Eq Comps Rewrite (No BB)
                        time:   [605.12 ns 608.83 ns 612.98 ns]
Found 11 outliers among 100 measurements (11.00%)
  1 (1.00%) low mild
  7 (7.00%) high mild
  3 (3.00%) high severe

Uneq Comps Rewrite (No BB)
                        time:   [31.799 ns 32.108 ns 32.433 ns]
Found 5 outliers among 100 measurements (5.00%)
  5 (5.00%) high mild

Uneq Comps 2 Rewrite (No BB)
                        time:   [47.091 µs 48.186 µs 49.085 µs]

Compare Comps Rewrite (No BB)
                        time:   [50.234 µs 50.725 µs 51.254 µs]
Found 10 outliers among 100 measurements (10.00%)
  9 (9.00%) high mild
  1 (1.00%) high severe

Compare Uneq Comps Rewrite (No BB)
                        time:   [49.262 µs 49.631 µs 50.067 µs]
Found 16 outliers among 100 measurements (16.00%)
  4 (4.00%) high mild
  12 (12.00%) high severe

Compare Uneq Comps 2 Rewrite (No BB)
                        time:   [43.397 ns 43.767 ns 44.171 ns]
Found 5 outliers among 100 measurements (5.00%)
  5 (5.00%) high mild

Edit: Updated Components::as_path to match on Option<FirstComponent>/self.first_comp instead of using if let Some(_) = self.first_comp and matching on that, benchmarking for this PR Components<'_> has been updated as a result. Everything else is unaffected by this change.

@asder8215

asder8215 commented May 17, 2026 •

Copy link
Copy Markdown
Contributor Author

Here are the benchmark results with black box:

From current Components<'_> implementation:

Std Components          time:   [20.947 µs 21.010 µs 21.084 µs]
Found 8 outliers among 100 measurements (8.00%)
  4 (4.00%) high mild
  4 (4.00%) high severe

Std Components Next     time:   [20.967 µs 20.993 µs 21.021 µs]
Found 2 outliers among 100 measurements (2.00%)
  2 (2.00%) high mild

Std Components Next Back
                        time:   [35.715 µs 35.802 µs 35.925 µs]
Found 20 outliers among 100 measurements (20.00%)
  6 (6.00%) high mild
  14 (14.00%) high severe

Std Path Iter           time:   [20.883 µs 20.992 µs 21.152 µs]
Found 12 outliers among 100 measurements (12.00%)
  5 (5.00%) high mild
  7 (7.00%) high severe

Std As Path Iter        time:   [80.673 µs 80.935 µs 81.261 µs]
Found 9 outliers among 100 measurements (9.00%)
  6 (6.00%) high mild
  3 (3.00%) high severe

Std Eq Comps            time:   [589.43 ns 593.36 ns 597.88 ns]
Found 4 outliers among 100 measurements (4.00%)
  1 (1.00%) low severe
  2 (2.00%) low mild
  1 (1.00%) high severe

Std Uneq Comps          time:   [63.919 ns 64.262 ns 64.765 ns]
Found 10 outliers among 100 measurements (10.00%)
  6 (6.00%) high mild
  4 (4.00%) high severe

Std Uneq 2 Comps        time:   [75.284 µs 75.939 µs 76.599 µs]
Found 3 outliers among 100 measurements (3.00%)
  3 (3.00%) high severe

From this Components<'_> implementation PR:

Components Rewrite      time:   [24.190 µs 24.425 µs 24.687 µs]
Found 5 outliers among 100 measurements (5.00%)
  4 (4.00%) high mild
  1 (1.00%) high severe

Components Next Rewrite time:   [24.230 µs 24.550 µs 24.889 µs]
Found 1 outliers among 100 measurements (1.00%)
  1 (1.00%) high mild

Components Next Back Rewrite
                        time:   [17.339 µs 17.488 µs 17.655 µs]
Found 5 outliers among 100 measurements (5.00%)
  3 (3.00%) high mild
  2 (2.00%) high severe

Path Iter Rewrite       time:   [23.845 µs 23.996 µs 24.154 µs]
Found 1 outliers among 100 measurements (1.00%)
  1 (1.00%) high mild

As Path Iter Rewrite    time:   [22.431 µs 22.676 µs 23.010 µs]
Found 4 outliers among 100 measurements (4.00%)
  2 (2.00%) high mild
  2 (2.00%) high severe

Eq Comps Rewrite        time:   [586.16 ns 588.10 ns 590.14 ns]

Found 5 outliers among 100 measurements (5.00%)
  2 (2.00%) low mild
  2 (2.00%) high mild
  1 (1.00%) high severe

Uneq Comps Rewrite      time:   [31.733 ns 32.023 ns 32.378 ns]

Found 7 outliers among 100 measurements (7.00%)
  1 (1.00%) high mild
  6 (6.00%) high severe

Uneq 2 Comps Rewrite    time:   [36.318 µs 36.574 µs 36.913 µs]
Found 23 outliers among 100 measurements (23.00%)
  23 (23.00%) high severe

Edit: Updated Components::as_path to match on Option<FirstComponent>/self.first_comp instead of using if let Some(_) = self.first_comp and matching on that, benchmarking for this PR Components<'_> has been updated as a result. Everything else is unaffected by this change.

Edit 2: Took off Path ordering benchmark here since it was incorrect see below to see corrected path ordering benchmarks.

@asder8215
asder8215 force-pushed the components_rewrite branch from 92e0132 to 574d7f2 Compare May 17, 2026 18:41
@asder8215
asder8215 marked this pull request as ready for review May 17, 2026 18:59
@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels May 17, 2026
@asder8215

asder8215 commented May 17, 2026 •

Copy link
Copy Markdown
Contributor Author

I'm confident this code works (passed CI in previous run, the current amended commit change I made doesn't change logic, but makes the code written in a more idiomatic way).

In my opinion, the logic in this code should look more readable than how Component<'_> is currently written implemented as. From benchmarking, we can see that Components::next_back, Components::as_path, and in the cases where Components equality falls down to using Components::next_back (when they are unequal or equality can't be determine unless one/both Components<'_> are normalized), this PR implementation of Components<'_> is faster than how it's currently implemented as. The trade off is that this PR implementation of Components<'_> has a slight reduction in performance for Components::next and as a result Components<'_> comparison, but I would take this slight reduction in performance to make path equality faster.

@asder8215

asder8215 commented May 19, 2026 •

Copy link
Copy Markdown
Contributor Author

@rustbot label +I-libs-nominated

Since Components<'_> is pretty well-used in many other methods, I think this may need discussion from the libs team on whether the re-implementation of Components<'_> here is okay/valid to take over the current implementation (and the trade-off between faster Components::next_back with a slight reduction in performance in Components::next). I wasn't sure if this should be labeled as I-libs-api-nominated or not since it pertains to an existing stable feature than a new feature.

@rustbot rustbot added the I-libs-nominated Nominated for discussion during a libs team meeting. label May 19, 2026
@rust-bors

This comment has been minimized.

asder8215 and others added 18 commits November 23, 2026 19:59
… of mutating and subslicing path field; as a result, Components iterator memory size goes from 64 bytes to 40 bytes and as_path does not use cloning at all
…ity, added safety comments, and check for root dir after Prefix component (e.g., '\\?\checkout\src\tools' should produce Prefix, RootDir, Normal, Normal, None, ...) in Components::parse_single_component
…here to use iter().position()/.iter().rposition(), refactored code in compare_components, and removed stale comments
…omponents::normalize_back instead, refactored Components::as_path code
…e in previous implementation, but making it work with Components<'_> front index
…nt_front and consume_first_component_back. Also introduced aggressive inlining.
…comparison (normalizing paths if needed) and return Ordering Equal/Greater/Less if possible before needing to fall back on Iterator::cmp
… make it simpler or have it documented in the appropriate areas, and renamed a couple of methods/field members, and simplified Components::next_back relative first component branch
…yte equality and Iterator::eq directly.

Co-authored-by: =?UTF-8?q?=E7=A6=BE=E5=8F=AF?= <chengkelfan@qq.com>
…path check to Components/Path equality by finding first mismatched byte, separated Components iterator into different versions and re-exported them accordingly based on target os. This also ends up removing HAS_PREFIXES because it becomes unused.
…rtialOrd implementation for non-prefixed Components via directly comparing mismatched byte if it's not a separator byte or cur dir byte; refactor while loop code to use iter().enumerate().find() instead
…e conditional style code rather than match cases (degrades Components::next_back for some reason)
…zes away trailing separator bytes and non-starting current directory component a well as added Components::is_sep_byte for nonprefixed_path.rs
…onents (forgot ! in front of is_sep_byte), added clarifying comments on Components::next and Components::next_back, removed redundant code in Components::next and PathBuf::push as well as reserve space for the separator byte in PathBuf::push, and renamed a couple functions in Windows Components implementation
@rustbot

rustbot commented Nov 24, 2026

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

…mplementation to its internal eq_components implementation
@MikkelPaulson

Copy link
Copy Markdown
Contributor

As part of the path refactor goal, I did a review/nitpick the entire existing API, including tearing the Components iterator to shreds. One thing that came of if is that at least internally and potentially externally there's value to a Components iterator where path.components().collect() is equal to path in all cases. (Actually equal, not just because we use the iterator to determine equality.) This could be done with a RawComponents iterator and a RawComponent enum that adds a Empty type. RawComponents then differs from Components in the following ways:

  • . (CurDir) is represented in all positions, not just as a prefix
  • // is represented as Empty rather than being silently ignored
  • Trailing / is also Empty

I mention that in this context because it permits Components to become a skinny wrapper that filters out Empty and non-leading CurDir, which would tremendously simplify the logic. Meanwhile, RawComponents can them be used eg. for normalize_lexically to determine if there's anything to normalize, or for the various trailing slash operations.

@asder8215

asder8215 commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor Author

@MikkelPaulson

I'm completely down for the RawComponents iterator idea, and I see your argument with this allowing path.components().collect() to be equal to path in all cases (I've also seen the issue raised in #164467 about this with the inconsistency between collected path components when it's collected as a Vec<_> versus PathBuf on Windows).

That said, I would likely work on the RawComponents iterator as a separate PR after this PR. This PR, at least when I started it, was meant to be a refactor split between Windows/non-Windows implementation of Components for the purposes of optimizing Unix's Components iterator. The solution I proposed in #148267 was kind of a quick way to solve that issue (albeit you could call it more of a hack instead of a neater solution like RawComponents). I would feel comfortable having this merged in first before implementing a lower level abstraction (implementation?) to Components just so that we have a base point we can go back to if implementing RawComponents causes any performance issues.

On the bright side, I think the current API for Unix's Components (with some slight changes) is almost enough to be converted as RawComponents. With a RawComponents iterator, it would have 4 states (when we're thinking about Unix specifically, Windows might need 5 for Prefix):

enum State {
    Root = 1,                // A root component (i.e. '/')
    Intermediate = 2 // In between non-root separators
    Body = 3,              // A non-root component ('foo', '.', '..', etc.)
    Done = 4,             // Iterator is fully consumed
}

If our iterator is at a Root state, we'd just slice away the first separator byte, transition the RawComponents iterator to an Intermediate state (or Done if iterator is fully consumed), and return a RootDir component. If our iterator is at a Body state, we slice up to but not including the first separator, transition the RawComponents to an Intermediate state or Done, and then return that body component (ParentDir, CurDir, Normal, etc.). The Intermediate state indicates that we're looking at separators between two components, and we could just keep parsing until we see a non-separator byte, transitioning the Components iterator to either Body or Done, and return a Slashes (or whatever we bikeshed the name to) component that contains a &'a OsStr just like Normal component.

Basically this:

Root -> Intermediate Done
Body -> Intermediate Done
Intermediate -> Body Done
Done -> we're done

Most importantly, RawComponents does not call normalize_front and normalize_back methods and instead we leave those calls to Components iterator to use (normalize_front after using RawComponents::next and normalize_back before RawComponents::next_back), which would effectively remove it covering the Intermediate state and non-leading CurDir components.

As far as Windows would go about it this, I would need a bit more time to think about this due to the Prefix component (the whole Windows Components` iterator implementation needs a redesign/refactor tbh).

@MikkelPaulson

Copy link
Copy Markdown
Contributor

Fair enough re: reorganize then tinker.

But the point is to remove complexity, not add complexity. You don't need a state machine (after the prefix), just to split on separators. ./abc///./../ would be represented by RawComponents as [CurDir, Normal("abc"), Empty, Empty, CurDir, Parent, Empty]. Dropping the Emptys and non-initial CurDir, the Components wrapper would then return [CurDir, Normal("abc"), Parent], which is the same as the current implementation.

@asder8215

asder8215 commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor Author

But the point is to remove complexity, not add complexity. You don't need a state machine (after the prefix), just to split on separators. ./abc///./../ would be represented by RawComponents as [CurDir, Normal("abc"), Empty, Empty, CurDir, Parent, Empty].

To note, the point I was making for having the higher level Components iterator invoke the normalization methods in Components::next/Components::next_back was for performance reasons in Unix. One of the reasons why the current Components iterator is slow is because its Components::next/Components::next_back doesn't trim away separators after/before parsing a component (respectively for Components::next/Components::next_back), e.g.

Path::new("foo////bar")

Turning this path into a Components iterator and calling Components::next on this will give us "foo", but internally the Components iterator is left with "////bar". Now in the next Components::next call, it loops multiple times one by one on the "/" (delimiting condition is separator byte) until it can finally parse "bar".

Now think about a path like this:

Path::new("first//////./////.//////./////./////last"")

With RawComponents, IIUC from what you had in mind, this would produce:

[Normal("first"), Empty, CurDir, Empty, CurDir, Empty, CurDir, Empty, CurDir, Empty, Normal("last")]

To call RawComponents::next 11 times, 9 of which has to be skipped, within Components::next is a performance cost, when we can just invoke normalize_front after a RawComponents::next (which internally normalizes away slashes + non-leading cur dir bytes) in Components::next to effectively skip the Empty, non-leading CurDir, ending up with just 2 RawComponents::next call from Components::next.

The complexity for this is actually not that high. It would just be something like this:

impl<'a> Iterator for Components<'a> {
    type Item = Component<'a>;

    fn next(&mut self) -> Option<Component<'a>> {
        if !self.raw_comp.is_done() {
            let comp = self.raw_comp.next(); // Will always be RootDir or Body
            self.raw_comp.normalize_front(); // Skip Empty, non-leading CurDir
            return comp;
        }
        None
    }
}


impl<'a> DoubleEndedIterator for Components<'a> {
    type Item = Component<'a>;

    fn next_back(&mut self) -> Option<Component<'a>> {
        if !self.raw_comp.is_done() {
            self.raw_comp.normalize_back(); // Skip Empty, non-leading CurDir
            let comp = self.raw_comp.next_back(); // Will always be RootDir or Body
            return comp;
        }
        None
    }
}

Components::next_back might need a bit more modification to return RootDir appropriately.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-path Area: `std::path` (note: `std::fs` is A-filesystem) S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Path comparison is obscenely slow

10 participants