Reduce string allocations with better alternatives (#7643)

Merge pull request 7643
This commit is contained in:
Ashwin Maroli 2019-05-16 19:34:37 +05:30 committed by jekyllbot
parent 1658a1596e
commit cbfdeaefcd
5 changed files with 19 additions and 10 deletions

View File

@ -75,11 +75,13 @@ module Jekyll
def entries def entries
return [] unless exists? return [] unless exists?
@entries ||= @entries ||= begin
collection_dir_slash = "#{collection_dir}/"
Utils.safe_glob(collection_dir, ["**", "*"], File::FNM_DOTMATCH).map do |entry| Utils.safe_glob(collection_dir, ["**", "*"], File::FNM_DOTMATCH).map do |entry|
entry["#{collection_dir}/"] = "" entry[collection_dir_slash] = ""
entry entry
end end
end
end end
# Filtered version of the entries in this collection. # Filtered version of the entries in this collection.

View File

@ -65,8 +65,9 @@ module Jekyll
# and the key matches a method in which case it raises a # and the key matches a method in which case it raises a
# DropMutationException. # DropMutationException.
def []=(key, val) def []=(key, val)
if respond_to?("#{key}=") setter = "#{key}="
public_send("#{key}=", val) if respond_to?(setter)
public_send(setter, val)
elsif respond_to?(key.to_s) elsif respond_to?(key.to_s)
if self.class.mutable? if self.class.mutable?
mutations[key] = val mutations[key] = val

View File

@ -91,7 +91,7 @@ module Jekyll
# Returns true if path matches against any glob pattern. # Returns true if path matches against any glob pattern.
# -- # --
def glob_include?(enum, entry) 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| enum.any? do |exp|
# Users who send a Regexp knows what they want to # Users who send a Regexp knows what they want to
# exclude, so let them send a Regexp to exclude files, # exclude, so let them send a Regexp to exclude files,
@ -102,7 +102,7 @@ module Jekyll
entry_path =~ exp entry_path =~ exp
else 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 # If it's a directory they want to exclude, AKA
# ends with a "/" then we will go on to check and # ends with a "/" then we will go on to check and
@ -123,5 +123,11 @@ module Jekyll
end end
end end
end end
private
def source_path
@source_path ||= Pathutil.new(site.in_source_dir)
end
end end
end end

View File

@ -111,7 +111,7 @@ module Jekyll
if scope["path"].to_s.include?("*") if scope["path"].to_s.include?("*")
glob_scope(sanitized_path, rel_scope_path) glob_scope(sanitized_path, rel_scope_path)
else else
path_is_subpath?(sanitized_path, strip_collections_dir(rel_scope_path)) path_is_subpath?(sanitized_path, strip_collections_dir(scope["path"]))
end end
end end
@ -120,7 +120,7 @@ module Jekyll
abs_scope_path = site_source.join(rel_scope_path).to_s abs_scope_path = site_source.join(rel_scope_path).to_s
glob_cache(abs_scope_path).each do |scope_path| 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) scope_path = strip_collections_dir(scope_path)
Jekyll.logger.debug "Globbed Scope Path:", scope_path Jekyll.logger.debug "Globbed Scope Path:", scope_path
return true if path_is_subpath?(sanitized_path, scope_path) return true if path_is_subpath?(sanitized_path, scope_path)
@ -143,7 +143,7 @@ module Jekyll
def strip_collections_dir(path) def strip_collections_dir(path)
collections_dir = @site.config["collections_dir"] 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) return path if collections_dir.empty? || !path.to_s.start_with?(slashed_coll_dir)
path.sub(slashed_coll_dir, "") path.sub(slashed_coll_dir, "")

View File

@ -72,7 +72,7 @@ module Jekyll
def proc_version def proc_version
@proc_version ||= @proc_version ||=
begin begin
Pathutil.new("/proc/version").read File.read("/proc/version")
rescue Errno::ENOENT, Errno::EACCES rescue Errno::ENOENT, Errno::EACCES
nil nil
end end