zig icon indicating copy to clipboard operation
zig copied to clipboard

add a flag to `std.fs.Dir.makeOpenPath` to hint whether it is more likely for the directory to already exist

Open andrewrk opened this issue 2 years ago • 2 comments

Currently, std.fs.Dir.makeOpenPath optimistically tries to open an existing directory, falling back to creating it and then opening it if it did not exist. This leads to syscalls like this in the case that the directory needs to be created:

openat(5, "tmp/47bd682a6a9fca69", O_RDONLY|O_CLOEXEC|O_PATH|O_DIRECTORY) = -1 ENOENT (No such file or directory)
mkdirat(5, "tmp/47bd682a6a9fca69", 0755) = 0
openat(5, "tmp/47bd682a6a9fca69", O_RDONLY|O_CLOEXEC|O_PATH|O_DIRECTORY) = 16

This is suboptimal when it is known that the directory is likely to not exist, as in the above example which is creating a new temporary directory based on a random number sequence, intentionally making it be a new directory.

In such case, it should prefer to call mkdirat first, which will result in the following syscall sequence instead:

mkdirat(5, "tmp/47bd682a6a9fca69", 0755) = 0
openat(5, "tmp/47bd682a6a9fca69", O_RDONLY|O_CLOEXEC|O_PATH|O_DIRECTORY) = 16

This will require adding a hint flag to the makeOpenPath function, which is a breaking change.

https://github.com/ziglang/zig/blob/bb0f7d55e8c50e379fa9bdcb8758d89d08e0cc1f/lib/std/fs/Dir.zig#L1172

andrewrk avatar Dec 25 '23 03:12 andrewrk

In regards to naming, I think it should talk about which case you want to optimize for, rather than what's likely. There are a few situations where you want to optimize for the less likely case.

N00byEdge avatar Dec 25 '23 04:12 N00byEdge

Another possibility would be to split it into two functions, e.g. makeOrOpenPath and openOrMakePath

squeek502 avatar Dec 26 '23 10:12 squeek502