Mercurial > prosody-hg
comparison 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 |
| parents | a3ddf3f42212 |
| children | e8934ce6ea0f |
comparison
equal
deleted
inserted
replaced
| 12406:a3ddf3f42212 | 12407:b6b01724e04f |
|---|---|
| 20 local type = type; | 20 local type = type; |
| 21 local s_gsub = string.gsub; | 21 local s_gsub = string.gsub; |
| 22 local s_sub = string.sub; | 22 local s_sub = string.sub; |
| 23 local s_find = string.find; | 23 local s_find = string.find; |
| 24 local t_move = table.move or require "util.table".move; | 24 local t_move = table.move or require "util.table".move; |
| 25 local t_create = require"util.table".create; | |
| 25 | 26 |
| 26 local valid_utf8 = require "util.encodings".utf8.valid; | 27 local valid_utf8 = require "util.encodings".utf8.valid; |
| 27 | 28 |
| 28 local do_pretty_printing, termcolours = pcall(require, "util.termcolours"); | 29 local do_pretty_printing, termcolours = pcall(require, "util.termcolours"); |
| 29 | 30 |
| 274 self = self:get_child(name, xmlns); | 275 self = self:get_child(name, xmlns); |
| 275 until not self | 276 until not self |
| 276 end | 277 end |
| 277 | 278 |
| 278 local function _clone(stanza, only_top) | 279 local function _clone(stanza, only_top) |
| 279 local attr, tags = {}, {}; | 280 local attr = {}; |
| 280 for k,v in pairs(stanza.attr) do attr[k] = v; end | 281 for k,v in pairs(stanza.attr) do attr[k] = v; end |
| 281 local old_namespaces, namespaces = stanza.namespaces; | 282 local old_namespaces, namespaces = stanza.namespaces; |
| 282 if old_namespaces then | 283 if old_namespaces then |
| 283 namespaces = {}; | 284 namespaces = {}; |
| 284 for k,v in pairs(old_namespaces) do namespaces[k] = v; end | 285 for k,v in pairs(old_namespaces) do namespaces[k] = v; end |
| 285 end | 286 end |
| 286 local new = { name = stanza.name, attr = attr, namespaces = namespaces, tags = tags }; | 287 local tags, new; |
| 288 if only_top then | |
| 289 tags = {}; | |
| 290 new = { name = stanza.name, attr = attr, namespaces = namespaces, tags = tags }; | |
| 291 else | |
| 292 tags = t_create(#stanza.tags, 0); | |
| 293 new = t_create(#stanza, 4); | |
| 294 new.name = stanza.name; | |
| 295 new.attr = attr; | |
| 296 new.namespaces = namespaces; | |
| 297 new.tags = tags; | |
| 298 end | |
| 299 | |
| 287 setmetatable(new, stanza_mt); | 300 setmetatable(new, stanza_mt); |
| 288 if not only_top then | 301 if not only_top then |
| 289 t_move(stanza, 1, #stanza, 1, new); | 302 t_move(stanza, 1, #stanza, 1, new); |
| 290 t_move(stanza.tags, 1, #stanza.tags, 1, tags); | 303 t_move(stanza.tags, 1, #stanza.tags, 1, tags); |
| 291 new:maptags(_clone); | 304 new:maptags(_clone); |
