Reputation: 1221
I just want to ask some questions that I believe could be logical, but I cannot seem to find definite proof on the internet to back it up.
Just a basis of the way to parse messages for this protocol. Read the socket buffer until \n
is met then dispatch the message to the message handler.
Now with that in mind, which would be more efficient.
Method A)
1) read 1 byte from from socket buffer
2) check to see if byte is a newline
3) if it is not append to the array if it is send array to message handler and clear array
4) repeat until connection closed
or
Method B)
1) read multi bytes from socket buffer as one chunk
2) search the array for a newline
3) if newline is found, send the beginning of the array up to the newline to message handler, then remove it from the array shifting remaining data down.
4) repeat until connection closed
If language plays an part on this, it would be done in ruby using its string functions for searching and such.
I would think method B would be faster because it is reading in more data, but then again the string search provides an additional step over method A.
Which would be faster?
Upvotes: 1
Views: 157
Reputation: 27207
In Ruby method B might be faster, but this is a language issue, not a general statement about handling sockets. In both cases the socket buffer, and low level routines that handle that, is providing the I/O performance in a different layer to the application.
If the application keeps up with data from the socket then you might be I/O bound and not notice any difference between approaches.
However, in Ruby, approaches where the code itemises individual numbers/bytes/characters are not efficient, as they generate large numbers of Ruby objects, and each has a small overhead. This makes pure Ruby relatively slow at parsing character-by-character, and if you want your application to perform well, you should use bulk methods on strings, where the character-by-character process happens in better optimised internal methods.
Whether or not this impacts time to complete depends on the speed of the data input in the socket. You might not measure a difference, unless you look at user CPU time. I should add that I am skipping over the more complex scenarios where there is contention for CPU time, such as on a single-core processor.
Upvotes: 1