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

@ -30,11 +30,8 @@ import (
) )
type packet_send_params struct { type packet_send_params struct {
peer *Peer peer *Peer
usage path.Usage elem *QueueOutboundElement
ttl uint8
packet []byte
offset int
} }
func (device *Device) SendPacket(peer *Peer, usage path.Usage, ttl uint8, packet []byte, offset int) { 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{ device.chan_send_packet <- &packet_send_params{
peer: peer, peer: peer,
usage: usage, elem: elem,
ttl: ttl,
packet: packet,
offset: offset,
} }
} }
@ -91,13 +90,8 @@ func (device *Device) RoutineSendPacket() {
} }
elem = device.NewOutboundElement() elem = device.NewOutboundElement()
params := <-device.chan_send_packet params := <-device.chan_send_packet
offset := params.offset elem := params.elem
packet := params.packet
peer := params.peer 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() { if peer.isRunning.Get() {
peer.StagePacket(elem) peer.StagePacket(elem)
elem = nil elem = nil

View File

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