Use f-variants of chmod() and chown()#3479
Merged
felixhandte merged 2 commits intofacebook:devfrom Feb 16, 2023
Merged
Conversation
Somewhat surprisingly, calling `fchmod()` is non-trivially faster than calling `chmod()`, and so on. This commit introduces alternate variants to some common file util functions that take an optional fd. If present, they call the `f`-variant of the underlying function. Otherwise, they fall back to the regular filename-taking version of the function.
Note that the `fd` is only valid while the file is still open. So we need to move the setting calls to before we close the file. However! We cannot do so with the `utime()` call (even though `futimens()` exists) because the follow- ing `close()` call to the `fd` will reset the atime of the file. So it seems the `utime()` call has to happen after the file is closed.
Cyan4973
approved these changes
Feb 10, 2023
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
These variants have lower overhead than their path-taking counterparts. This makes compressing the github dataset something like 10% faster, recovering much of the regression introduced in the 3 added syscalls of #3432.
Note that this requires making some changes to the flow of these syscalls. Previously, we
close()-ed the file, thenutime()-ed it, thench{mod,own}()-ed it. However, anfdis only valid while the file is open, so in order to callfchmod()andfchown(), we need to move those calls to before weclose()the file. While there is also afutimens(), we can't use it, since theutime()call has to come after weclose()the file, so that we end up with the desiredatime. (If weutime()thenclose(), theclose()will override theatimewe set.) So the new order isfch{mod,own}(),close(),utime(). This raises minor questions about whether this will now produce situations where wechown()the file away in such a way that we then don't have permissions toutime()it. But I think those concerns are unfounded.man utimensatsays we need:But we can only
chown()the file away to a differentuidif:So I think it's unlikely there are situations where we can
chown()something away to a state where we can't thenutime()it.