5
0
mirror of https://github.com/cwinfo/yggdrasil-go.git synced 2024-09-19 16:09:36 +00:00

use heap.Fix instead of heap.Remove + heap.Push when updating queues, this is theoretically faster

This commit is contained in:
Arceliar 2020-05-25 20:09:57 -05:00
parent 1f65ffb310
commit 09f9f4e8e4

View File

@ -38,12 +38,16 @@ func (q *packetQueue) drop() bool {
longestIdx = idx
}
}
stream := heap.Remove(q, longestIdx).(pqStream)
stream := q.streams[longestIdx]
info := stream.infos[0]
if len(stream.infos) > 1 {
stream.infos = stream.infos[1:]
stream.size -= uint64(len(info.packet))
heap.Push(q, stream)
q.streams[longestIdx] = stream
q.size -= uint64(len(info.packet))
heap.Fix(q, longestIdx)
} else {
heap.Remove(q, longestIdx)
}
pool_putBytes(info.packet)
return true
@ -67,12 +71,16 @@ func (q *packetQueue) push(packet []byte) {
func (q *packetQueue) pop() ([]byte, bool) {
if q.size > 0 {
stream := heap.Pop(q).(pqStream)
stream := q.streams[0]
info := stream.infos[0]
if len(stream.infos) > 1 {
stream.infos = stream.infos[1:]
stream.size -= uint64(len(info.packet))
heap.Push(q, stream)
q.streams[0] = stream
q.size -= uint64(len(info.packet))
heap.Fix(q, 0)
} else {
heap.Remove(q, 0)
}
return info.packet, true
}