@@ -88,15 +88,21 @@ impl Remote for RemoteResource {
88
88
let mut buf = [ 0 ; MAX_ENCODED_SIZE ] ; // used to avoid a heap allocation
89
89
let encoded_size = encoding:: encode_size ( data, & mut buf) ;
90
90
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
+
91
98
let mut total_bytes_sent = 0 ;
92
99
let total_bytes = encoded_size. len ( ) + data. len ( ) ;
93
- loop {
100
+ let status = loop {
94
101
let data_to_send = match total_bytes_sent < encoded_size. len ( ) {
95
102
true => & encoded_size[ total_bytes_sent..] ,
96
103
false => & data[ total_bytes_sent - encoded_size. len ( ) ..] ,
97
104
} ;
98
105
99
- let stream = & self . stream ;
100
106
match stream. deref ( ) . write ( data_to_send) {
101
107
Ok ( bytes_sent) => {
102
108
total_bytes_sent += bytes_sent;
@@ -110,7 +116,13 @@ impl Remote for RemoteResource {
110
116
break SendStatus :: ResourceNotFound // should not happen
111
117
}
112
118
}
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
114
126
}
115
127
}
116
128
0 commit comments