Diff

net/server_epoll.lua @ 11696:917eca7be82b

net.server_epoll: Optionally let go of buffers Reusing an already existing buffer table would reduce garbage, but keeping it while idle is a waste.
author Kim Alvefur <zash@zash.se>
date Fri, 16 Jul 2021 02:28:32 +0200
parent 11695:1573099157c9
child 11697:6c3d8aa3bbd9
line wrap: on
line diff
--- a/net/server_epoll.lua	Fri Jul 16 01:21:05 2021 +0200
+++ b/net/server_epoll.lua	Fri Jul 16 02:28:32 2021 +0200
@@ -86,6 +86,9 @@
 
 	-- Whether to let the Nagle algorithm stay enabled
 	nagle = true;
+
+	-- Reuse write buffer tables
+	keep_buffers = true;
 }};
 local cfg = default_config.__index;
 
@@ -472,17 +475,25 @@
 	local ok, err, partial = self.conn:send(data);
 	if ok then
 		self:set(nil, false);
-		for i = #buffer, 1, -1 do
-			buffer[i] = nil;
+		if cfg.keep_buffers then
+			for i = #buffer, 1, -1 do
+				buffer[i] = nil;
+			end
+		else
+			self.writebuffer = nil;
 		end
 		self:setwritetimeout(false);
 		self:ondrain(); -- Be aware of writes in ondrain
 		return ok;
 	elseif partial then
 		self:debug("Sent %d out of %d buffered bytes", partial, #data);
-		buffer[1] = data:sub(partial+1);
-		for i = #buffer, 2, -1 do
-			buffer[i] = nil;
+		if cfg.keep_buffers then
+			buffer[1] = data:sub(partial+1);
+			for i = #buffer, 2, -1 do
+				buffer[i] = nil;
+			end
+		else
+			data.writebuffer = { data:sub(partial+1) };
 		end
 		self:set(nil, true);
 		self:setwritetimeout();
@@ -509,6 +520,7 @@
 	if buffer then
 		t_insert(buffer, data);
 	else
+		self:noise("Allocating buffer!")
 		self.writebuffer = { data };
 	end
 	if not self._write_lock then