Changeset

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
parents 3637:bd491def3efb
children 3639:889ef938552c
files core/xmlhandlers.lua util/stanza.lua util/xmppstream.lua
diffstat 3 files changed, 16 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- a/core/xmlhandlers.lua	Thu Nov 25 08:38:26 2010 +0500
+++ b/core/xmlhandlers.lua	Fri Nov 26 05:23:37 2010 +0500
@@ -114,7 +114,8 @@
 				chardata = {};
 			end
 			-- Complete stanza
-			if #stanza.last_add == 0 then
+			local last_add = stanza.last_add;
+			if not last_add or #last_add == 0 then
 				if tagname ~= stream_error_tag then
 					cb_handlestanza(session, stanza);
 				else
--- a/util/stanza.lua	Thu Nov 25 08:38:26 2010 +0500
+++ b/util/stanza.lua	Fri Nov 26 05:23:37 2010 +0500
@@ -46,7 +46,7 @@
 stanza_mt.__index = stanza_mt;
 
 function stanza(name, attr)
-	local stanza = { name = name, attr = attr or {}, tags = {}, last_add = {}};
+	local stanza = { name = name, attr = attr or {}, tags = {} };
 	return setmetatable(stanza, stanza_mt);
 end
 
@@ -60,26 +60,27 @@
 
 function stanza_mt:tag(name, attrs)
 	local s = stanza(name, attrs);
-	(self.last_add[#self.last_add] or self):add_direct_child(s);
-	t_insert(self.last_add, s);
+	local last_add = self.last_add;
+	if not last_add then last_add = {}; self.last_add = last_add; end
+	(last_add[#last_add] or self):add_direct_child(s);
+	t_insert(last_add, s);
 	return self;
 end
 
 function stanza_mt:text(text)
-	(self.last_add[#self.last_add] or self):add_direct_child(text);
+	local last_add = self.last_add;
+	(last_add and last_add[#last_add] or self):add_direct_child(text);
 	return self;
 end
 
 function stanza_mt:up()
-	t_remove(self.last_add);
+	local last_add = self.last_add;
+	if last_add then t_remove(last_add); end
 	return self;
 end
 
 function stanza_mt:reset()
-	local last_add = self.last_add;
-	for i = 1,#last_add do
-		last_add[i] = nil;
-	end
+	self.last_add = nil;
 	return self;
 end
 
@@ -91,7 +92,8 @@
 end
 
 function stanza_mt:add_child(child)
-	(self.last_add[#self.last_add] or self):add_direct_child(child);
+	local last_add = self.last_add;
+	(last_add and last_add[#last_add] or self):add_direct_child(child);
 	return self;
 end
 
@@ -311,9 +313,6 @@
 				end
 			end
 			stanza.tags = tags;
-			if not stanza.last_add then
-				stanza.last_add = {};
-			end
 		end
 	end
 	
--- a/util/xmppstream.lua	Thu Nov 25 08:38:26 2010 +0500
+++ b/util/xmppstream.lua	Fri Nov 26 05:23:37 2010 +0500
@@ -117,7 +117,8 @@
 				chardata = {};
 			end
 			-- Complete stanza
-			if #stanza.last_add == 0 then
+			local last_add = stanza.last_add;
+			if not last_add or #last_add == 0 then
 				if tagname ~= stream_error_tag then
 					cb_handlestanza(session, stanza);
 				else