Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[draft] Store the path in io::Error without extra allocations. #86826

Closed
wants to merge 1 commit into from

Conversation

m-ou-se
Copy link
Member

@m-ou-se m-ou-se commented Jul 2, 2021

This stores the path of the file/directory in io::Error, without doing any allocations and without increasing the size of io::Error.

We need to allocate for doing a syscall on a path anyway, so this just keeps that allocation around in the io::Error if the syscall fails.

This implements it only on unix. But it can work similarly on other platforms as well.

It does not store the path for the rename and (sym)link operations, as they involve two paths, and you can't really tell which of the two paths caused the error.

This only shows the path in the Debug implementation of io::Error. It's not clear what else we should do with it. Putting it in Display will make many programs show the path twice, and have less control over the output format. Adding a .path() accessor to io::Error is also not great, because it'll have to be a Option, and won't be accessible through the Error triat.

@m-ou-se m-ou-se added T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. A-error-handling Area: Error handling T-libs Relevant to the library team, which will review and decide on the PR/issue. A-io Area: `std::io`, `std::fs`, `std::net` and `std::path` labels Jul 2, 2021
@m-ou-se m-ou-se self-assigned this Jul 2, 2021
@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jul 2, 2021
@m-ou-se
Copy link
Member Author

m-ou-se commented Jul 2, 2021

cc @yaahc

@m-ou-se m-ou-se added S-experimental Status: Ongoing experiment that does not require reviewing and won't be merged in its current state. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jul 2, 2021
@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@m-ou-se
Copy link
Member Author

m-ou-se commented Jul 2, 2021

@CryZe Since you reacted with a 👎, I'd be curious to hear your thoughts on this.

@rust-log-analyzer

This comment has been minimized.

We need to allocate for doing a syscall on a path anyway, so this just
keeps that allocation around in the io::Error if the syscall fails.
@CryZe

This comment has been minimized.

@yaahc
Copy link
Member

yaahc commented Jul 2, 2021

@CryZe I will note that mara has only added the path info to the Debug format, not to Display, precisely because people already add the path at a higher level where they have that context on the semantics of it's usage. Does this change your reaction?

@CryZe
Copy link
Contributor

CryZe commented Jul 2, 2021

Yeah I guess that may be fine then.

@bors
Copy link
Contributor

bors commented Aug 20, 2021

☔ The latest upstream changes (presumably #87329) made this pull request unmergeable. Please resolve the merge conflicts.

@bors bors added the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Aug 20, 2021
@sunfishcode
Copy link
Member

It's possible that in the future, std could be optimized to avoid allocing the path buffer in common cases, using a technique like this which can be about 2.5% faster in microbenchmarks that include the system call. If Rust does that, would it still be ok allocing in the error path to continue to support storing the path in the Error?

@yaahc yaahc added the PG-error-handling Project group: Error handling (https://github.com/rust-lang/project-error-handling) label Sep 27, 2021
@thomcc
Copy link
Member

thomcc commented Jan 31, 2022

Since it was brought up to me, #87869 and this aren't really incompatible beyond it needing to allocate in the 64 bit case (on 32 bit targets it would be almost identical).

(IMO that's probably fine, since this is in the error case, and that having a smaller io::Error optimizes the success path more than allocating in the error case would pessimize anything)

@ben0x539
Copy link
Contributor

When making the OsPathBuf allocation, can't you just leave some room for the error kind field in there, even if it's not needed in the success case? (With 64 bit, maybe half of the string length usize unless we assume path names can be too long for u32 length field?)

@ben0x539
Copy link
Contributor

ben0x539 commented Jan 31, 2022

It does not store the path for the rename and (sym)link operations, as they involve two paths, and you can't really tell which of the two paths caused the error.

Can we store both of them, like, allocated contiguously?

@saethlin
Copy link
Member

saethlin commented Oct 8, 2022

@sunfishcode The link in your comment above is now broken, at a guess because it wasn't a permalink that included the git commit. Did you mean this: https://github.com/bytecodealliance/rustix/blob/e1ec8abf106e976bc05ce45d308602728e84ccf3/src/path/arg.rs#L933-L953

@sunfishcode
Copy link
Member

@saethlin Yes; that's the right link. But also, there is now a PR implenting this in Rust, which may land soon: #93668

@Dylan-DPC
Copy link
Member

Closing this as inactive and was still a draft.

@Dylan-DPC Dylan-DPC closed this Jul 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-error-handling Area: Error handling A-io Area: `std::io`, `std::fs`, `std::net` and `std::path` PG-error-handling Project group: Error handling (https://github.com/rust-lang/project-error-handling) S-experimental Status: Ongoing experiment that does not require reviewing and won't be merged in its current state. S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-libs Relevant to the library team, which will review and decide on the PR/issue. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.