minor performance adjustments

This commit is contained in:
Arceliar 2018-01-18 18:48:34 -06:00
parent e3d87b8ee6
commit f929df1ea9
3 changed files with 34 additions and 15 deletions

View File

@ -138,7 +138,14 @@ func (r *router) sendPacket(bs []byte) {
fallthrough fallthrough
//default: go func() { sinfo.send<-bs }() //default: go func() { sinfo.send<-bs }()
default: default:
sinfo.send <- bs for {
select {
case sinfo.send <- bs:
return
default:
util_putBytes(<-sinfo.send)
}
}
} }
} }
@ -189,7 +196,14 @@ func (r *router) handleTraffic(packet []byte) {
return return
} }
//go func () { sinfo.recv<-&p }() //go func () { sinfo.recv<-&p }()
sinfo.recv <- &p for {
select {
case sinfo.recv <- &p:
return
default:
util_putBytes((<-sinfo.recv).payload)
}
}
} }
func (r *router) handleProto(packet []byte) { func (r *router) handleProto(packet []byte) {

View File

@ -125,13 +125,14 @@ type switchMessage struct {
type switchPort uint64 type switchPort uint64
type tableElem struct { type tableElem struct {
locator switchLocator port switchPort
firstSeen time.Time firstSeen time.Time
locator switchLocator
} }
type lookupTable struct { type lookupTable struct {
self switchLocator self switchLocator
elems map[switchPort]tableElem elems []tableElem
} }
type switchData struct { type switchData struct {
@ -163,7 +164,7 @@ func (t *switchTable) init(core *Core, key sigPubKey) {
peers := make(map[switchPort]peerInfo) peers := make(map[switchPort]peerInfo)
t.data = switchData{locator: locator, peers: peers} t.data = switchData{locator: locator, peers: peers}
t.updater.Store(&sync.Once{}) t.updater.Store(&sync.Once{})
t.table.Store(lookupTable{elems: make(map[switchPort]tableElem)}) t.table.Store(lookupTable{})
t.drop = make(map[sigPubKey]int64) t.drop = make(map[sigPubKey]int64)
doTicker := func() { doTicker := func() {
ticker := time.NewTicker(time.Second) ticker := time.NewTicker(time.Second)
@ -380,18 +381,19 @@ func (t *switchTable) updateTable() {
defer t.mutex.RUnlock() defer t.mutex.RUnlock()
newTable := lookupTable{ newTable := lookupTable{
self: t.data.locator.clone(), self: t.data.locator.clone(),
elems: make(map[switchPort]tableElem), elems: make([]tableElem, 0, len(t.data.peers)),
} }
for _, pinfo := range t.data.peers { for _, pinfo := range t.data.peers {
//if !pinfo.forward { continue } //if !pinfo.forward { continue }
loc := pinfo.locator.clone() loc := pinfo.locator.clone()
loc.coords = loc.coords[:len(loc.coords)-1] // Remove the them->self link loc.coords = loc.coords[:len(loc.coords)-1] // Remove the them->self link
newTable.elems[pinfo.port] = tableElem{ newTable.elems = append(newTable.elems, tableElem{
locator: loc, locator: loc,
//degree: pinfo.degree, //degree: pinfo.degree,
firstSeen: pinfo.firstSeen, firstSeen: pinfo.firstSeen,
//forward: pinfo.forward, //forward: pinfo.forward,
} port: pinfo.port,
})
} }
t.table.Store(newTable) t.table.Store(newTable)
} }
@ -414,7 +416,7 @@ func (t *switchTable) lookup(dest []byte, ttl uint64) (switchPort, uint64) {
} }
// score is in units of bandwidth / distance // score is in units of bandwidth / distance
bestScore := float64(-1) bestScore := float64(-1)
for port, info := range table.elems { for _, info := range table.elems {
if info.locator.root != table.self.root { if info.locator.root != table.self.root {
continue continue
} }
@ -422,10 +424,10 @@ func (t *switchTable) lookup(dest []byte, ttl uint64) (switchPort, uint64) {
if !(dist < myDist) { if !(dist < myDist) {
continue continue
} }
score := getBandwidth(port) score := getBandwidth(info.port)
score /= float64(1 + dist) score /= float64(1 + dist)
if score > bestScore { if score > bestScore {
best = port best = info.port
bestScore = score bestScore = score
} }
} }

View File

@ -223,10 +223,13 @@ func (iface *udpInterface) handleKeys(msg []byte, addr connAddr) {
} }
conn.peer.out = func(msg []byte) { conn.peer.out = func(msg []byte) {
defer func() { recover() }() defer func() { recover() }()
select { for {
case conn.out <- msg: select {
default: case conn.out <- msg:
util_putBytes(msg) return
default:
util_putBytes(<-conn.out)
}
} }
} }
go func() { go func() {