1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2025-01-02 15:36:41 -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. // NewLargeBuffer creates a Buffer with 64K bytes of arbitrary content.
func NewLargeBuffer() *Buffer { //func NewLargeBuffer() *Buffer {
return largePool.Allocate() // return largePool.Allocate()
} //}
func NewBufferWithSize(size int) *Buffer { //func NewBufferWithSize(size int) *Buffer {
if size <= BufferSize { // if size <= BufferSize {
return NewBuffer() // return NewBuffer()
} // }
//
return NewLargeBuffer() // return NewLargeBuffer()
} //}
func NewLocalBuffer(size int) *Buffer { func NewLocalBuffer(size int) *Buffer {
return CreateBuffer(make([]byte, size), nil) return CreateBuffer(make([]byte, size), nil)

View File

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

View File

@ -11,7 +11,7 @@ import (
func TestBufferedReader(t *testing.T) { func TestBufferedReader(t *testing.T) {
assert := assert.On(t) assert := assert.On(t)
content := alloc.NewLargeBuffer() content := alloc.NewBuffer()
len := content.Len() len := content.Len()
reader := NewBufferedReader(content) reader := NewBufferedReader(content)
@ -31,13 +31,4 @@ func TestBufferedReader(t *testing.T) {
assert.Error(err).IsNil() assert.Error(err).IsNil()
assert.Int(content.Len()).Equals(len2) 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) { func TestBufferedWriter(t *testing.T) {
assert := assert.On(t) assert := assert.On(t)
content := alloc.NewLargeBuffer().Clear() content := alloc.NewBuffer().Clear()
writer := NewBufferedWriter(content) writer := NewBufferedWriter(content)
assert.Bool(writer.Cached()).IsTrue() assert.Bool(writer.Cached()).IsTrue()

View File

@ -19,24 +19,28 @@ func NewChainWriter(writer Writer) *ChainWriter {
} }
func (this *ChainWriter) Write(payload []byte) (int, error) { 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() this.Lock()
defer this.Unlock() defer this.Unlock()
if this.writer == nil { if this.writer == nil {
return 0, io.ErrClosedPipe return 0, io.ErrClosedPipe
} }
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) err := this.writer.Write(buffer)
if err != nil { if err != nil {
return 0, err return 0, err
} }
}
return size, nil return size, nil
} }

View File

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

View File

@ -19,9 +19,4 @@ func TestAdaptiveReader(t *testing.T) {
assert.Error(err).IsNil() assert.Error(err).IsNil()
assert.Bool(b1.IsFull()).IsTrue() assert.Bool(b1.IsFull()).IsTrue()
assert.Int(b1.Len()).Equals(alloc.BufferSize) 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) { func TestAdaptiveWriter(t *testing.T) {
assert := assert.On(t) assert := assert.On(t)
lb := alloc.NewLargeBuffer() lb := alloc.NewBuffer()
rand.Read(lb.Value) rand.Read(lb.Value)
writeBuffer := make([]byte, 0, 1024*1024) writeBuffer := make([]byte, 0, 1024*1024)

View File

@ -12,6 +12,8 @@ import (
"v2ray.com/core" "v2ray.com/core"
"v2ray.com/core/common/log" "v2ray.com/core/common/log"
//"net/http"
//_ "net/http/pprof"
) )
var ( var (
@ -94,6 +96,10 @@ func main() {
return return
} }
//go func() {
// http.ListenAndServe(":6060", nil)
//}()
if point := startV2Ray(); point != nil { if point := startV2Ray(); point != nil {
osSignals := make(chan os.Signal, 1) osSignals := make(chan os.Signal, 1)
signal.Notify(osSignals, os.Interrupt, os.Kill, syscall.SIGTERM) 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) { func (this *ChunkReader) Read() (*alloc.Buffer, error) {
buffer := alloc.NewLargeBuffer() buffer := alloc.NewBuffer()
if _, err := io.ReadFull(this.reader, buffer.Value[:2]); err != nil { if _, err := io.ReadFull(this.reader, buffer.Value[:2]); err != nil {
buffer.Release() buffer.Release()
return nil, err return nil, err

View File

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

View File

@ -60,36 +60,6 @@ func TestLargeIO(t *testing.T) {
if writeSize == len(content) { if writeSize == len(content) {
break 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.Write(alloc.NewBuffer().Clear())
writer.Release() writer.Release()

View File

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