Skip to content

Change implementation of remove_dir_all from recursive to an iterative version#154829

Open
asder8215 wants to merge 1 commit intorust-lang:mainfrom
asder8215:remove_dir_all_iterative
Open

Change implementation of remove_dir_all from recursive to an iterative version#154829
asder8215 wants to merge 1 commit intorust-lang:mainfrom
asder8215:remove_dir_all_iterative

Conversation

@asder8215
Copy link
Copy Markdown
Contributor

@asder8215 asder8215 commented Apr 4, 2026

This PR essentially changes the underlying remove_dir_all implementation used by unix and I believe uefi platforms from a recursive-based approach to an iterative version. The order it removes directories and files from the recursive version should be preserved in this iterative version. I'm curious if this iterative approach could show any performance improvement over the current recursive based approach that remove_dir_all has on unix/uefi platforms.

I also made a comment on how this iterative-based approach is redundantly holding a PathBuf in the tuple (which could incur a big allocation cost if we are removing deeply nested directories). This could be optimized if we are able to retrieve a reference to the path that ReadDir struct is holding, as it contains this information in its InnerReadDir's root field; I believe this would also make the iterative approach allocate less memory than the recursive approach as we would only need the main directory/nested directories &Path values when we are removing it.

I can make an ACP for ReadDir struct root path to be accessible via a method if that would be appropriate.

@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 Apr 4, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Apr 4, 2026

r? @jhpratt

rustbot has assigned @jhpratt.
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
  • Random selection from Mark-Simulacrum, jhpratt

@asder8215 asder8215 changed the title Changed implementation of remove_dir_all from recursive to an iterative version Change implementation of remove_dir_all from recursive to an iterative version Apr 4, 2026
// in the ReadDir struct through a method that return &Path, we can reduce memory usage
// allocated to this VecDeque.

let mut directories = VecDeque::new();
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, noting that this could use the std library LinkedList implementation if the resizing cost from VecDeque is undesirable.

@jhpratt
Copy link
Copy Markdown
Member

jhpratt commented Apr 7, 2026

@rustbot reroll

@rustbot rustbot assigned Mark-Simulacrum and unassigned jhpratt Apr 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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.

4 participants