pFad - Phone/Frame/Anonymizer/Declutterfier! Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

URL: http://github.com/php/php-src/pull/20399

.com/assets/global-68dd150ce6c8e711.css" /> IO copy API for stream copying by bukka · Pull Request #20399 · php/php-src · GitHub
Skip to content

IO copy API for stream copying#20399

Draft
bukka wants to merge 30 commits intophp:masterfrom
bukka:io_copy
Draft

IO copy API for stream copying#20399
bukka wants to merge 30 commits intophp:masterfrom
bukka:io_copy

Conversation

@bukka
Copy link
Member

@bukka bukka commented Nov 5, 2025

This introduces new API for fd copying and modifies php_stream_copy_to_stream_ex to use it. The implementation is separated for various platforms and the end result have couple of implications:

  • sendfile is used for copying file to generic fd (e.g. sockets) on linux
  • splice is used for copying between generic fds (e.g. sockets) on linux
  • copy_file_range should get used on alpine linux with directly using syscall (as musl does not seem to implement it)
  • copy_file_range is used in the loop so it is used multiple times for files bigger than 2GB on linux
  • file mmap for copying is removed as it allowed crashing PHP when another process modified mapped file - this was used as a fallback for file copying. Sendfile on Linux should replace it and other platform might see a slight slow down - their sendfile behaves differently but could be at least partially introduced in the future.
  • TransmitFile is used on Windows for copying file to generic fd (socket)
  • file to file copying was also optimized on Windows with use of ReadFile and WriteFile.

@bukka
Copy link
Member Author

bukka commented Mar 15, 2026

I have done some benchmarking of this on Linux. The result vary between runs as it's highly system dependent but I can see some useful result from that:

  • file to file - this seems pretty much the same as both use copy_file_range
  • file to socket - this one shows the biggest improvement as sendfile is clearly faster here. I actually started without this change 682f12b and saw significant slow down because it was using fallback. But after fixing that and verifying that sendfile is used, I saw a significant improvement so this really works.
  • socket to socket and socket to file - both of those might cause slight slow down (overhead) for small copy but it seems improving larger copying where splice pipe overhead disappear. There is also significant reduction of syscalls so this seems like an improvement as well.

So this seems good in terms of Linux perf which probably matter most. We should look after some follow ups to improve FreeBSD, MacOS and other unix variant. The sendfile works a bit differently for partial writes (think it blocks there but I made some changes so it is only used for real socket so it should be fine). there so it needs more testing but I think it can be a follow up PR.

I should also note that the pipe check is there for future use - that pipe flag is only supported on Win but if we supported pipes more on Linux, we could potentially make use of that and it would limit some overhead for splice.

@devnexen Please could you review it.

@bukka bukka requested a review from devnexen March 15, 2026 19:12
@bukka bukka marked this pull request as ready for review March 15, 2026 19:16
@devnexen
Copy link
Member

I ll do a deeper dive sometime next week but here some quick findings.

maxlen = 0;
}

if (php_stream_mmap_possible(src)) {
Copy link
Member

Choose a reason for hiding this comment

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

I m curious of the performance implications here.

Copy link
Member Author

Choose a reason for hiding this comment

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

This impacted just file -> anything other than file and user case which for socket dest is handled by sendfile now so it might slightly regress for cases other than that on linux but sendfile is actually faster from my testing. For other UNIX platforms, we should probably consider sendfile as well. I initially got it so I should consider to put it back to limit impact there. Also file to file will regress there because they don't use copy_file_range (we could consider it again for freebsd maybe).

But I really want to get rid of the mmap because it's causing segfault if file is modified (which we got already report for) so we don't want anything that is causing segfault...

bytes_to_send = (DWORD) min(maxlen, MAXDWORD);
}

if (TransmitFile(dest_sock, file_handle, bytes_to_send, 0, NULL, NULL, 0)) {
Copy link
Member

@devnexen devnexen Mar 15, 2026

Choose a reason for hiding this comment

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

so linux's splice does the work in chunks but seems to me TransmitfFile is its win32 equivalent, I do not know if we should proceed in a similar manner.

Copy link
Member Author

Choose a reason for hiding this comment

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

This is sendfile equivalent. It should be in general safe because it should be only for socket dest but I need to test win more as I can see one test failed (not sure if it's related).

@bukka bukka marked this pull request as draft March 18, 2026 13:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

pFad - Phonifier reborn

Pfad - The Proxy pFad © 2024 Your Company Name. All rights reserved.





Check this box to remove all script contents from the fetched content.



Check this box to remove all images from the fetched content.


Check this box to remove all CSS styles from the fetched content.


Check this box to keep images inefficiently compressed and original size.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy