Socket #16
Replies: 2 comments
-
fn send(&mut self, data: &[u8]) -> GDResult<()> {
self.socket.send_to(data, &self.complete_address).map_err(|_| PacketSend)?;
Ok(())
} where does the copying using a loop occurs?
Thanks a lot for the suggestions, and again, please DO correct me if I got something wrong! |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
new()
method, you can explicitly annotate the socket variable with thenet::TcpStream
type:receive()
method of both TcpSocket and UdpSocket, you are dynamically allocating a Vec to hold the received data. This can be slow if the data is large, as the allocator needs to find a suitable block of memory. Instead, consider passing in a pre-allocated buffer to thereceive()
method. For example:copy_from_slice()
method - In thesend()
method of TcpSocket and UdpSocket, you are copying data from a slice into a buffer using a loop. This can be slow if the data is large, as each iteration of the loop incurs some overhead. Instead, consider using thecopy_from_slice()
method to copy the data in one go. For example:new()
method of UdpSocket, you are allocating a String to hold the complete address, even though you only need it temporarily. Instead, consider using a stack-allocated buffer to format the address, like this:receive()
method of UdpSocket, you are allocating a new Vec to hold the received data, even though you know the size of the data in advance. Instead, consider using a fixed-size buffer and returning only the actual data received. For example:Beta Was this translation helpful? Give feedback.
All reactions