poco
poco copied to clipboard
Use sendfile system call on Linux in HTTPServerResponseImpl::sendFile
At the moment, HTTPServerResponseImpl::sendFile uses StreamCopied::copyStream which in turn uses intermediate buffer copies. This is inefficient on Linux as the buffers are copied from user space to user space and again from user space to kernel space.
https://github.com/pocoproject/poco/blob/3fc3e5f5b8462f7666952b43381383a79b8b5d92/Net/src/HTTPServerResponseImpl.cpp#L131
https://github.com/pocoproject/poco/blob/3fc3e5f5b8462f7666952b43381383a79b8b5d92/Foundation/src/StreamCopier.cpp#L36
As the size of the file increases this will become evident as the kernel buffer cache will get filled up pretty quickly and we have to resort to running
$ free -m
total used free shared buff/cache available
Mem: 32115 1859 12880 1 17374 29812
Swap: 3935 167 3768
$ echo 3 | sudo tee /proc/sys/vm/drop_caches
3
$ free -m
total used free shared buff/cache available
Mem: 32115 1866 29841 1 407 29816
Swap: 3935 167 3768
Linux has sendfile system call. This can be used to speed up the sends and also minimize the memory usage of the process and in turn reduce the kernel buffer cache.
It would require exposing the file descriptors, either at the HTTPResponse (from HTTPSession) level, or at the streams layer. Plus reliably detecting if sendfile() is available (since 2.2, so I think practically can be relied on being there for linux).
This issue is stale because it has been open for 365 days with no activity.
This issue was closed because it has been inactive for 60 days since being marked as stale.
This issue was closed because it has been inactive for 60 days since being marked as stale.