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

Make File.Move(..., ..., overwrite: true) more efficient on Unix. #47118

Merged
merged 1 commit into from
Jan 21, 2021

Conversation

gukoff
Copy link
Contributor

@gukoff gukoff commented Jan 18, 2021

The current implementation has an inefficiency. When moving a file across volumes on Unix, and when the target file doesn't exist, the "rename" syscall will be called twice (unsuccesfully) instead of once.

By branching on "overwrite" from the beginning, we avoid calling the first "rename" in vain.

As a bonus, we avoid 2 unnecessary lstat syscalls.

From strace output:

stat("/tmp/dockervol/somedata", {st_mode=S_IFREG|0644, st_size=0, ...}) = 0
lstat("/tmp/dockervol/somedata", {st_mode=S_IFREG|0644, st_size=0, ...}) = 0              <-------- redundant
lstat("/somedata", 0x7ffc9d5abcd0)      = -1 ENOENT (No such file or directory)           <-------- redundant
mprotect(0x7f01a667e000, 4096, PROT_READ|PROT_WRITE) = 0
madvise(0x7f01a667e000, 4096, MADV_DODUMP) = 0
rename("/tmp/dockervol/somedata", "/somedata") = -1 EXDEV (Invalid cross-device link)     <-------- double work
rename("/tmp/dockervol/somedata", "/somedata") = -1 EXDEV (Invalid cross-device link)     <--------ᐟ 
lstat("/usr", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0
lstat("/usr/share", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0

@dnfadmin
Copy link

dnfadmin commented Jan 18, 2021

CLA assistant check
All CLA requirements met.

@gukoff gukoff changed the title Make File.Move(..., ..., overwrite: true) more efficient. Make File.Move(..., ..., overwrite: true) more efficient on Unix. Jan 18, 2021
Copy link
Member

@stephentoub stephentoub left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems reasonable. Thanks.

The current implementation has an inefficiency. When moving a file across volumes, and when
the target file doesn't exist, the "rename" syscall will be called twice (unsuccesfully) instead of once.

By branching on "overwrite" from the beginning, we avoid calling the first "rename" in vain.
@gukoff gukoff force-pushed the make-file-move-more-efficient branch from ffcd6cd to 6db74d9 Compare January 19, 2021 08:47
@stephentoub stephentoub merged commit 661885a into dotnet:master Jan 21, 2021
@ghost ghost locked as resolved and limited conversation to collaborators Mar 5, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants