From 70f741b86fafba6c35faf354d268b63e10d5e037 Mon Sep 17 00:00:00 2001 From: Jordon Bedwell Date: Sat, 9 Jan 2016 05:28:06 -0600 Subject: [PATCH] Remove ObectSpace dumping and start using inherited, it's faster. --- lib/jekyll/generator.rb | 3 +-- lib/jekyll/plugin.rb | 43 +++++++++++++++++++++++++++++------------ lib/jekyll/site.rb | 22 +++++++++------------ 3 files changed, 41 insertions(+), 27 deletions(-) diff --git a/lib/jekyll/generator.rb b/lib/jekyll/generator.rb index 57973a74..bf7c0f19 100644 --- a/lib/jekyll/generator.rb +++ b/lib/jekyll/generator.rb @@ -1,4 +1,3 @@ module Jekyll - class Generator < Plugin - end + Generator = Class.new(Plugin) end diff --git a/lib/jekyll/plugin.rb b/lib/jekyll/plugin.rb index 0207314c..e2a95168 100644 --- a/lib/jekyll/plugin.rb +++ b/lib/jekyll/plugin.rb @@ -1,20 +1,39 @@ module Jekyll class Plugin - PRIORITIES = { :lowest => -100, - :low => -10, - :normal => 0, - :high => 10, - :highest => 100 } + PRIORITIES = { + :low => -10, + :highest => 100, + :lowest => -100, + :normal => 0, + :high => 10 + } - # Fetch all the subclasses of this class and its subclasses' subclasses. # - # Returns an array of descendant classes. - def self.descendants - descendants = [] - ObjectSpace.each_object(singleton_class) do |k| - descendants.unshift k unless k == self + + def self.inherited(const) + return catch_inheritance(const) do |const_| + catch_inheritance(const_) end - descendants + end + + # + + def self.catch_inheritance(const) + const.define_singleton_method :inherited do |const_| + (@children ||= Set.new).add const_ + if block_given? + yield const_ + end + end + end + + # + + def self.descendants + @children ||= Set.new + out = @children.map(&:descendants) + out << self unless superclass == Plugin + Set.new(out).flatten end # Get or set the priority of this plugin. When called without an diff --git a/lib/jekyll/site.rb b/lib/jekyll/site.rb index fcd4bf77..465e154e 100644 --- a/lib/jekyll/site.rb +++ b/lib/jekyll/site.rb @@ -263,25 +263,21 @@ module Jekyll end # Get the implementation class for the given Converter. - # - # klass - The Class of the Converter to fetch. - # # Returns the Converter instance implementing the given Converter. + # klass - The Class of the Converter to fetch. + def find_converter_instance(klass) - converters.find { |c| c.class == klass } || proc { raise "No converter for #{klass}" }.call + converters.find { |klass_| klass_.instance_of?(klass) } || \ + raise("No Converters found for #{klass}") end + # klass - class or module containing the subclasses. + # Returns array of instances of subclasses of parameter. # Create array of instances of the subclasses of the class or module - # passed in as argument. - # - # klass - class or module containing the subclasses which should be - # instantiated - # - # Returns array of instances of subclasses of parameter + # passed in as argument. + def instantiate_subclasses(klass) - klass.descendants.select do |c| - !safe || c.safe - end.sort.map do |c| + klass.descendants.select { |c| !safe || c.safe }.sort.map do |c| c.new(config) end end