From 754813294449f04abd44bb38d7a8a113d26e18a6 Mon Sep 17 00:00:00 2001 From: Ashwin Maroli Date: Thu, 16 May 2019 20:25:59 +0530 Subject: [PATCH] Reduce allocations from Jekyll::Document instances (#7625) Merge pull request 7625 --- lib/jekyll/document.rb | 43 ++++++++++++++++++++++++++++-------------- 1 file changed, 29 insertions(+), 14 deletions(-) diff --git a/lib/jekyll/document.rb b/lib/jekyll/document.rb index 7e9e42ca..38867b02 100644 --- a/lib/jekyll/document.rb +++ b/lib/jekyll/document.rb @@ -17,6 +17,20 @@ module Jekyll SASS_FILE_EXTS = %w(.sass .scss).freeze YAML_FILE_EXTS = %w(.yaml .yml).freeze + # + + # Class-wide cache to stash and retrieve regexp to detect "super-directories" + # of a particular Jekyll::Document object. + # + # dirname - The *special directory* for the Document. + # e.g. "_posts" or "_drafts" for Documents from the `site.posts` collection. + def self.superdirs_regex(dirname) + @superdirs_regex ||= {} + @superdirs_regex[dirname] ||= %r!#{dirname}.*! + end + + # + # Create a new Document. # # path - the path to the file @@ -404,28 +418,29 @@ module Jekyll # # Returns nothing. def categories_from_path(special_dir) - superdirs = relative_path.sub(%r!#{special_dir}(.*)!, "") - .split(File::SEPARATOR) - .reject do |c| - c.empty? || c == special_dir || c == basename - end + superdirs = relative_path.sub(Document.superdirs_regex(special_dir), "") + superdirs = superdirs.split(File::SEPARATOR) + superdirs.reject! { |c| c.empty? || c == special_dir || c == basename } + merge_data!({ "categories" => superdirs }, :source => "file path") end def populate_categories - merge_data!( - "categories" => ( - Array(data["categories"]) + Utils.pluralized_array_from_hash( - data, "category", "categories" - ) - ).map(&:to_s).flatten.uniq + categories = Array(data["categories"]) + Utils.pluralized_array_from_hash( + data, "category", "categories" ) + categories.map!(&:to_s) + categories.flatten! + categories.uniq! + + merge_data!("categories" => categories) end def populate_tags - merge_data!( - "tags" => Utils.pluralized_array_from_hash(data, "tag", "tags").flatten - ) + tags = Utils.pluralized_array_from_hash(data, "tag", "tags") + tags.flatten! + + merge_data!("tags" => tags) end private