Comparison

net/server_epoll.lua @ 10571:cfeb0077c9e9

net.server_epoll: Avoid concatenating buffer with single item Saves creating a string that'll be identical to buffer[1] anyways, as well as a C function call. Depending on Lua version and length of the string, this could be reusing an interned string, but a longer one would probably be duplicated for no reason. Having exactly one item in the buffer seems like it would be fairly common, but I have not done an extensive study. If opportunistic writes are enabled then it will be even more likely. This special case could be optimized like this in table.concat but it does not look like it is.
author Kim Alvefur <zash@zash.se>
date Sat, 28 Dec 2019 06:18:58 +0100
parent 10546:944863f878b9
child 10586:f86ed818a7f8
comparison
equal deleted inserted replaced
10570:962efe23975d 10571:cfeb0077c9e9
406 -- Called when socket is writable 406 -- Called when socket is writable
407 function interface:onwritable() 407 function interface:onwritable()
408 self:onconnect(); 408 self:onconnect();
409 if not self.conn then return; end -- could have been closed in onconnect 409 if not self.conn then return; end -- could have been closed in onconnect
410 local buffer = self.writebuffer; 410 local buffer = self.writebuffer;
411 local data = t_concat(buffer); 411 local data = #buffer == 1 and buffer[1] or t_concat(buffer);
412 local ok, err, partial = self.conn:send(data); 412 local ok, err, partial = self.conn:send(data);
413 if ok then 413 if ok then
414 self:set(nil, false); 414 self:set(nil, false);
415 for i = #buffer, 1, -1 do 415 for i = #buffer, 1, -1 do
416 buffer[i] = nil; 416 buffer[i] = nil;