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.
|
StreamCopier::copyStream(istr, *_pStream); |
|
istr.read(buffer.begin(), bufferSize); |
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.
At the moment,
HTTPServerResponseImpl::sendFileusesStreamCopied::copyStreamwhich 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.poco/Net/src/HTTPServerResponseImpl.cpp
Line 131 in 3fc3e5f
poco/Foundation/src/StreamCopier.cpp
Line 36 in 3fc3e5f
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
Linux has
sendfilesystem 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.