1
0
Fork 0
mirror of https://github.com/NixOS/nix synced 2025-06-27 08:31:16 +02:00

* Buffer writes in FdSink. This significantly reduces the number of

system calls / context switches when dumping a NAR and in the worker
  protocol.
This commit is contained in:
Eelco Dolstra 2011-12-14 23:30:06 +00:00
parent 893cac1402
commit 3a48282b06
4 changed files with 51 additions and 15 deletions

View file

@ -9,7 +9,30 @@ namespace nix {
void FdSink::operator () (const unsigned char * data, unsigned int len)
{
writeFull(fd, data, len);
if (!buffer) buffer = new unsigned char[bufSize];
while (len) {
/* Optimisation: bypass the buffer if the data exceeds the
buffer size and there is no unflushed data. */
if (bufPos == 0 && len >= bufSize) {
writeFull(fd, data, len);
break;
}
/* Otherwise, copy the bytes to the buffer. Flush the buffer
when it's full. */
size_t n = bufPos + len > bufSize ? bufSize - bufPos : len;
memcpy(buffer + bufPos, data, n);
data += n; bufPos += n; len -= n;
if (bufPos == bufSize) flush();
}
}
void FdSink::flush()
{
if (fd == -1 || bufPos == 0) return;
writeFull(fd, buffer, bufPos);
bufPos = 0;
}

View file

@ -28,22 +28,29 @@ struct Source
};
/* A sink that writes data to a file descriptor. */
/* A sink that writes data to a file descriptor (using a buffer). */
struct FdSink : Sink
{
int fd;
unsigned int bufSize, bufPos;
unsigned char * buffer;
FdSink()
{
fd = -1;
}
FdSink() : fd(-1), bufSize(32 * 1024), bufPos(0), buffer(0) { }
FdSink(int fd)
FdSink(int fd, unsigned int bufSize = 32 * 1024)
: fd(fd), bufSize(bufSize), bufPos(0), buffer(0)
{
this->fd = fd;
}
~FdSink()
{
flush();
if (buffer) delete[] buffer;
}
void operator () (const unsigned char * data, unsigned int len);
void flush();
};