]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/commitdiff
plugins.rb: set up fast delegation hash
authorGiuseppe Bilotta <giuseppe.bilotta@gmail.com>
Sun, 2 Sep 2007 09:30:08 +0000 (09:30 +0000)
committerGiuseppe Bilotta <giuseppe.bilotta@gmail.com>
Sun, 2 Sep 2007 09:30:08 +0000 (09:30 +0000)
Delegating a message requires checking if each of the loaded plugins
responds to a given method. This can be time consuming when many
plugins are loaded.

We set up a hash that maps each commonly delegated method to the list of
plugins that respond to it.

lib/rbot/plugins.rb

index e1cf9c9fb23915979af78cfdecad1d6527851d45..9880d720f1fbbdf81a05c860b0e0c226ebe01cb9 100644 (file)
@@ -270,6 +270,16 @@ module Plugins
     attr_reader :bot
     attr_reader :botmodules
 
+    # This is the list of patterns commonly delegated to plugins.
+    # A fast delegation lookup is enabled for them.
+    DEFAULT_DELEGATE_PATTERNS = %r{^(?:
+      connect|names|nick|
+      listen|ctcp_listen|privmsg|unreplied|
+      kick|join|part|quit|
+      save|cleanup|flush_registry|
+      set_.*|event_.*
+    )$}x
+
     def initialize
       @botmodules = {
         :CoreBotModule => [],
@@ -278,6 +288,9 @@ module Plugins
 
       @names_hash = Hash.new
       @commandmappers = Hash.new
+      @delegate_list = Hash.new { |h, k|
+        h[k] = Array.new
+      }
 
       @dirs = []
 
@@ -416,6 +429,8 @@ module Plugins
     def scan
       @failed.clear
       @ignored.clear
+      @delegate_list.clear
+
       processed = Hash.new
 
       @bot.config['plugins.blacklist'].each { |p|
@@ -460,6 +475,11 @@ module Plugins
         end
       }
       debug "finished loading plugins: #{status(true)}"
+      (core_modules + plugins).each { |p|
+       p.methods.grep(DEFAULT_DELEGATE_PATTERNS).each { |m|
+         @delegate_list[m.intern] << p
+       }
+      }
     end
 
     # call the save method for each active plugin