about summary refs log tree commit diff
path: root/src/libutil/serialise.hh
diff options
context:
space:
mode:
authorEelco Dolstra <e.dolstra@tudelft.nl>2011-12-14T23·30+0000
committerEelco Dolstra <e.dolstra@tudelft.nl>2011-12-14T23·30+0000
commit3a48282b0681d68147e18f7464eaddf1d188c3be (patch)
tree43a5e7b6a16a5fb3ccd5991a30e2214df697d40a /src/libutil/serialise.hh
parent893cac140232478e3ce9640ccf31dbfbfc2434c0 (diff)
* Buffer writes in FdSink. This significantly reduces the number of
  system calls / context switches when dumping a NAR and in the worker
  protocol.

Diffstat (limited to 'src/libutil/serialise.hh')
-rw-r--r--src/libutil/serialise.hh19
1 files changed, 13 insertions, 6 deletions
diff --git a/src/libutil/serialise.hh b/src/libutil/serialise.hh
index 0e797d63bc..711bd5e6c7 100644
--- a/src/libutil/serialise.hh
+++ b/src/libutil/serialise.hh
@@ -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()
+    FdSink() : fd(-1), bufSize(32 * 1024), bufPos(0), buffer(0) { }
+    
+    FdSink(int fd, unsigned int bufSize = 32 * 1024)
+        : fd(fd), bufSize(bufSize), bufPos(0), buffer(0)
     {
-        fd = -1;
     }
-    
-    FdSink(int fd) 
+
+    ~FdSink()
     {
-        this->fd = fd;
+        flush();
+        if (buffer) delete[] buffer;
     }
     
     void operator () (const unsigned char * data, unsigned int len);
+
+    void flush();
 };