Comparison

util/stanza.lua @ 3638:6f58a3063c14

util.stanza, util.xmppstream, core.xmlhandlers: Allow stanza.last_add to be nil, and set it nil by default. Saves a table allocation per-element. 20% faster stanza building.
author Waqas Hussain <waqas20@gmail.com>
date Fri, 26 Nov 2010 05:23:37 +0500
parent 3502:28887137bb1b
child 3726:fa516dc2c2de
comparison
equal deleted inserted replaced
3637:bd491def3efb 3638:6f58a3063c14
44 44
45 stanza_mt = { __type = "stanza" }; 45 stanza_mt = { __type = "stanza" };
46 stanza_mt.__index = stanza_mt; 46 stanza_mt.__index = stanza_mt;
47 47
48 function stanza(name, attr) 48 function stanza(name, attr)
49 local stanza = { name = name, attr = attr or {}, tags = {}, last_add = {}}; 49 local stanza = { name = name, attr = attr or {}, tags = {} };
50 return setmetatable(stanza, stanza_mt); 50 return setmetatable(stanza, stanza_mt);
51 end 51 end
52 52
53 function stanza_mt:query(xmlns) 53 function stanza_mt:query(xmlns)
54 return self:tag("query", { xmlns = xmlns }); 54 return self:tag("query", { xmlns = xmlns });
58 return self:tag("body", attr):text(text); 58 return self:tag("body", attr):text(text);
59 end 59 end
60 60
61 function stanza_mt:tag(name, attrs) 61 function stanza_mt:tag(name, attrs)
62 local s = stanza(name, attrs); 62 local s = stanza(name, attrs);
63 (self.last_add[#self.last_add] or self):add_direct_child(s); 63 local last_add = self.last_add;
64 t_insert(self.last_add, s); 64 if not last_add then last_add = {}; self.last_add = last_add; end
65 (last_add[#last_add] or self):add_direct_child(s);
66 t_insert(last_add, s);
65 return self; 67 return self;
66 end 68 end
67 69
68 function stanza_mt:text(text) 70 function stanza_mt:text(text)
69 (self.last_add[#self.last_add] or self):add_direct_child(text); 71 local last_add = self.last_add;
72 (last_add and last_add[#last_add] or self):add_direct_child(text);
70 return self; 73 return self;
71 end 74 end
72 75
73 function stanza_mt:up() 76 function stanza_mt:up()
74 t_remove(self.last_add); 77 local last_add = self.last_add;
78 if last_add then t_remove(last_add); end
75 return self; 79 return self;
76 end 80 end
77 81
78 function stanza_mt:reset() 82 function stanza_mt:reset()
79 local last_add = self.last_add; 83 self.last_add = nil;
80 for i = 1,#last_add do
81 last_add[i] = nil;
82 end
83 return self; 84 return self;
84 end 85 end
85 86
86 function stanza_mt:add_direct_child(child) 87 function stanza_mt:add_direct_child(child)
87 if type(child) == "table" then 88 if type(child) == "table" then
89 end 90 end
90 t_insert(self, child); 91 t_insert(self, child);
91 end 92 end
92 93
93 function stanza_mt:add_child(child) 94 function stanza_mt:add_child(child)
94 (self.last_add[#self.last_add] or self):add_direct_child(child); 95 local last_add = self.last_add;
96 (last_add and last_add[#last_add] or self):add_direct_child(child);
95 return self; 97 return self;
96 end 98 end
97 99
98 function stanza_mt:get_child(name, xmlns) 100 function stanza_mt:get_child(name, xmlns)
99 for _, child in ipairs(self.tags) do 101 for _, child in ipairs(self.tags) do
309 if type(child) == "table" then 311 if type(child) == "table" then
310 t_insert(tags, child); 312 t_insert(tags, child);
311 end 313 end
312 end 314 end
313 stanza.tags = tags; 315 stanza.tags = tags;
314 if not stanza.last_add then
315 stanza.last_add = {};
316 end
317 end 316 end
318 end 317 end
319 318
320 return stanza; 319 return stanza;
321 end 320 end