Repository navigation
shutil.copy() inefficient implementation in Windows #88745
Description
Activity
In Windows shutil.copy() uses _copyfileobj_readinto which copies file in user mode.
In Windows there is an fast API to copy file in kernel mode: CopyFile (see https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-copyfile).- addedstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directory3.8 (EOL)end of lifeend of lifeperformancePerformance or resource usagePerformance or resource usage
on Jul 7, 2021 In Windows there is an fast API to copy file in kernel mode: CopyFile
The possibility of calling CopyFileEx() for shutil.copy2() is discussed in bpo-30044. Note that CopyFileEx() is a high-level Windows API function, not a "kernel mode" copy. It opens the source and destination files and makes multiple system calls in order to copy file data and metadata (e.g. system calls such as NtOpenFile, NtCreateFile, NtReadFile, NtWriteFile, NtQueryInformationFile, NtSetInformationFile, NtQueryEaFile, NtSetEaFile, NtQuerySecurityObject, etc). This includes copying the primary data stream, alternate data streams, file attributes, extended file attributes, and security resource attributes.
Note that CopyFileEx() is a high-level Windows API function, not a "kernel mode" copy.
This is true today, but could change whenever Windows feels like changing it. If we switch to the native API then we'll get any advantage there automatically.
The only challenge is in managing changes to semantics (that is, anything extra we do to "match" Unix that isn't normally how copies work on Windows - personally, I'd rather be more native on Windows anyway).
- added and removedstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directory
on Jul 7, 2021 - added3.12only security fixesonly security fixesand removed3.8 (EOL)end of lifeend of life
on Sep 7, 2022 I think it's time to do this one. I've assigned it to myself, but if someone else gets an implementation together first (it could use a
CopyFileEximplementation in_winapi) I'm happy to review and merge.Performance enhancement can be backported to 3.12. The behaviour of
copy2should be totally unchanged by this, except in obscure edge cases (where they'll likely be more consistent with every other Windows app as a result of the change).We should also clearly document that
copy2may be significantly faster on Windows than the othercopy*functions. Probably also worth looking for optimisations on other platforms as well, particularly if they preserve stat, so that we can say thatcopy2is fastest on most platforms.- added a commit that references this issue
on May 30, 2023
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
Linked PRs