@@ -88,15 +88,21 @@ impl Remote for RemoteResource {
8888 let mut buf = [ 0 ; MAX_ENCODED_SIZE ] ; // used to avoid a heap allocation
8989 let encoded_size = encoding:: encode_size ( data, & mut buf) ;
9090
91+ let stream = & self . stream ;
92+ // We want to send the message as a whole whatever it can be possible.
93+ // In this protocol, sending few bytes than the message has no sense and adds latency:
94+ // by the network sending small chunks, and by the receiver allocating memory to decode them.
95+ // If the target is throughput, use TCP instead.
96+ stream. set_nodelay ( false ) . ok ( ) ;
97+
9198 let mut total_bytes_sent = 0 ;
9299 let total_bytes = encoded_size. len ( ) + data. len ( ) ;
93- loop {
100+ let status = loop {
94101 let data_to_send = match total_bytes_sent < encoded_size. len ( ) {
95102 true => & encoded_size[ total_bytes_sent..] ,
96103 false => & data[ total_bytes_sent - encoded_size. len ( ) ..] ,
97104 } ;
98105
99- let stream = & self . stream ;
100106 match stream. deref ( ) . write ( data_to_send) {
101107 Ok ( bytes_sent) => {
102108 total_bytes_sent += bytes_sent;
@@ -110,7 +116,13 @@ impl Remote for RemoteResource {
110116 break SendStatus :: ResourceNotFound // should not happen
111117 }
112118 }
113- }
119+ } ;
120+
121+ // We have already the entire message in the OS buffer, send now, not wait for the next one.
122+ // The message in this protocol has an information meanless.
123+ // The user can process already this unit of data. Do not wait for other possible message.
124+ stream. set_nodelay ( true ) . ok ( ) ;
125+ status
114126 }
115127}
116128
0 commit comments