eliminate some more copying between slices

This commit is contained in:
Arceliar 2019-08-04 14:50:19 -05:00
parent f52955ee0f
commit 75b931f37e
2 changed files with 13 additions and 8 deletions

View file

@ -139,8 +139,10 @@ func (tun *TunAdapter) readerPacketHandler(ch chan []byte) {
continue
}
}
// Shift forward to avoid leaking bytes off the front of the slide when we eventually store it
bs = append(recvd[:0], bs...)
if offset != 0 {
// Shift forward to avoid leaking bytes off the front of the slice when we eventually store it
bs = append(recvd[:0], bs...)
}
// From the IP header, work out what our source and destination addresses
// and node IDs are. We will need these in order to work out where to send
// the packet
@ -277,11 +279,12 @@ func (tun *TunAdapter) readerPacketHandler(ch chan []byte) {
}
func (tun *TunAdapter) reader() error {
recvd := make([]byte, 65535+tun_ETHER_HEADER_LENGTH)
toWorker := make(chan []byte, 32)
defer close(toWorker)
go tun.readerPacketHandler(toWorker)
for {
// Get a slice to store the packet in
recvd := util.ResizeBytes(util.GetBytes(), 65535+tun_ETHER_HEADER_LENGTH)
// Wait for a packet to be delivered to us through the TUN/TAP adapter
n, err := tun.iface.Read(recvd)
if err != nil {
@ -291,9 +294,10 @@ func (tun *TunAdapter) reader() error {
panic(err)
}
if n == 0 {
util.PutBytes(recvd)
continue
}
bs := append(util.GetBytes(), recvd[:n]...)
toWorker <- bs
// Send the packet to the worker
toWorker <- recvd[:n]
}
}