You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Description of issue
Adding a large number of sockets (~40000) to a SocketSet is very inefficient, taking about 10 seconds on my machine (4.2Ghz AMD processor).
SocketSet does not keep an internal index, so it needs to do a full search of the backing storage vector to find where to insert. However, this makes the common use case of adding a large number of sockets very slow.
Potential fix
Add a new field to SocketSet, ex. first_empty_index: usize. When add is called, just run put(self.first_empty_index, &mut self.sockets[self.first_empty_index], socket), then search for the new lowest free space, starting from first_empty_index + 1 rather than 0. When removing a socket, if the removal index is less than first_empty_index, set first_empty_index equal to the removal index.
This should provide better performance for repeated calls to add without needing to make any radical changes to the data structure.
The text was updated successfully, but these errors were encountered:
SocketSet does not keep an internal index, so it needs to do a full search of the backing storage vector to find where to insert. However, this makes the common use case of adding a large number of sockets very slow.
I think that smoltcp is used a lot in a no_std embedded context, where adding a lot of sockets is not really the most common thing. However, if the solution is simple and does not add too much complexity, a PR would be very welcome!
Description of issue
Adding a large number of sockets (~40000) to a
SocketSet
is very inefficient, taking about 10 seconds on my machine (4.2Ghz AMD processor).Steps to reproduce
Run the following code:
Cause
I suspect the cause is the following loop (@ line 76 in socket_set.rs):
SocketSet
does not keep an internal index, so it needs to do a full search of the backing storage vector to find where to insert. However, this makes the common use case ofadd
ing a large number of sockets very slow.Potential fix
Add a new field to
SocketSet
, ex.first_empty_index: usize
. Whenadd
is called, just runput(self.first_empty_index, &mut self.sockets[self.first_empty_index], socket)
, then search for the new lowest free space, starting fromfirst_empty_index + 1
rather than0
. When removing a socket, if the removal index is less thanfirst_empty_index
, setfirst_empty_index
equal to the removal index.This should provide better performance for repeated calls to
add
without needing to make any radical changes to the data structure.The text was updated successfully, but these errors were encountered: