From b1dfff2fdd9f9443e1f48809fb14989f4bcbcaa0 Mon Sep 17 00:00:00 2001 From: Darien Raymond Date: Sat, 15 Apr 2017 21:19:21 +0200 Subject: [PATCH] remove 8k buffer pool --- common/buf/buffer.go | 5 ---- common/buf/buffer_pool.go | 5 +--- common/buf/buffer_test.go | 36 ------------------------- proxy/shadowsocks/protocol.go | 4 +-- proxy/socks/protocol.go | 4 +-- proxy/socks/server.go | 2 +- transport/internet/headers/http/http.go | 6 ++--- transport/internet/kcp/dialer.go | 2 +- transport/internet/kcp/output.go | 2 +- transport/internet/kcp/segment.go | 2 +- transport/internet/udp/hub.go | 2 +- 11 files changed, 13 insertions(+), 57 deletions(-) diff --git a/common/buf/buffer.go b/common/buf/buffer.go index 857b815d..52516e84 100644 --- a/common/buf/buffer.go +++ b/common/buf/buffer.go @@ -181,11 +181,6 @@ func New() *Buffer { return mediumPool.Allocate() } -// NewSmall returns a buffer with 0 length and 2K capacity. -func NewSmall() *Buffer { - return smallPool.Allocate() -} - // NewLocal creates and returns a buffer with 0 length and given capacity on current thread. func NewLocal(size int) *Buffer { return &Buffer{ diff --git a/common/buf/buffer_pool.go b/common/buf/buffer_pool.go index 15c94b66..dbfec4ee 100644 --- a/common/buf/buffer_pool.go +++ b/common/buf/buffer_pool.go @@ -96,16 +96,13 @@ func (p *BufferPool) Free(buffer *Buffer) { const ( // Size of a regular buffer. - Size = 8 * 1024 - // SizeSmall is the size of a small buffer. - SizeSmall = 2 * 1024 + Size = 2 * 1024 poolSizeEnvKey = "v2ray.buffer.size" ) var ( mediumPool Pool - smallPool = NewSyncPool(SizeSmall) ) func getDefaultPoolSize() uint32 { diff --git a/common/buf/buffer_test.go b/common/buf/buffer_test.go index 8c1a94f9..bc52a0f4 100644 --- a/common/buf/buffer_test.go +++ b/common/buf/buffer_test.go @@ -67,39 +67,3 @@ func BenchmarkNewLocalBuffer8192(b *testing.B) { buffer.Release() } } - -func BenchmarkNewBuffer2048(b *testing.B) { - for i := 0; i < b.N; i++ { - buffer := NewSmall() - buffer.Release() - } -} - -func BenchmarkNewLocalBuffer2048(b *testing.B) { - for i := 0; i < b.N; i++ { - buffer := NewLocal(2048) - buffer.Release() - } -} - -func BenchmarkBufferValue(b *testing.B) { - x := Buffer{} - doSomething := func(a Buffer) { - _ = a.Len() - } - b.ResetTimer() - for i := 0; i < b.N; i++ { - doSomething(x) - } -} - -func BenchmarkBufferPointer(b *testing.B) { - x := NewSmall() - doSomething := func(a *Buffer) { - _ = a.Len() - } - b.ResetTimer() - for i := 0; i < b.N; i++ { - doSomething(x) - } -} diff --git a/proxy/shadowsocks/protocol.go b/proxy/shadowsocks/protocol.go index 52a60058..993d7b62 100644 --- a/proxy/shadowsocks/protocol.go +++ b/proxy/shadowsocks/protocol.go @@ -246,7 +246,7 @@ func EncodeUDPPacket(request *protocol.RequestHeader, payload *buf.Buffer) (*buf } account := rawAccount.(*ShadowsocksAccount) - buffer := buf.NewSmall() + buffer := buf.New() ivLen := account.Cipher.IVSize() buffer.AppendSupplier(buf.ReadFullFrom(rand.Reader, ivLen)) iv := buffer.Bytes() @@ -363,7 +363,7 @@ type UDPReader struct { } func (v *UDPReader) Read() (buf.MultiBuffer, error) { - buffer := buf.NewSmall() + buffer := buf.New() err := buffer.AppendSupplier(buf.ReadFrom(v.Reader)) if err != nil { buffer.Release() diff --git a/proxy/socks/protocol.go b/proxy/socks/protocol.go index 238c5803..0175f8a2 100644 --- a/proxy/socks/protocol.go +++ b/proxy/socks/protocol.go @@ -332,7 +332,7 @@ func DecodeUDPPacket(packet []byte) (*protocol.RequestHeader, []byte, error) { } func EncodeUDPPacket(request *protocol.RequestHeader, data []byte) *buf.Buffer { - b := buf.NewSmall() + b := buf.New() b.AppendBytes(0, 0, 0 /* Fragment */) appendAddress(b, request.Address, request.Port) b.Append(data) @@ -348,7 +348,7 @@ func NewUDPReader(reader io.Reader) *UDPReader { } func (r *UDPReader) Read() (buf.MultiBuffer, error) { - b := buf.NewSmall() + b := buf.New() if err := b.AppendSupplier(buf.ReadFrom(r.reader)); err != nil { return nil, err } diff --git a/proxy/socks/server.go b/proxy/socks/server.go index 901c9f8f..5b3c2b5e 100644 --- a/proxy/socks/server.go +++ b/proxy/socks/server.go @@ -181,7 +181,7 @@ func (v *Server) handleUDPPayload(ctx context.Context, conn internet.Connection, log.Access(source, request.Destination, log.AccessAccepted, "") } - dataBuf := buf.NewSmall() + dataBuf := buf.New() dataBuf.Append(data) udpServer.Dispatch(ctx, request.Destination(), dataBuf, func(payload *buf.Buffer) { defer payload.Release() diff --git a/transport/internet/headers/http/http.go b/transport/internet/headers/http/http.go index 4d89bc49..09cd1a21 100644 --- a/transport/internet/headers/http/http.go +++ b/transport/internet/headers/http/http.go @@ -53,7 +53,7 @@ type HeaderReader struct { } func (*HeaderReader) Read(reader io.Reader) (*buf.Buffer, error) { - buffer := buf.NewSmall() + buffer := buf.New() totalBytes := 0 endingDetected := false for totalBytes < maxHeaderLength { @@ -169,7 +169,7 @@ func (c *HttpConn) Close() error { } func formResponseHeader(config *ResponseConfig) *HeaderWriter { - header := buf.NewSmall() + header := buf.New() header.AppendSupplier(serial.WriteString(strings.Join([]string{config.GetFullVersion(), config.GetStatusValue().Code, config.GetStatusValue().Reason}, " "))) header.AppendSupplier(writeCRLF) @@ -194,7 +194,7 @@ type HttpAuthenticator struct { } func (a HttpAuthenticator) GetClientWriter() *HeaderWriter { - header := buf.NewSmall() + header := buf.New() config := a.config.Request header.AppendSupplier(serial.WriteString(strings.Join([]string{config.GetMethodValue(), config.PickUri(), config.GetFullVersion()}, " "))) header.AppendSupplier(writeCRLF) diff --git a/transport/internet/kcp/dialer.go b/transport/internet/kcp/dialer.go index a52d45f2..a39b07d7 100644 --- a/transport/internet/kcp/dialer.go +++ b/transport/internet/kcp/dialer.go @@ -81,7 +81,7 @@ func (c *ClientConnection) ResetSecurity(header internet.PacketHeader, security } func (c *ClientConnection) Run() { - payload := buf.NewSmall() + payload := buf.New() defer payload.Release() for { diff --git a/transport/internet/kcp/output.go b/transport/internet/kcp/output.go index 36614b9d..b2a2deef 100644 --- a/transport/internet/kcp/output.go +++ b/transport/internet/kcp/output.go @@ -20,7 +20,7 @@ type SimpleSegmentWriter struct { func NewSegmentWriter(writer io.Writer) SegmentWriter { return &SimpleSegmentWriter{ writer: writer, - buffer: buf.NewSmall(), + buffer: buf.New(), } } diff --git a/transport/internet/kcp/segment.go b/transport/internet/kcp/segment.go index 6fba4d2b..ded2fc4d 100644 --- a/transport/internet/kcp/segment.go +++ b/transport/internet/kcp/segment.go @@ -63,7 +63,7 @@ func (v *DataSegment) Command() Command { func (v *DataSegment) SetData(data []byte) { if v.Data == nil { - v.Data = buf.NewSmall() + v.Data = buf.New() } v.Data.Reset(func(b []byte) (int, error) { return copy(b, data), nil diff --git a/transport/internet/udp/hub.go b/transport/internet/udp/hub.go index c3271f8a..4fb129f7 100644 --- a/transport/internet/udp/hub.go +++ b/transport/internet/udp/hub.go @@ -136,7 +136,7 @@ L: default: } - buffer := buf.NewSmall() + buffer := buf.New() var noob int var addr *net.UDPAddr err := buffer.AppendSupplier(func(b []byte) (int, error) {