Skip to content

Commit

Permalink
Batch sendState calls using a timer
Browse files Browse the repository at this point in the history
Improves libutp->utp performance from ~18.8MiB/s to ~25MiB/s. See issue #3.
  • Loading branch information
anacrolix committed Oct 2, 2015
1 parent a1344fe commit 64e1b89
Showing 1 changed file with 32 additions and 2 deletions.
34 changes: 32 additions & 2 deletions utp.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"fmt"
"io"
"log"
"math"
"math/rand"
"net"
"os"
Expand Down Expand Up @@ -362,6 +363,9 @@ type Conn struct {
connDeadlines

latencies []time.Duration

pendingSendState bool
sendStateTimer *time.Timer
}

type send struct {
Expand Down Expand Up @@ -645,6 +649,15 @@ func (s *Socket) newConn(addr net.Addr) (c *Conn) {
created: time.Now(),
}
c.event.L = &c.mu
c.mu.Lock()
c.sendStateTimer = time.AfterFunc(math.MaxInt64, func() {
c.mu.Lock()
defer c.mu.Unlock()
if !c.pendingSendState {
return
}
c.sendState()
})
c.connDeadlines.read.setCallback(func() {
c.mu.Lock()
c.event.Broadcast()
Expand All @@ -655,6 +668,7 @@ func (s *Socket) newConn(addr net.Addr) (c *Conn) {
c.event.Broadcast()
c.mu.Unlock()
})
c.mu.Unlock()
return
}

Expand Down Expand Up @@ -771,9 +785,25 @@ func (c *Conn) send(_type st, connID uint16, payload []byte, seqNr uint16) (err
if n1 != len(p) {
panic(n1)
}
c.unpendSendState()
return
}

func (me *Conn) unpendSendState() {
me.pendingSendState = false
me.sendStateTimer.Stop()
}

func (c *Conn) pendSendState() {
if c.pendingSendState {
return
}
c.pendingSendState = true
if c.sendStateTimer.Reset(250 * time.Microsecond) {
panic("extended send state timer")
}
}

func (me *Socket) writeTo(b []byte, addr net.Addr) (n int, err error) {
mu.RLock()
apdc := artificialPacketDropChance
Expand Down Expand Up @@ -1051,7 +1081,7 @@ func (c *Conn) deliver(h header, payload []byte) {
}
if !seqLess(c.ack_nr, h.SeqNr) {
if h.Type == stSyn {
c.sendState()
c.pendSendState()
}
// Already received this packet.
return
Expand Down Expand Up @@ -1081,7 +1111,7 @@ func (c *Conn) deliver(h header, payload []byte) {
// if h.Type==stFin{
// c.gotFin
c.processInbound()
c.sendState()
c.pendSendState()
}

func (c *Conn) applyAcks(h header) {
Expand Down

0 comments on commit 64e1b89

Please sign in to comment.