Skip to content

Fix #80061: Copying large files may have suboptimal performance #6078

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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions main/streams/php_stream_mmap.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ typedef struct {

#define PHP_STREAM_MMAP_ALL 0

#define PHP_STREAM_MMAP_MAX (512 * 1024 * 1024)

#define php_stream_mmap_supported(stream) (_php_stream_set_option((stream), PHP_STREAM_OPTION_MMAP_API, PHP_STREAM_MMAP_SUPPORTED, NULL) == 0 ? 1 : 0)

/* Returns 1 if the stream in its current state can be memory mapped,
Expand Down
5 changes: 5 additions & 0 deletions main/streams/plain_wrapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -826,6 +826,11 @@ static int php_stdiop_set_option(php_stream *stream, int option, int value, void
delta = (DWORD)range->offset - loffs;
}

/* MapViewOfFile()ing zero bytes would map to the end of the file; match *nix behavior instead */
if (range->length + delta == 0) {
return PHP_STREAM_OPTION_RETURN_ERR;
}

data->last_mapped_addr = MapViewOfFile(data->file_mapping, acc, 0, loffs, range->length + delta);

if (data->last_mapped_addr) {
Expand Down
53 changes: 36 additions & 17 deletions main/streams/streams.c
Original file line number Diff line number Diff line change
Expand Up @@ -1573,29 +1573,48 @@ PHPAPI int _php_stream_copy_to_stream_ex(php_stream *src, php_stream *dest, size

if (php_stream_mmap_possible(src)) {
char *p;
size_t mapped;

p = php_stream_mmap_range(src, php_stream_tell(src), maxlen, PHP_STREAM_MAP_MODE_SHARED_READONLY, &mapped);
do {
size_t chunk_size = (maxlen == 0 || maxlen > PHP_STREAM_MMAP_MAX) ? PHP_STREAM_MMAP_MAX : maxlen;
size_t mapped;

if (p) {
ssize_t didwrite = php_stream_write(dest, p, mapped);
if (didwrite < 0) {
*len = 0;
return FAILURE;
}
p = php_stream_mmap_range(src, php_stream_tell(src), chunk_size, PHP_STREAM_MAP_MODE_SHARED_READONLY, &mapped);

if (p) {
ssize_t didwrite;

if (php_stream_seek(src, mapped, SEEK_CUR) != 0) {
php_stream_mmap_unmap(src);
break;
}

didwrite = php_stream_write(dest, p, mapped);
if (didwrite < 0) {
*len = haveread;
return FAILURE;
}

php_stream_mmap_unmap_ex(src, mapped);
php_stream_mmap_unmap(src);

*len = didwrite;
*len = haveread += didwrite;

/* we've got at least 1 byte to read
* less than 1 is an error
* AND read bytes match written */
if (mapped > 0 && mapped == didwrite) {
return SUCCESS;
/* we've got at least 1 byte to read
* less than 1 is an error
* AND read bytes match written */
if (mapped == 0 || mapped != didwrite) {
return FAILURE;
}
if (mapped < chunk_size) {
return SUCCESS;
}
if (maxlen != 0) {
maxlen -= mapped;
if (maxlen == 0) {
return SUCCESS;
}
}
}
return FAILURE;
}
} while (p);
}

while(1) {
Expand Down