Reduce allocations by using #each_with_object (#7758)

Merge pull request 7758
This commit is contained in:
Ashwin Maroli 2019-08-05 01:38:54 +05:30 committed by jekyllbot
parent 2c7cbddeba
commit 65f8831168
3 changed files with 13 additions and 10 deletions

View File

@ -4,7 +4,7 @@ module Jekyll
class Configuration < Hash class Configuration < Hash
# Default options. Overridden by values in _config.yml. # Default options. Overridden by values in _config.yml.
# Strings rather than symbols are used for compatibility with YAML. # Strings rather than symbols are used for compatibility with YAML.
DEFAULTS = Configuration[{ DEFAULTS = {
# Where things are # Where things are
"source" => Dir.pwd, "source" => Dir.pwd,
"destination" => File.join(Dir.pwd, "_site"), "destination" => File.join(Dir.pwd, "_site"),
@ -75,7 +75,7 @@ module Jekyll
"footnote_nr" => 1, "footnote_nr" => 1,
"show_warnings" => false, "show_warnings" => false,
}, },
}.map { |k, v| [k, v.freeze] }].freeze }.each_with_object(Configuration.new) { |(k, v), hsh| hsh[k] = v.freeze }.freeze
class << self class << self
# Static: Produce a Configuration ready for use in a Site. # Static: Produce a Configuration ready for use in a Site.
@ -94,7 +94,7 @@ module Jekyll
# #
# Return a copy of the hash where all its keys are strings # Return a copy of the hash where all its keys are strings
def stringify_keys def stringify_keys
reduce({}) { |hsh, (k, v)| hsh.merge(k.to_s => v) } each_with_object({}) { |(k, v), hsh| hsh[k.to_s] = v }
end end
def get_config_value_with_override(config_key, override) def get_config_value_with_override(config_key, override)
@ -235,7 +235,9 @@ module Jekyll
# Ensure we have a hash. # Ensure we have a hash.
if config["collections"].is_a?(Array) if config["collections"].is_a?(Array)
config["collections"] = Hash[config["collections"].map { |c| [c, {}] }] config["collections"] = config["collections"].each_with_object({}) do |collection, hash|
hash[collection] = {}
end
end end
config["collections"] = Utils.deep_merge_hashes( config["collections"] = Utils.deep_merge_hashes(

View File

@ -112,9 +112,10 @@ module Jekyll
# #
# Returns the Hash representation of this Convertible. # Returns the Hash representation of this Convertible.
def to_liquid(attrs = nil) def to_liquid(attrs = nil)
further_data = Hash[(attrs || self.class::ATTRIBUTES_FOR_LIQUID).map do |attribute| further_data = \
[attribute, send(attribute)] (attrs || self.class::ATTRIBUTES_FOR_LIQUID).each_with_object({}) do |attribute, hsh|
end] hsh[attribute] = send(attribute)
end
defaults = site.frontmatter_defaults.all(relative_path, type) defaults = site.frontmatter_defaults.all(relative_path, type)
Utils.deep_merge_hashes defaults, Utils.deep_merge_hashes(data, further_data) Utils.deep_merge_hashes defaults, Utils.deep_merge_hashes(data, further_data)

View File

@ -145,9 +145,9 @@ module Jekyll
# #
# Returns a Hash containing collection name-to-instance pairs. # Returns a Hash containing collection name-to-instance pairs.
def collections def collections
@collections ||= Hash[collection_names.map do |coll| @collections ||= collection_names.each_with_object({}) do |name, hsh|
[coll, Jekyll::Collection.new(self, coll)] hsh[name] = Jekyll::Collection.new(self, name)
end] end
end end
# The list of collection names. # The list of collection names.