changeset 2070:2356114ff505

mod_firewall: Optimize string match operations, string.find is faster than .match since no string is returned
author Kim Alvefur <zash@zash.se>
date Fri, 11 Mar 2016 18:13:53 +0100
parents cf9cd666ba00
children 4161ff87e5a4
files mod_firewall/actions.lib.lua mod_firewall/conditions.lib.lua mod_firewall/mod_firewall.lua
diffstat 3 files changed, 12 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/mod_firewall/actions.lib.lua	Thu Mar 10 19:00:04 2016 +0100
+++ b/mod_firewall/actions.lib.lua	Fri Mar 11 18:13:53 2016 +0100
@@ -29,7 +29,7 @@
 				else
 					table.insert(attr_str, ", ");
 				end
-				if k:match("^%a%w*$") then
+				if k:find("^%a%w*$") then
 					table.insert(attr_str, string.format("%s = %q", k, v));
 				else
 					table.insert(attr_str, string.format("[%q] = %q", k, v));
@@ -45,7 +45,7 @@
 				code[#code+1] = (string.format(":tag(%q%s)", name, table.concat(attr_str)));
 			end
 		end
-		if text and text:match("%S") then
+		if text and text:find("%S") then
 			code[#code+1] = (string.format(":text(%q)", text));
 		elseif short_close then
 			short_close = nil;
--- a/mod_firewall/conditions.lib.lua	Thu Mar 10 19:00:04 2016 +0100
+++ b/mod_firewall/conditions.lib.lua	Fri Mar 11 18:13:53 2016 +0100
@@ -28,7 +28,7 @@
 		if pattern == "*" then
 			return part;
 		end
-		if pattern:match("^<.*>$") then
+		if pattern:find("^<.*>$") then
 			pattern = pattern:match("^<(.*)>$");
 		else
 			pattern = pattern:gsub("%p", "%%%0"):gsub("%%(%p)", wildcard_equivs);
@@ -144,7 +144,7 @@
 				op = "or";
 			end
 			table.insert(conditions, ("current_day >= %d %s current_day <= %d"):format(day_start_num, op, day_end_num));
-		elseif day_range:match("%a") then
+		elseif day_range:find("%a") then
 			local day = resolve_day_number(day_range:match("%a+"));
 			table.insert(conditions, "current_day == "..day);
 		else
--- a/mod_firewall/mod_firewall.lua	Thu Mar 10 19:00:04 2016 +0100
+++ b/mod_firewall/mod_firewall.lua	Fri Mar 11 18:13:53 2016 +0100
@@ -166,7 +166,7 @@
 		end
 		line_no = line_no + 1;
 
-		if line_hold or line:match("^[#;]") then
+		if line_hold or line:find("^[#;]") then
 			-- No action; comment or partial line
 		elseif line == "" then
 			if state == "rules" then
@@ -174,7 +174,7 @@
 					:format(line_no);
 			end
 			state = nil;
-		elseif not(state) and line:match("^::") then
+		elseif not(state) and line:sub(1, 2) == "::" then
 			chain = line:gsub("^::%s*", "");
 			local chain_info = chains[chain];
 			if not chain_info then
@@ -183,7 +183,7 @@
 				return nil, errmsg("Only event chains supported at the moment");
 			end
 			ruleset[chain] = ruleset[chain] or {};
-		elseif not(state) and line:match("^%%") then -- Definition (zone, limit, etc.)
+		elseif not(state) and line:sub(1,1) == "%" then -- Definition (zone, limit, etc.)
 			local what, name = line:match("^%%%s*(%w+) +([^ :]+)");
 			if not definition_handlers[what] then
 				return nil, errmsg("Definition of unknown object: "..what);
@@ -192,7 +192,7 @@
 			end
 
 			local val = line:match(": ?(.*)$");
-			if not val and line:match(":<") then -- Read from file
+			if not val and line:find(":<") then -- Read from file
 				local fn = line:match(":< ?(.-)%s*$");
 				if not fn then
 					return nil, errmsg("Unable to parse filename");
@@ -214,7 +214,7 @@
 				active_definitions[what] = {};
 			end
 			active_definitions[what][name] = ret;
-		elseif line:match("^[^%s:]+[%.=]") then
+		elseif line:find("^[^%s:]+[%.=]") then
 			-- Action
 			if state == nil then
 				-- This is a standalone action with no conditions
@@ -247,7 +247,7 @@
 			-- Check standard modifiers for the condition (e.g. NOT)
 			local negated;
 			local condition = line:match("^[^:=%.]*");
-			if condition:match("%f[%w]NOT%f[^%w]") then
+			if condition:find("%f[%w]NOT%f[^%w]") then
 				local s, e = condition:match("%f[%w]()NOT()%f[^%w]");
 				condition = (condition:sub(1,s-1)..condition:sub(e+1, -1)):match("^%s*(.-)%s*$");
 				negated = true;
@@ -281,7 +281,7 @@
 		-- chain (filter-based will be added later)
 		for _, rule in ipairs(rules) do
 			for _, condition in ipairs(rule.conditions) do
-				if condition:match("^not%(.+%)$") then
+				if condition:find("^not%(.+%)$") then
 					condition = condition:match("^not%((.+)%)$");
 				end
 				condition_uses[condition] = (condition_uses[condition] or 0) + 1;
@@ -376,7 +376,7 @@
 						for _, event_name in ipairs(chain_definition) do
 							module:hook(event_name, handler, chain_definition.priority);
 						end
-					elseif not chain:match("^user/") then
+					elseif not chain:sub(1, 5) == "user/" then
 						module:log("warn", "Unknown chain %q", chain);
 					end
 					module:hook("firewall/chains/"..chain, handler);