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
# Default options. Overridden by values in _config.yml.
# Strings rather than symbols are used for compatibility with YAML.
DEFAULTS = Configuration[{
DEFAULTS = {
# Where things are
"source" => Dir.pwd,
"destination" => File.join(Dir.pwd, "_site"),
@ -75,7 +75,7 @@ module Jekyll
"footnote_nr" => 1,
"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
# 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
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
def get_config_value_with_override(config_key, override)
@ -235,7 +235,9 @@ module Jekyll
# Ensure we have a hash.
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
config["collections"] = Utils.deep_merge_hashes(

View File

@ -112,9 +112,10 @@ module Jekyll
#
# Returns the Hash representation of this Convertible.
def to_liquid(attrs = nil)
further_data = Hash[(attrs || self.class::ATTRIBUTES_FOR_LIQUID).map do |attribute|
[attribute, send(attribute)]
end]
further_data = \
(attrs || self.class::ATTRIBUTES_FOR_LIQUID).each_with_object({}) do |attribute, hsh|
hsh[attribute] = send(attribute)
end
defaults = site.frontmatter_defaults.all(relative_path, type)
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.
def collections
@collections ||= Hash[collection_names.map do |coll|
[coll, Jekyll::Collection.new(self, coll)]
end]
@collections ||= collection_names.each_with_object({}) do |name, hsh|
hsh[name] = Jekyll::Collection.new(self, name)
end
end
# The list of collection names.