Remove ObectSpace dumping and start using inherited, it's faster.

This commit is contained in:
Jordon Bedwell 2016-01-09 05:28:06 -06:00
parent 88b970f5dc
commit 70f741b86f
3 changed files with 41 additions and 27 deletions

View File

@ -1,4 +1,3 @@
module Jekyll
class Generator < Plugin
end
Generator = Class.new(Plugin)
end

View File

@ -1,20 +1,39 @@
module Jekyll
class Plugin
PRIORITIES = { :lowest => -100,
PRIORITIES = {
:low => -10,
:highest => 100,
:lowest => -100,
:normal => 0,
:high => 10,
:highest => 100 }
: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

View File

@ -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
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