From 122dd5feef0e61ae10879f11e0fd40fdac7c3d26 Mon Sep 17 00:00:00 2001 From: Kornelius Kalnbach Date: Mon, 13 Jun 2016 01:02:48 +0200 Subject: speedup plugin lookup --- lib/coderay/helpers/plugin_host.rb | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) (limited to 'lib/coderay/helpers/plugin_host.rb') diff --git a/lib/coderay/helpers/plugin_host.rb b/lib/coderay/helpers/plugin_host.rb index e9bc17c..12ee29d 100644 --- a/lib/coderay/helpers/plugin_host.rb +++ b/lib/coderay/helpers/plugin_host.rb @@ -47,11 +47,21 @@ module CodeRay # Example: # yaml_plugin = MyPluginHost[:yaml] def [] id, *args, &blk - plugin = validate_id(id) - begin - plugin = plugin_hash.[](plugin, *args, &blk) - end while plugin.is_a? String - plugin + if !args.empty? || blk + plugin = validate_id(id) + begin + plugin = plugin_hash.[](plugin, *args, &blk) + end while plugin.is_a? String + plugin + else + (@cache ||= Hash.new do |cache, key| + plugin = validate_id(key) + begin + plugin = plugin_hash.[](plugin) + end while plugin.is_a? String + cache[key] = plugin + end)[id] + end end alias load [] -- cgit v1.2.1