diff --git a/common/alloc/buffer.go b/common/alloc/buffer.go index ed5c21315..821808218 100644 --- a/common/alloc/buffer.go +++ b/common/alloc/buffer.go @@ -5,10 +5,6 @@ import ( "io" ) -const ( - defaultOffset = 16 -) - // BytesWriter is a writer that writes contents into the given buffer. type BytesWriter func([]byte) int @@ -28,8 +24,8 @@ func CreateBuffer(container []byte, parent Pool) *Buffer { b := new(Buffer) b.v = container b.pool = parent - b.start = defaultOffset - b.end = defaultOffset + b.start = 0 + b.end = 0 return b } @@ -48,14 +44,8 @@ func (b *Buffer) Release() { // Clear clears the content of the buffer, results an empty buffer with // Len() = 0. func (b *Buffer) Clear() { - b.start = defaultOffset - b.end = defaultOffset -} - -// Reset resets this Buffer into its original state. -func (b *Buffer) Reset() { - b.start = defaultOffset - b.end = len(b.v) + b.start = 0 + b.end = 0 } // AppendBytes appends one or more bytes to the end of the buffer. @@ -75,23 +65,6 @@ func (b *Buffer) AppendFunc(writer BytesWriter) { b.end += nBytes } -// Prepend prepends bytes in front of the buffer. Caller must ensure total bytes prepended is -// no more than 16 bytes. -func (b *Buffer) Prepend(data []byte) { - b.SliceBack(len(data)) - copy(b.v[b.start:], data) -} - -// PrependBytes prepends all data in front of the buffer. -func (b *Buffer) PrependBytes(data ...byte) { - b.Prepend(data) -} - -func (b *Buffer) PrependFunc(offset int, writer BytesWriter) { - b.SliceBack(offset) - writer(b.v[b.start:]) -} - // Byte returns the bytes at index. func (b *Buffer) Byte(index int) byte { return b.v[b.start+index] @@ -108,7 +81,7 @@ func (b *Buffer) Bytes() []byte { } func (b *Buffer) SetBytesFunc(writer BytesWriter) { - b.start = defaultOffset + b.start = 0 b.end = b.start + writer(b.v[b.start:]) } @@ -160,15 +133,6 @@ func (b *Buffer) SliceFrom(from int) { b.start += from } -// SliceBack extends the Buffer to its front by offset bytes. -// Caller must ensure cumulated offset is no more than 16. -func (b *Buffer) SliceBack(offset int) { - b.start -= offset - if b.start < 0 { - panic("Negative buffer offset.") - } -} - // Len returns the length of the buffer content. func (b *Buffer) Len() int { if b == nil { diff --git a/common/alloc/buffer_pool.go b/common/alloc/buffer_pool.go index 6c7d66bce..e305d72b1 100644 --- a/common/alloc/buffer_pool.go +++ b/common/alloc/buffer_pool.go @@ -84,11 +84,8 @@ func (p *BufferPool) Free(buffer *Buffer) { } const ( - mediumBufferByteSize = 8 * 1024 - BufferSize = mediumBufferByteSize - defaultOffset - - smallBufferByteSize = 2 * 1024 - SmallBufferSize = smallBufferByteSize - defaultOffset + BufferSize = 8 * 1024 + SmallBufferSize = 2 * 1024 PoolSizeEnvKey = "v2ray.buffer.size" ) @@ -109,8 +106,8 @@ func init() { } if size > 0 { totalByteSize := size * 1024 * 1024 - mediumPool = NewBufferPool(mediumBufferByteSize, totalByteSize/mediumBufferByteSize) + mediumPool = NewBufferPool(BufferSize, totalByteSize/BufferSize) } else { - mediumPool = NewSyncPool(mediumBufferByteSize) + mediumPool = NewSyncPool(BufferSize) } } diff --git a/common/alloc/buffer_test.go b/common/alloc/buffer_test.go index 8d00985de..6c0492566 100644 --- a/common/alloc/buffer_test.go +++ b/common/alloc/buffer_test.go @@ -31,22 +31,6 @@ func TestBufferIsEmpty(t *testing.T) { assert.Bool(buffer.IsEmpty()).IsTrue() } -func TestBufferPrepend(t *testing.T) { - assert := assert.On(t) - - buffer := NewBuffer() - defer buffer.Release() - - buffer.Append([]byte{'a', 'b', 'c'}) - buffer.Prepend([]byte{'x', 'y', 'z'}) - - assert.Int(buffer.Len()).Equals(6) - assert.String(buffer.String()).Equals("xyzabc") - - buffer.Prepend([]byte{'u', 'v', 'w'}) - assert.String(buffer.String()).Equals("uvwxyzabc") -} - func TestBufferString(t *testing.T) { assert := assert.On(t) @@ -60,7 +44,7 @@ func TestBufferString(t *testing.T) { func TestBufferWrite(t *testing.T) { assert := assert.On(t) - buffer := NewLocalBuffer(24) // 16 + 8 + buffer := NewLocalBuffer(8) nBytes, err := buffer.Write([]byte("abcd")) assert.Error(err).IsNil() assert.Int(nBytes).Equals(4) diff --git a/proxy/vmess/inbound/inbound.go b/proxy/vmess/inbound/inbound.go index 4f7fdca89..f305b679c 100644 --- a/proxy/vmess/inbound/inbound.go +++ b/proxy/vmess/inbound/inbound.go @@ -225,7 +225,7 @@ func (v *VMessInboundHandler) HandleConnection(connection internet.Connection) { } output.Release() if request.Option.Has(protocol.RequestOptionChunkStream) { - if err := bodyWriter.Write(alloc.NewLocalBuffer(32)); err != nil { + if err := bodyWriter.Write(alloc.NewLocalBuffer(8)); err != nil { connection.SetReusable(false) } } diff --git a/proxy/vmess/outbound/outbound.go b/proxy/vmess/outbound/outbound.go index 1f2b143e0..314ab2c05 100644 --- a/proxy/vmess/outbound/outbound.go +++ b/proxy/vmess/outbound/outbound.go @@ -116,7 +116,7 @@ func (v *VMessOutboundHandler) handleRequest(session *encoding.ClientSession, co } if request.Option.Has(protocol.RequestOptionChunkStream) { - err := bodyWriter.Write(alloc.NewLocalBuffer(32)) + err := bodyWriter.Write(alloc.NewLocalBuffer(8)) if err != nil { conn.SetReusable(false) }