Diff

util/stanza.lua @ 12407:b6b01724e04f

util.stanza: Create tables with correct size to avoid reallocations Potential performance gain since the tables don't need to be resized as they grow to the final size.
author Kim Alvefur <zash@zash.se>
date Fri, 18 Mar 2022 16:43:06 +0100
parent 12406:a3ddf3f42212
child 12636:e8934ce6ea0f
line wrap: on
line diff
--- a/util/stanza.lua	Fri Mar 18 16:39:48 2022 +0100
+++ b/util/stanza.lua	Fri Mar 18 16:43:06 2022 +0100
@@ -22,6 +22,7 @@
 local s_sub         =    string.sub;
 local s_find        =   string.find;
 local t_move        =    table.move or require "util.table".move;
+local t_create = require"util.table".create;
 
 local valid_utf8 = require "util.encodings".utf8.valid;
 
@@ -276,14 +277,26 @@
 end
 
 local function _clone(stanza, only_top)
-	local attr, tags = {}, {};
+	local attr = {};
 	for k,v in pairs(stanza.attr) do attr[k] = v; end
 	local old_namespaces, namespaces = stanza.namespaces;
 	if old_namespaces then
 		namespaces = {};
 		for k,v in pairs(old_namespaces) do namespaces[k] = v; end
 	end
-	local new = { name = stanza.name, attr = attr, namespaces = namespaces, tags = tags };
+	local tags, new;
+	if only_top then
+		tags = {};
+		new = { name = stanza.name, attr = attr, namespaces = namespaces, tags = tags };
+	else
+		tags = t_create(#stanza.tags, 0);
+		new = t_create(#stanza, 4);
+		new.name = stanza.name;
+		new.attr = attr;
+		new.namespaces = namespaces;
+		new.tags = tags;
+	end
+
 	setmetatable(new, stanza_mt);
 	if not only_top then
 		t_move(stanza, 1, #stanza, 1, new);