From 65f88311685c0df5aa3a0ee0970d41b879fd3de8 Mon Sep 17 00:00:00 2001 From: Ashwin Maroli Date: Mon, 5 Aug 2019 01:38:54 +0530 Subject: [PATCH] Reduce allocations by using #each_with_object (#7758) Merge pull request 7758 --- lib/jekyll/configuration.rb | 10 ++++++---- lib/jekyll/convertible.rb | 7 ++++--- lib/jekyll/site.rb | 6 +++--- 3 files changed, 13 insertions(+), 10 deletions(-) diff --git a/lib/jekyll/configuration.rb b/lib/jekyll/configuration.rb index 2e4d609d..298a90fa 100644 --- a/lib/jekyll/configuration.rb +++ b/lib/jekyll/configuration.rb @@ -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( diff --git a/lib/jekyll/convertible.rb b/lib/jekyll/convertible.rb index 35f6b0f4..3847831f 100644 --- a/lib/jekyll/convertible.rb +++ b/lib/jekyll/convertible.rb @@ -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) diff --git a/lib/jekyll/site.rb b/lib/jekyll/site.rb index 50653e90..8aca2d65 100644 --- a/lib/jekyll/site.rb +++ b/lib/jekyll/site.rb @@ -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.