Skip to content

Use sendfile system call on Linux in HTTPServerResponseImpl::sendFile #3277

@carun

Description

@carun

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.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions