aboutsummaryrefslogtreecommitdiffstats
path: root/util/stanza.lua
diff options
context:
space:
mode:
authorKim Alvefur <zash@zash.se>2022-03-18 16:43:06 +0100
committerKim Alvefur <zash@zash.se>2022-03-18 16:43:06 +0100
commit47dbb928887f1ece83ca7005eb714a4e937113e5 (patch)
treeecbad4691d96913aace16be5ca88e70971eb0692 /util/stanza.lua
parentceea9b1788ea9f06314c6aeb493c3b63c7ca6c5e (diff)
downloadprosody-47dbb928887f1ece83ca7005eb714a4e937113e5.tar.gz
prosody-47dbb928887f1ece83ca7005eb714a4e937113e5.zip
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.
Diffstat (limited to 'util/stanza.lua')
-rw-r--r--util/stanza.lua17
1 files changed, 15 insertions, 2 deletions
diff --git a/util/stanza.lua b/util/stanza.lua
index 9e249059..a14be5f0 100644
--- a/util/stanza.lua
+++ b/util/stanza.lua
@@ -22,6 +22,7 @@ local s_gsub = string.gsub;
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 @@ function stanza_mt:find(path)
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);