]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Use a buffer to reduce syscall.Write calls
authorMatt Joiner <anacrolix@gmail.com>
Fri, 21 Nov 2014 06:04:07 +0000 (00:04 -0600)
committerMatt Joiner <anacrolix@gmail.com>
Fri, 21 Nov 2014 06:04:07 +0000 (00:04 -0600)
connection.go

index c1f337fc8cba35b0fcdf9ce3a5c8622156f7aedf..1a75e17a6f0d15129aef0580d70420fc82ec103c 100644 (file)
@@ -1,6 +1,7 @@
 package torrent
 
 import (
+       "bufio"
        "container/list"
        "encoding"
        "errors"
@@ -301,19 +302,34 @@ var (
 
 // Writes buffers to the socket from the write channel.
 func (conn *connection) writer() {
+       // Reduce write syscalls.
+       buf := bufio.NewWriterSize(conn.Socket, 0x8000) // 32 KiB
+       // Returns immediately if the buffer contains data.
+       notEmpty := make(chan struct{}, 1)
        for {
+               if buf.Buffered() != 0 {
+                       select {
+                       case notEmpty <- struct{}{}:
+                       default:
+                       }
+               }
                select {
                case b, ok := <-conn.writeCh:
                        if !ok {
                                return
                        }
-                       _, err := conn.Socket.Write(b)
+                       _, err := buf.Write(b)
                        if err != nil {
                                conn.Close()
                                return
                        }
                case <-conn.closing:
                        return
+               case <-notEmpty:
+                       err := buf.Flush()
+                       if err != nil {
+                               return
+                       }
                }
        }
 }