Skip to content

Commit

Permalink
get rid of heap & optimize in slice for better jitter
Browse files Browse the repository at this point in the history
  • Loading branch information
xtaci committed Jan 17, 2017
1 parent b6862bd commit 86aa2c9
Showing 1 changed file with 4 additions and 20 deletions.
24 changes: 4 additions & 20 deletions kcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
package kcp

import (
"container/heap"
"encoding/binary"
"sync/atomic"
)
Expand Down Expand Up @@ -143,7 +142,7 @@ type KCP struct {
snd_buf []Segment
rcv_buf []Segment

acklist ackList
acklist []ackItem

buffer []byte
output Output
Expand All @@ -154,20 +153,6 @@ type ackItem struct {
ts uint32
}

type ackList []ackItem

func (l ackList) Len() int { return len(l) }
func (l ackList) Less(i, j int) bool { return l[i].ts < l[j].ts }
func (l ackList) Swap(i, j int) { l[i], l[j] = l[j], l[i] }
func (l *ackList) Push(x interface{}) { *l = append(*l, x.(ackItem)) }
func (l *ackList) Pop() interface{} {
old := *l
n := len(old)
x := old[n-1]
*l = old[0 : n-1]
return x
}

// NewKCP create a new kcp control object, 'conv' must equal in two endpoint
// from the same connection.
func NewKCP(conv uint32, output Output) *KCP {
Expand Down Expand Up @@ -432,7 +417,7 @@ func (kcp *KCP) parse_una(una uint32) {

// ack append
func (kcp *KCP) ack_push(sn, ts uint32) {
heap.Push(&kcp.acklist, ackItem{sn, ts})
kcp.acklist = append(kcp.acklist, ackItem{sn, ts})
}

func (kcp *KCP) parse_data(newseg *Segment) {
Expand Down Expand Up @@ -628,14 +613,13 @@ func (kcp *KCP) flush() {

// flush acknowledges
ptr := buffer
for kcp.acklist.Len() > 0 {
for i, ack := range kcp.acklist {
size := len(buffer) - len(ptr)
if size+IKCP_OVERHEAD > int(kcp.mtu) {
kcp.output(buffer, size)
ptr = buffer
}
ack := heap.Pop(&kcp.acklist).(ackItem)
if ack.sn >= kcp.rcv_nxt || kcp.acklist.Len() == 0 {
if ack.sn >= kcp.rcv_nxt || len(kcp.acklist)-1 == i {
seg.sn, seg.ts = ack.sn, ack.ts
ptr = seg.encode(ptr)
}
Expand Down

0 comments on commit 86aa2c9

Please sign in to comment.