Skip to content

Commit d9c447a

Browse files
committed
chore: remove unneeded copy in uploadQueue
1 parent 6791df1 commit d9c447a

File tree

2 files changed

+9
-11
lines changed

2 files changed

+9
-11
lines changed

transport/xhttp/server.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ func (c *httpServerConn) Wait() <-chan struct{} {
7575
}
7676

7777
type httpSession struct {
78-
uploadQueue *uploadQueue
78+
uploadQueue *UploadQueue
7979
connected chan struct{}
8080
once sync.Once
8181
}

transport/xhttp/upload_queue.go

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ import (
88

99
type Packet struct {
1010
Seq uint64
11-
Payload []byte
11+
Payload []byte // UploadQueue will hold Payload, so never reuse it after UploadQueue.Push
1212
Reader io.ReadCloser
1313
}
1414

15-
type uploadQueue struct {
15+
type UploadQueue struct {
1616
mu sync.Mutex
1717
cond *sync.Cond
1818
packets map[uint64][]byte
@@ -22,15 +22,15 @@ type uploadQueue struct {
2222
reader io.ReadCloser
2323
}
2424

25-
func NewUploadQueue() *uploadQueue {
26-
q := &uploadQueue{
25+
func NewUploadQueue() *UploadQueue {
26+
q := &UploadQueue{
2727
packets: make(map[uint64][]byte),
2828
}
2929
q.cond = sync.NewCond(&q.mu)
3030
return q
3131
}
3232

33-
func (q *uploadQueue) Push(p Packet) error {
33+
func (q *UploadQueue) Push(p Packet) error {
3434
q.mu.Lock()
3535
defer q.mu.Unlock()
3636

@@ -48,14 +48,12 @@ func (q *uploadQueue) Push(p Packet) error {
4848
return nil
4949
}
5050

51-
cp := make([]byte, len(p.Payload))
52-
copy(cp, p.Payload)
53-
q.packets[p.Seq] = cp
51+
q.packets[p.Seq] = p.Payload
5452
q.cond.Broadcast()
5553
return nil
5654
}
5755

58-
func (q *uploadQueue) Read(b []byte) (int, error) {
56+
func (q *UploadQueue) Read(b []byte) (int, error) {
5957
q.mu.Lock()
6058

6159
for {
@@ -87,7 +85,7 @@ func (q *uploadQueue) Read(b []byte) (int, error) {
8785
}
8886
}
8987

90-
func (q *uploadQueue) Close() error {
88+
func (q *UploadQueue) Close() error {
9189
q.mu.Lock()
9290
defer q.mu.Unlock()
9391

0 commit comments

Comments
 (0)