Skip to content

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

Merged
stephentoub merged 1 commit into
dotnet:masterfrom
gukoff:make-file-move-more-efficient
Jan 21, 2021
Merged

Make File.Move(..., ..., overwrite: true) more efficient on Unix.#47118
stephentoub merged 1 commit into
dotnet:masterfrom
gukoff:make-file-move-more-efficient

Conversation

@gukoff

@gukoff gukoff commented Jan 18, 2021

Copy link
Copy Markdown
Contributor

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

@ghost ghost added the area-System.IO label Jan 18, 2021
@dnfadmin

dnfadmin commented Jan 18, 2021

Copy link
Copy Markdown

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
Comment thread src/libraries/System.IO.FileSystem/src/System/IO/FileSystem.Unix.cs Outdated

@stephentoub stephentoub left a comment

Copy link
Copy Markdown
Member

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