Kaynağa Gözat

remove large buffer

Darien Raymond 9 yıl önce
ebeveyn
işleme
5a32cd8602

+ 11 - 11
common/alloc/buffer.go

@@ -206,17 +206,17 @@ func NewBuffer() *Buffer {
 }
 
 // NewLargeBuffer creates a Buffer with 64K bytes of arbitrary content.
-func NewLargeBuffer() *Buffer {
-	return largePool.Allocate()
-}
-
-func NewBufferWithSize(size int) *Buffer {
-	if size <= BufferSize {
-		return NewBuffer()
-	}
-
-	return NewLargeBuffer()
-}
+//func NewLargeBuffer() *Buffer {
+//	return largePool.Allocate()
+//}
+
+//func NewBufferWithSize(size int) *Buffer {
+//	if size <= BufferSize {
+//		return NewBuffer()
+//	}
+//
+//	return NewLargeBuffer()
+//}
 
 func NewLocalBuffer(size int) *Buffer {
 	return CreateBuffer(make([]byte, size), nil)

+ 3 - 3
common/alloc/buffer_pool.go

@@ -63,7 +63,7 @@ const (
 
 var (
 	mediumPool *BufferPool
-	largePool  *BufferPool
+	//largePool  *BufferPool
 )
 
 func init() {
@@ -76,6 +76,6 @@ func init() {
 		}
 	}
 	totalByteSize := size * 1024 * 1024
-	mediumPool = NewBufferPool(mediumBufferByteSize, totalByteSize/4*3/mediumBufferByteSize)
-	largePool = NewBufferPool(largeBufferByteSize, totalByteSize/4/largeBufferByteSize)
+	mediumPool = NewBufferPool(mediumBufferByteSize, totalByteSize/mediumBufferByteSize)
+	//largePool = NewBufferPool(largeBufferByteSize, totalByteSize/4/largeBufferByteSize)
 }

+ 1 - 10
common/io/buffered_reader_test.go

@@ -11,7 +11,7 @@ import (
 func TestBufferedReader(t *testing.T) {
 	assert := assert.On(t)
 
-	content := alloc.NewLargeBuffer()
+	content := alloc.NewBuffer()
 	len := content.Len()
 
 	reader := NewBufferedReader(content)
@@ -31,13 +31,4 @@ func TestBufferedReader(t *testing.T) {
 	assert.Error(err).IsNil()
 
 	assert.Int(content.Len()).Equals(len2)
-	reader.SetCached(false)
-
-	payload2 := alloc.NewBuffer()
-	reader.Read(payload2.Value)
-
-	assert.Int(content.Len()).Equals(len2)
-
-	reader.Read(payload2.Value)
-	assert.Int(content.Len()).LessThan(len2)
 }

+ 1 - 1
common/io/buffered_writer_test.go

@@ -11,7 +11,7 @@ import (
 func TestBufferedWriter(t *testing.T) {
 	assert := assert.On(t)
 
-	content := alloc.NewLargeBuffer().Clear()
+	content := alloc.NewBuffer().Clear()
 
 	writer := NewBufferedWriter(content)
 	assert.Bool(writer.Cached()).IsTrue()

+ 15 - 11
common/io/chain_writer.go

@@ -19,24 +19,28 @@ func NewChainWriter(writer Writer) *ChainWriter {
 }
 
 func (this *ChainWriter) Write(payload []byte) (int, error) {
-	if this.writer == nil {
-		return 0, io.ErrClosedPipe
-	}
-
-	size := len(payload)
-	buffer := alloc.NewBufferWithSize(size).Clear()
-	buffer.Append(payload)
-
 	this.Lock()
 	defer this.Unlock()
 	if this.writer == nil {
 		return 0, io.ErrClosedPipe
 	}
 
-	err := this.writer.Write(buffer)
-	if err != nil {
-		return 0, err
+	size := len(payload)
+	for size > 0 {
+		buffer := alloc.NewBuffer().Clear()
+		if size > alloc.BufferSize {
+			buffer.Append(payload[:alloc.BufferSize])
+			size -= alloc.BufferSize
+		} else {
+			buffer.Append(payload)
+			size = 0
+		}
+		err := this.writer.Write(buffer)
+		if err != nil {
+			return 0, err
+		}
 	}
+
 	return size, nil
 }
 

+ 5 - 5
common/io/reader.go

@@ -38,11 +38,11 @@ func (this *AdaptiveReader) Read() (*alloc.Buffer, error) {
 		return nil, err
 	}
 
-	if buffer.Len() >= alloc.BufferSize {
-		this.allocate = alloc.NewLargeBuffer
-	} else {
-		this.allocate = alloc.NewBuffer
-	}
+	//if buffer.Len() >= alloc.BufferSize {
+	//	this.allocate = alloc.NewLargeBuffer
+	//} else {
+	//	this.allocate = alloc.NewBuffer
+	//}
 
 	return buffer, nil
 }

+ 0 - 5
common/io/reader_test.go

@@ -19,9 +19,4 @@ func TestAdaptiveReader(t *testing.T) {
 	assert.Error(err).IsNil()
 	assert.Bool(b1.IsFull()).IsTrue()
 	assert.Int(b1.Len()).Equals(alloc.BufferSize)
-
-	b2, err := reader.Read()
-	assert.Error(err).IsNil()
-	assert.Bool(b2.IsFull()).IsTrue()
-	assert.Int(b2.Len()).Equals(alloc.LargeBufferSize)
 }

+ 1 - 1
common/io/writer_test.go

@@ -13,7 +13,7 @@ import (
 func TestAdaptiveWriter(t *testing.T) {
 	assert := assert.On(t)
 
-	lb := alloc.NewLargeBuffer()
+	lb := alloc.NewBuffer()
 	rand.Read(lb.Value)
 
 	writeBuffer := make([]byte, 0, 1024*1024)

+ 6 - 0
main/main.go

@@ -12,6 +12,8 @@ import (
 
 	"v2ray.com/core"
 	"v2ray.com/core/common/log"
+	//"net/http"
+	//_ "net/http/pprof"
 )
 
 var (
@@ -94,6 +96,10 @@ func main() {
 		return
 	}
 
+	//go func() {
+	//	http.ListenAndServe(":6060", nil)
+	//}()
+
 	if point := startV2Ray(); point != nil {
 		osSignals := make(chan os.Signal, 1)
 		signal.Notify(osSignals, os.Interrupt, os.Kill, syscall.SIGTERM)

+ 1 - 1
proxy/shadowsocks/ota.go

@@ -73,7 +73,7 @@ func (this *ChunkReader) Release() {
 }
 
 func (this *ChunkReader) Read() (*alloc.Buffer, error) {
-	buffer := alloc.NewLargeBuffer()
+	buffer := alloc.NewBuffer()
 	if _, err := io.ReadFull(this.reader, buffer.Value[:2]); err != nil {
 		buffer.Release()
 		return nil, err

+ 1 - 1
proxy/shadowsocks/protocol_test.go

@@ -59,7 +59,7 @@ func TestTCPRequest(t *testing.T) {
 	}
 
 	data := alloc.NewLocalBuffer(256).Clear().AppendString("test string")
-	cache := alloc.NewLargeBuffer().Clear()
+	cache := alloc.NewBuffer().Clear()
 
 	writer, err := WriteTCPRequest(request, cache)
 	assert.Error(err).IsNil()

+ 0 - 30
proxy/vmess/io/io_test.go

@@ -60,36 +60,6 @@ func TestLargeIO(t *testing.T) {
 		if writeSize == len(content) {
 			break
 		}
-
-		chunkSize = 8 * 1024
-		if chunkSize+writeSize > len(content) {
-			chunkSize = len(content) - writeSize
-		}
-		writer.Write(alloc.NewLargeBuffer().Clear().Append(content[writeSize : writeSize+chunkSize]))
-		writeSize += chunkSize
-		if writeSize == len(content) {
-			break
-		}
-
-		chunkSize = 63 * 1024
-		if chunkSize+writeSize > len(content) {
-			chunkSize = len(content) - writeSize
-		}
-		writer.Write(alloc.NewLargeBuffer().Clear().Append(content[writeSize : writeSize+chunkSize]))
-		writeSize += chunkSize
-		if writeSize == len(content) {
-			break
-		}
-
-		chunkSize = 64*1024 - 16
-		if chunkSize+writeSize > len(content) {
-			chunkSize = len(content) - writeSize
-		}
-		writer.Write(alloc.NewLargeBuffer().Clear().Append(content[writeSize : writeSize+chunkSize]))
-		writeSize += chunkSize
-		if writeSize == len(content) {
-			break
-		}
 	}
 	writer.Write(alloc.NewBuffer().Clear())
 	writer.Release()

+ 2 - 2
proxy/vmess/io/reader.go

@@ -51,7 +51,7 @@ func (this *AuthChunkReader) Read() (*alloc.Buffer, error) {
 		buffer = this.last
 		this.last = nil
 	} else {
-		buffer = alloc.NewBufferWithSize(4096).Clear()
+		buffer = alloc.NewBuffer().Clear()
 	}
 
 	if this.chunkLength == -1 {
@@ -97,7 +97,7 @@ func (this *AuthChunkReader) Read() (*alloc.Buffer, error) {
 		}
 		leftLength := buffer.Len() - this.chunkLength
 		if leftLength > 0 {
-			this.last = alloc.NewBufferWithSize(leftLength + 4096).Clear()
+			this.last = alloc.NewBuffer().Clear()
 			this.last.Append(buffer.Value[this.chunkLength:])
 			buffer.Slice(0, this.chunkLength)
 		}