From 4c63e9e16861b0418c27d635ff855c7411e8b6e3 Mon Sep 17 00:00:00 2001 From: v2ray Date: Tue, 12 Jul 2016 14:32:17 +0200 Subject: [PATCH] improve fetch performance --- common/alloc/buffer.go | 6 ++++++ transport/internet/kcp/connection.go | 6 +++--- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/common/alloc/buffer.go b/common/alloc/buffer.go index 8dada067..e9109ee8 100644 --- a/common/alloc/buffer.go +++ b/common/alloc/buffer.go @@ -40,6 +40,12 @@ func (b *Buffer) Clear() *Buffer { return b } +func (b *Buffer) Reset() *Buffer { + b.offset = defaultOffset + b.Value = b.head + return b +} + // AppendBytes appends one or more bytes to the end of the buffer. func (b *Buffer) AppendBytes(bytes ...byte) *Buffer { b.Value = append(b.Value, bytes...) diff --git a/transport/internet/kcp/connection.go b/transport/internet/kcp/connection.go index dc2c79aa..42d24710 100644 --- a/transport/internet/kcp/connection.go +++ b/transport/internet/kcp/connection.go @@ -333,11 +333,12 @@ func (this *Connection) updateTask() { func (this *Connection) FetchInputFrom(conn net.Conn) { go func() { + payload := alloc.NewBuffer() + defer payload.Release() for { - payload := alloc.NewBuffer() + payload.Reset() nBytes, err := conn.Read(payload.Value) if err != nil { - payload.Release() return } payload.Slice(0, nBytes) @@ -346,7 +347,6 @@ func (this *Connection) FetchInputFrom(conn net.Conn) { } else { log.Info("KCP|Connection: Invalid response from ", conn.RemoteAddr()) } - payload.Release() } }() }