Changeset

3501:90c18e0355af

util.events: Event handler indices are now built lazily (faster server startup for large number of hosts).
author Waqas Hussain <waqas20@gmail.com>
date Sun, 19 Sep 2010 17:51:00 +0500
parents 3500:a49ed9166820
children 3502:28887137bb1b
files util/events.lua
diffstat 1 files changed, 8 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/util/events.lua	Fri Sep 17 04:09:59 2010 +0500
+++ b/util/events.lua	Sun Sep 19 17:51:00 2010 +0500
@@ -10,21 +10,26 @@
 local pairs = pairs;
 local t_insert = table.insert;
 local t_sort = table.sort;
+local setmetatable = setmetatable;
+local next = next;
 
 module "events"
 
 function new()
 	local handlers = {};
 	local event_map = {};
-	local function _rebuild_index(event) -- TODO optimize index rebuilding
+	local function _rebuild_index(handlers, event)
 		local _handlers = event_map[event];
+		if not _handlers or next(_handlers) == nil then return; end
 		local index = {};
 		for handler in pairs(_handlers) do
 			t_insert(index, handler);
 		end
 		t_sort(index, function(a, b) return _handlers[a] > _handlers[b]; end);
 		handlers[event] = index;
+		return index;
 	end;
+	setmetatable(handlers, { __index = _rebuild_index });
 	local function add_handler(event, handler, priority)
 		local map = event_map[event];
 		if map then
@@ -33,13 +38,13 @@
 			map = {[handler] = priority or 0};
 			event_map[event] = map;
 		end
-		_rebuild_index(event);
+		handlers[event] = nil;
 	end;
 	local function remove_handler(event, handler)
 		local map = event_map[event];
 		if map then
 			map[handler] = nil;
-			_rebuild_index(event);
+			handlers[event] = nil;
 		end
 	end;
 	local function add_handlers(handlers)