Skip to content

Conversation

@giampaolo
Copy link
Contributor

@giampaolo giampaolo commented Jun 13, 2018

This is a follow up of #7160.
Changes:

https://bugs.python.org/issue33671

...as it's considerably slower for smaller files; e.g. with a 8MiB file:

--- original

~/svn/cpython {33671-release-memoryview}$ ./python  -m timeit -s "import
shutil; f1 = open('f1', 'rb'); f2 = open('f2', 'wb')"
"shutil.copyfileobj(f1, f2)"
200000 loops, best of 5: 1.64 usec per loop

--- _copybinfileobj()

~/svn/cpython {33671-release-memoryview}$ ./python  -m timeit -s "import
shutil; f1 = open('f1', 'rb'); f2 = open('f2', 'wb')"
"shutil.copyfileobj(f1, f2)"
100000 loops, best of 5: 3.3 usec per loop
@giampaolo giampaolo changed the title bpo-33671 remove memoryview() from shutil.copyfileobj() bpo-33671 use memoryview()-based variant of copyfileobj() on Win for files >= 128 MiB Jun 13, 2018
Lib/shutil.py Outdated

if _HAS_FCOPYFILE:
# macOS
elif _HAS_FCOPYFILE:
Copy link
Member

Choose a reason for hiding this comment

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

Both options are always exclusive? macOS might have sendfile() in the future, no?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is now outdated: I updated code to always use fcopyfile on OSX and leave sendfile() alone. This way we avoid invoking sendfile() on OSX which would fail anyway.

Lib/shutil.py Outdated
# considerable speedup by using a readinto()/memoryview()
# variant of copyfileobj(), see:
# https://github.com/python/cpython/pull/7160#discussion_r195162475
elif _WINDOWS and file_size >= 128 * 1024 * 1024:
Copy link
Member

Choose a reason for hiding this comment

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

Would you mind to add a top-level private constant rather than an hardcoded value? (Would it make sense to make it somehow public? Or add an optional parameter...?)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is outdated, I modified code to always use the readinto() variant regardless of file size, see: #7160 (comment)

As per #7160 (comment)
it appears using memoryview() on Windows is faster also for small files
if memoryview() length is equal to file size.
@giampaolo giampaolo changed the title bpo-33671 use memoryview()-based variant of copyfileobj() on Win for files >= 128 MiB bpo-33671 / shutil.copyfile: use memoryview() with dynamic size on Windows Jun 14, 2018
@giampaolo giampaolo merged commit c7f02a9 into python:master Jun 19, 2018
@bedevere-bot
Copy link

@giampaolo: Please replace # with GH- in the commit message next time. Thanks!

@giampaolo giampaolo deleted the 33671-release-memoryview branch June 19, 2018 15:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants