Fix performance issue(remove redundant copy)

This commit is contained in:
testscript 2023-02-23 13:58:43 +08:00
parent fd551988cd
commit 7775441e24
2 changed files with 17 additions and 22 deletions

View File

@ -31,10 +31,7 @@ import (
type packet_send_params struct {
peer *Peer
usage path.Usage
ttl uint8
packet []byte
offset int
elem *QueueOutboundElement
}
func (device *Device) SendPacket(peer *Peer, usage path.Usage, ttl uint8, packet []byte, offset int) {
@ -72,13 +69,15 @@ func (device *Device) SendPacket(peer *Peer, usage path.Usage, ttl uint8, packet
}
}
}
var elem *QueueOutboundElement
elem = device.NewOutboundElement()
copy(elem.buffer[offset:offset+len(packet)], packet)
elem.Type = usage
elem.TTL = ttl
elem.packet = elem.buffer[offset : offset+len(packet)]
device.chan_send_packet <- &packet_send_params{
peer: peer,
usage: usage,
ttl: ttl,
packet: packet,
offset: offset,
elem: elem,
}
}
@ -91,13 +90,8 @@ func (device *Device) RoutineSendPacket() {
}
elem = device.NewOutboundElement()
params := <-device.chan_send_packet
offset := params.offset
packet := params.packet
elem := params.elem
peer := params.peer
copy(elem.buffer[offset:offset+len(packet)], packet)
elem.Type = params.usage
elem.TTL = params.ttl
elem.packet = elem.buffer[offset : offset+len(packet)]
if peer.isRunning.Get() {
peer.StagePacket(elem)
elem = nil

View File

@ -217,13 +217,11 @@ func (device *Device) RoutineReadFromTUN() {
device.log.Verbosef("Routine: TUN reader - started")
elem := &QueueOutboundElement{
buffer: &[MaxMessageSize]byte{},
}
var elem *QueueOutboundElement
for {
elem = device.NewOutboundElement()
// read packet
offset := MessageTransportHeaderSize
size, err := device.tap.device.Read(elem.buffer[:], offset+path.EgHeaderLen)
@ -279,7 +277,10 @@ func (device *Device) RoutineReadFromTUN() {
if peer == nil {
continue
}
device.SendPacket(peer, elem.Type, elem.TTL, elem.packet, offset)
device.chan_send_packet <- &packet_send_params{
peer: peer,
elem: elem,
}
}
} else {
device.BoardcastPacket(make(map[mtypes.Vertex]bool, 0), elem.Type, elem.TTL, elem.packet, offset)