1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-30 05:56:54 -05:00

remove large buffer

This commit is contained in:
Darien Raymond 2016-11-19 01:50:09 +01:00
parent ac2f62b239
commit 5a32cd8602
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169
13 changed files with 46 additions and 80 deletions

View File

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

View File

@ -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)
}

View File

@ -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)
}

View File

@ -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()

View File

@ -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
}

View File

@ -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
}

View File

@ -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)
}

View File

@ -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)

View File

@ -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)

View File

@ -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

View File

@ -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()

View File

@ -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()

View File

@ -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)
}