Skip to content

Conversation

@asder8215
Copy link

@asder8215 asder8215 commented Oct 28, 2025

The current implementation of create_dir_all(...) in std::fs operates recursively. As mentioned in #124309, this could run into a stack overflow with big paths. To avoid this stack overflow issue, this PR implements the method in an iterative manner, preserving the documented behavior of:

Recursively create a directory and all of its parent components if they are missing. This function is not atomic. If it returns an error, any parent components it was able to create will remain. If the empty path is passed to this function, it always succeeds without creating any directories. 
@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 Oct 28, 2025
@rustbot
Copy link
Collaborator

rustbot commented Oct 28, 2025

r? @tgross35

rustbot has assigned @tgross35.
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

@rustbot
Copy link
Collaborator

rustbot commented Oct 28, 2025

⚠️ Warning ⚠️

  • There are issue links (such as #123) in the commit messages of the following commits.
    Please move them to the PR description, to avoid spamming the issues with references to the commit, and so this bot can automatically canonicalize them to avoid issues with subtree.
@asder8215 asder8215 requested a review from bjorn3 October 28, 2025 12:52
@workingjubilee
Copy link
Member

Why does this need to manipulate a list of uncreated_dirs at all? Can't we just path.ancestors().rev() and start making directories? So what if we try to recreate the root of the filesystem. Aside from being silly, we'll just get io::ErrorKind::AlreadyExists.

@workingjubilee
Copy link
Member

workingjubilee commented Oct 28, 2025

🥴 Answer to jubilee: It's not a reversible iterator!!!

@workingjubilee
Copy link
Member

jubilee's counterargument to jubilee's counterargument: Okay, so we'd have to use path.ancestors().nth(i) instead, sure? But you get the idea.

@workingjubilee
Copy link
Member

workingjubilee commented Oct 28, 2025

Please feel free to question/reject my comment/suggestion if you feel it is incorrect/scope-creeping, I only am bringing it up because I had precisely this thought after wondering why code I had earlier written elsewhere was doing try_exists and then using create_dir_all.

@workingjubilee
Copy link
Member

workingjubilee commented Oct 28, 2025

Followup response to jubilee: what about create_dir_all("./somewhere/../../something")?

in the tune of "Khaaan!": RELATIVE PAAATHS!

…t directory and uses a boxed slice to pre-allocate memory for uncreated directories and iterate through & create them efficiently
@asder8215
Copy link
Author

Hey jubilee, I took your suggestion and iterate through the Ancestors Iterator. One thing that I did differently however is that instead of using path.ancestors().nth(i) to repeatedly traverse through the iterator up until the uncreated directory, I used a counter and .take() to pre-allocate the exact memory to hold the remaining directories that I need to create and then just use a reversed iterator on that.

Does this approach look good to you?

@workingjubilee
Copy link
Member

workingjubilee commented Oct 29, 2025

Yes, allocating once instead of many times sounds like a reasonable improvement in the spirit of my note, so if others are cool with it, then I'm cool! That way it's "just" a classic space vs. time tradeoff and getting the main improvement we're looking for.

@asder8215
Copy link
Author

Hi @tgross35, I noticed that you were the assignee for this pull request. I wanted to ask if there is anything I should change with this PR/commits or if everything looks alright?

@tgross35
Copy link
Contributor

tgross35 commented Nov 4, 2025

I haven't looked at this yet so I'm going to

r? @workingjubilee

who has. (I know it's at your limit so feel free to reroll again or throw it back if you want)

@rustbot rustbot assigned workingjubilee and unassigned tgross35 Nov 4, 2025
@rustbot
Copy link
Collaborator

rustbot commented Nov 4, 2025

workingjubilee is currently at their maximum review capacity.
They may take a while to respond.

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.

5 participants