From 73afe9a0018d96939b3109355a699358733be11a Mon Sep 17 00:00:00 2001 From: v2ray Date: Wed, 13 Jul 2016 21:47:40 +0200 Subject: [PATCH] use alloc.Buffer in sending queue --- transport/internet/kcp/sending.go | 15 +++++++-------- transport/internet/kcp/sending_test.go | 17 +++++++++-------- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/transport/internet/kcp/sending.go b/transport/internet/kcp/sending.go index 2fb3899fc..a39af80de 100644 --- a/transport/internet/kcp/sending.go +++ b/transport/internet/kcp/sending.go @@ -175,14 +175,14 @@ type SendingQueue struct { start uint32 cap uint32 len uint32 - list []*DataSegment + list []*alloc.Buffer } func NewSendingQueue(size uint32) *SendingQueue { return &SendingQueue{ start: 0, cap: size, - list: make([]*DataSegment, size), + list: make([]*alloc.Buffer, size), len: 0, } } @@ -195,7 +195,7 @@ func (this *SendingQueue) IsEmpty() bool { return this.len == 0 } -func (this *SendingQueue) Pop() *DataSegment { +func (this *SendingQueue) Pop() *alloc.Buffer { if this.IsEmpty() { return nil } @@ -209,7 +209,7 @@ func (this *SendingQueue) Pop() *DataSegment { return seg } -func (this *SendingQueue) Push(seg *DataSegment) { +func (this *SendingQueue) Push(seg *alloc.Buffer) { if this.IsFull() { return } @@ -326,9 +326,7 @@ func (this *SendingWorker) Push(b []byte) int { } else { size = len(b) } - seg := NewDataSegment() - seg.Data = alloc.NewSmallBuffer().Clear().Append(b[:size]) - this.queue.Push(seg) + this.queue.Push(alloc.NewSmallBuffer().Clear().Append(b[:size])) b = b[size:] nBytes += size } @@ -395,7 +393,8 @@ func (this *SendingWorker) Flush(current uint32) { } for !this.queue.IsEmpty() && !this.window.IsFull() { - seg := this.queue.Pop() + seg := NewDataSegment() + seg.Data = this.queue.Pop() seg.Number = this.nextNumber seg.timeout = current seg.ackSkipped = 0 diff --git a/transport/internet/kcp/sending_test.go b/transport/internet/kcp/sending_test.go index 6e8709ade..f627dbab7 100644 --- a/transport/internet/kcp/sending_test.go +++ b/transport/internet/kcp/sending_test.go @@ -3,6 +3,7 @@ package kcp_test import ( "testing" + "github.com/v2ray/v2ray-core/common/alloc" "github.com/v2ray/v2ray-core/testing/assert" . "github.com/v2ray/v2ray-core/transport/internet/kcp" ) @@ -12,10 +13,10 @@ func TestSendingQueue(t *testing.T) { queue := NewSendingQueue(3) - seg0 := &DataSegment{} - seg1 := &DataSegment{} - seg2 := &DataSegment{} - seg3 := &DataSegment{} + seg0 := alloc.NewBuffer() + seg1 := alloc.NewBuffer() + seg2 := alloc.NewBuffer() + seg3 := alloc.NewBuffer() assert.Bool(queue.IsEmpty()).IsTrue() assert.Bool(queue.IsFull()).IsFalse() @@ -44,10 +45,10 @@ func TestSendingQueueClear(t *testing.T) { queue := NewSendingQueue(3) - seg0 := &DataSegment{} - seg1 := &DataSegment{} - seg2 := &DataSegment{} - seg3 := &DataSegment{} + seg0 := alloc.NewBuffer() + seg1 := alloc.NewBuffer() + seg2 := alloc.NewBuffer() + seg3 := alloc.NewBuffer() queue.Push(seg0) assert.Bool(queue.IsEmpty()).IsFalse()