From cbfdeaefcd79821dbc266ccfd01e7e2cabd9082c Mon Sep 17 00:00:00 2001 From: Ashwin Maroli Date: Thu, 16 May 2019 19:34:37 +0530 Subject: [PATCH] Reduce string allocations with better alternatives (#7643) Merge pull request 7643 --- lib/jekyll/collection.rb | 6 ++++-- lib/jekyll/drops/drop.rb | 5 +++-- lib/jekyll/entry_filter.rb | 10 ++++++++-- lib/jekyll/frontmatter_defaults.rb | 6 +++--- lib/jekyll/utils/platforms.rb | 2 +- 5 files changed, 19 insertions(+), 10 deletions(-) diff --git a/lib/jekyll/collection.rb b/lib/jekyll/collection.rb index 41bbb70e..874d26f2 100644 --- a/lib/jekyll/collection.rb +++ b/lib/jekyll/collection.rb @@ -75,11 +75,13 @@ module Jekyll def entries return [] unless exists? - @entries ||= + @entries ||= begin + collection_dir_slash = "#{collection_dir}/" Utils.safe_glob(collection_dir, ["**", "*"], File::FNM_DOTMATCH).map do |entry| - entry["#{collection_dir}/"] = "" + entry[collection_dir_slash] = "" entry end + end end # Filtered version of the entries in this collection. diff --git a/lib/jekyll/drops/drop.rb b/lib/jekyll/drops/drop.rb index 90c6cfaf..c883bc0c 100644 --- a/lib/jekyll/drops/drop.rb +++ b/lib/jekyll/drops/drop.rb @@ -65,8 +65,9 @@ module Jekyll # and the key matches a method in which case it raises a # DropMutationException. def []=(key, val) - if respond_to?("#{key}=") - public_send("#{key}=", val) + setter = "#{key}=" + if respond_to?(setter) + public_send(setter, val) elsif respond_to?(key.to_s) if self.class.mutable? mutations[key] = val diff --git a/lib/jekyll/entry_filter.rb b/lib/jekyll/entry_filter.rb index 5f34cf82..fab08cbc 100644 --- a/lib/jekyll/entry_filter.rb +++ b/lib/jekyll/entry_filter.rb @@ -91,7 +91,7 @@ module Jekyll # Returns true if path matches against any glob pattern. # -- def glob_include?(enum, entry) - entry_path = Pathutil.new(site.in_source_dir).join(entry) + entry_path = source_path.join(entry) enum.any? do |exp| # Users who send a Regexp knows what they want to # exclude, so let them send a Regexp to exclude files, @@ -102,7 +102,7 @@ module Jekyll entry_path =~ exp else - item = Pathutil.new(site.in_source_dir).join(exp) + item = source_path.join(exp) # If it's a directory they want to exclude, AKA # ends with a "/" then we will go on to check and @@ -123,5 +123,11 @@ module Jekyll end end end + + private + + def source_path + @source_path ||= Pathutil.new(site.in_source_dir) + end end end diff --git a/lib/jekyll/frontmatter_defaults.rb b/lib/jekyll/frontmatter_defaults.rb index c3b73ef9..c099731f 100644 --- a/lib/jekyll/frontmatter_defaults.rb +++ b/lib/jekyll/frontmatter_defaults.rb @@ -111,7 +111,7 @@ module Jekyll if scope["path"].to_s.include?("*") glob_scope(sanitized_path, rel_scope_path) else - path_is_subpath?(sanitized_path, strip_collections_dir(rel_scope_path)) + path_is_subpath?(sanitized_path, strip_collections_dir(scope["path"])) end end @@ -120,7 +120,7 @@ module Jekyll abs_scope_path = site_source.join(rel_scope_path).to_s glob_cache(abs_scope_path).each do |scope_path| - scope_path = Pathname.new(scope_path).relative_path_from(site_source) + scope_path = Pathname.new(scope_path).relative_path_from(site_source).to_s scope_path = strip_collections_dir(scope_path) Jekyll.logger.debug "Globbed Scope Path:", scope_path return true if path_is_subpath?(sanitized_path, scope_path) @@ -143,7 +143,7 @@ module Jekyll def strip_collections_dir(path) collections_dir = @site.config["collections_dir"] - slashed_coll_dir = "#{collections_dir}/" + slashed_coll_dir = collections_dir.empty? ? "/" : "#{collections_dir}/" return path if collections_dir.empty? || !path.to_s.start_with?(slashed_coll_dir) path.sub(slashed_coll_dir, "") diff --git a/lib/jekyll/utils/platforms.rb b/lib/jekyll/utils/platforms.rb index 5265e4b2..cd0730be 100644 --- a/lib/jekyll/utils/platforms.rb +++ b/lib/jekyll/utils/platforms.rb @@ -72,7 +72,7 @@ module Jekyll def proc_version @proc_version ||= begin - Pathutil.new("/proc/version").read + File.read("/proc/version") rescue Errno::ENOENT, Errno::EACCES nil end