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

optimize std.fs.Dir.makeOpenPath for the case that the path already exists #12474

Closed
andrewrk opened this issue Aug 18, 2022 · 1 comment · Fixed by #14833
Closed

optimize std.fs.Dir.makeOpenPath for the case that the path already exists #12474

andrewrk opened this issue Aug 18, 2022 · 1 comment · Fixed by #14833
Labels
contributor friendly This issue is limited in scope and/or knowledge of Zig internals. enhancement Solving this issue will likely involve adding new logic or components to the codebase. optimization standard library This issue involves writing Zig code for the standard library.
Milestone

Comments

@andrewrk
Copy link
Member

andrewrk commented Aug 18, 2022

Current implementation:

zig/lib/std/fs.zig

Lines 1406 to 1413 in 79757f2

/// This function performs `makePath`, followed by `openDir`.
/// If supported by the OS, this operation is atomic. It is not atomic on
/// all operating systems.
pub fn makeOpenPath(self: Dir, sub_path: []const u8, open_dir_options: OpenDirOptions) !Dir {
// TODO improve this implementation on Windows; we can avoid 1 call to NtClose
try self.makePath(sub_path);
return self.openDir(sub_path, open_dir_options);
}

Typically, something like this will happen:

mkdirat(AT_FDCWD, "/home/andy/.cache/zig", 0755) = -1 EEXIST (File exists)
openat(AT_FDCWD, "/home/andy/.cache/zig", O_RDONLY|O_CLOEXEC|O_PATH|O_DIRECTORY) = 5

We can optimize for the common case by first attempting the openat() and then falling back to mkdirat() if we get ENOENT. This will make the common case one syscall instead of two.

As a bonus, consider solving the TODO above as well regarding Windows. NtCreateFile is capable of creating and opening a directory with a single call.

@andrewrk andrewrk added enhancement Solving this issue will likely involve adding new logic or components to the codebase. contributor friendly This issue is limited in scope and/or knowledge of Zig internals. optimization standard library This issue involves writing Zig code for the standard library. labels Aug 18, 2022
@andrewrk andrewrk added this to the 0.12.0 milestone Aug 18, 2022
joedavis added a commit to joedavis/zig that referenced this issue Aug 18, 2022
…xists

Uses a single NtCreateFile syscall on windows.

Resolves ziglang#12474
joedavis added a commit to joedavis/zig that referenced this issue Aug 18, 2022
…xists

Uses a single NtCreateFile syscall on windows.

Resolves ziglang#12474
joedavis added a commit to joedavis/zig that referenced this issue Aug 18, 2022
…xists

Uses a single NtCreateFile syscall on windows.

Resolves ziglang#12474
@Jarred-Sumner
Copy link
Contributor

nice

matu3ba pushed a commit to matu3ba/zig that referenced this issue Dec 8, 2022
Uses a single NtCreateFile syscall on windows.

Closes ziglang#12474. Thanks to @joedavis for the PR.
matu3ba pushed a commit to matu3ba/zig that referenced this issue Dec 8, 2022
Uses a single NtCreateFile syscall on windows.

Closes ziglang#12474. Thanks to @joedavis for the PR.
matu3ba pushed a commit to matu3ba/zig that referenced this issue Dec 8, 2022
Uses a single NtCreateFile syscall on windows.

Closes ziglang#12474. Thanks to @joedavis for the PR.
matu3ba pushed a commit to matu3ba/zig that referenced this issue Dec 8, 2022
Uses a single NtCreateFile syscall on windows.

Closes ziglang#12474. Thanks to @joedavis for the PR.
matu3ba pushed a commit to matu3ba/zig that referenced this issue Dec 10, 2022
Uses a single NtCreateFile syscall on windows.

Closes ziglang#12474. Thanks to @joedavis for the PR.
QusaiHroub added a commit to QusaiHroub/zig that referenced this issue Mar 4, 2023
…y exists

Uses a single NtCreateFile syscall on windows.

Closes ziglang#12474. Thanks to @joedavis and @matu3ba.
QusaiHroub added a commit to QusaiHroub/zig that referenced this issue Mar 5, 2023
QusaiHroub added a commit to QusaiHroub/zig that referenced this issue Mar 6, 2023
QusaiHroub added a commit to QusaiHroub/zig that referenced this issue Mar 7, 2023
…y exists

Uses a single NtCreateFile syscall on windows.

Closes ziglang#12474. Thanks to @joedavis and @matu3ba.
QusaiHroub added a commit to QusaiHroub/zig that referenced this issue Mar 7, 2023
QusaiHroub added a commit to QusaiHroub/zig that referenced this issue Mar 7, 2023
QusaiHroub added a commit to QusaiHroub/zig that referenced this issue Mar 10, 2023
…y exists

Uses a single NtCreateFile syscall on windows.

Closes ziglang#12474. Thanks to @joedavis and @matu3ba.
QusaiHroub added a commit to QusaiHroub/zig that referenced this issue Mar 10, 2023
QusaiHroub added a commit to QusaiHroub/zig that referenced this issue Mar 10, 2023
QusaiHroub added a commit to QusaiHroub/zig that referenced this issue Mar 10, 2023
QusaiHroub added a commit to QusaiHroub/zig that referenced this issue Mar 10, 2023
QusaiHroub added a commit to QusaiHroub/zig that referenced this issue Mar 10, 2023
QusaiHroub added a commit to QusaiHroub/zig that referenced this issue Mar 10, 2023
andrewrk pushed a commit to QusaiHroub/zig that referenced this issue Jun 18, 2023
andrewrk pushed a commit to QusaiHroub/zig that referenced this issue Jun 18, 2023
QusaiHroub added a commit to QusaiHroub/zig that referenced this issue Jun 19, 2023
QusaiHroub added a commit to QusaiHroub/zig that referenced this issue Jun 19, 2023
…y exists

Uses a single NtCreateFile syscall on windows.

Closes ziglang#12474. Thanks to @joedavis and @matu3ba.
QusaiHroub added a commit to QusaiHroub/zig that referenced this issue Jun 19, 2023
QusaiHroub added a commit to QusaiHroub/zig that referenced this issue Jun 19, 2023
QusaiHroub added a commit to QusaiHroub/zig that referenced this issue Jun 19, 2023
QusaiHroub added a commit to QusaiHroub/zig that referenced this issue Jun 19, 2023
squeek502 pushed a commit to squeek502/zig that referenced this issue Jul 27, 2023
…y exists

Uses a single NtCreateFile syscall on windows.

Closes ziglang#12474. Thanks to @joedavis and @matu3ba.
squeek502 pushed a commit to squeek502/zig that referenced this issue Jul 27, 2023
squeek502 pushed a commit to squeek502/zig that referenced this issue Jul 27, 2023
squeek502 pushed a commit to squeek502/zig that referenced this issue Jul 27, 2023
squeek502 pushed a commit to squeek502/zig that referenced this issue Jul 27, 2023
QusaiHroub added a commit to QusaiHroub/zig that referenced this issue Jul 28, 2023
…y exists

Uses a single NtCreateFile syscall on windows.

Closes ziglang#12474. Thanks to @joedavis and @matu3ba.
QusaiHroub added a commit to QusaiHroub/zig that referenced this issue Jul 28, 2023
QusaiHroub added a commit to QusaiHroub/zig that referenced this issue Jul 28, 2023
QusaiHroub added a commit to QusaiHroub/zig that referenced this issue Jul 28, 2023
QusaiHroub added a commit to QusaiHroub/zig that referenced this issue Jul 28, 2023
QusaiHroub added a commit to QusaiHroub/zig that referenced this issue Jul 28, 2023
QusaiHroub added a commit to QusaiHroub/zig that referenced this issue Aug 3, 2023
…y exists

Uses a single NtCreateFile syscall on windows.

Closes ziglang#12474. Thanks to @joedavis and @matu3ba.
QusaiHroub added a commit to QusaiHroub/zig that referenced this issue Aug 3, 2023
QusaiHroub added a commit to QusaiHroub/zig that referenced this issue Aug 3, 2023
QusaiHroub added a commit to QusaiHroub/zig that referenced this issue Aug 3, 2023
QusaiHroub added a commit to QusaiHroub/zig that referenced this issue Aug 3, 2023
QusaiHroub added a commit to QusaiHroub/zig that referenced this issue Aug 3, 2023
andrewrk pushed a commit to QusaiHroub/zig that referenced this issue Oct 19, 2023
Uses a single NtCreateFile syscall on windows.

Closes ziglang#12474. Thanks to @joedavis and @matu3ba.
andrewrk added a commit that referenced this issue Oct 22, 2023
…nPath_12474

#12474: std.fs.Dir.makeOpenPath: optimize case, if path already exists
@andrewrk andrewrk modified the milestones: 0.15.0, 0.12.0 Oct 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
contributor friendly This issue is limited in scope and/or knowledge of Zig internals. enhancement Solving this issue will likely involve adding new logic or components to the codebase. optimization standard library This issue involves writing Zig code for the standard library.
Projects
None yet
2 participants