Skip to content

Commit

Permalink
Merge pull request #311 from Shopify/file-allocs
Browse files Browse the repository at this point in the history
Reduce allocations by freezing paths
  • Loading branch information
casperisfine committed Jun 29, 2020
2 parents 68faeab + 17ad3cf commit fc3acd5
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 16 deletions.
12 changes: 6 additions & 6 deletions lib/bootsnap/load_path_cache/cache.rb
Expand Up @@ -46,7 +46,7 @@ def load_dir(dir)
# loadpath.
def find(feature)
reinitialize if (@has_relative_paths && dir_changed?) || stale?
feature = feature.to_s
feature = feature.to_s.freeze
return feature if absolute_path?(feature)
return expand_path(feature) if feature.start_with?('./')
@mutex.synchronize do
Expand Down Expand Up @@ -178,25 +178,25 @@ def now

if DLEXT2
def search_index(f)
try_index(f + DOT_RB) || try_index(f + DLEXT) || try_index(f + DLEXT2) || try_index(f)
try_index("#{f}#{DOT_RB}") || try_index("#{f}#{DLEXT}") || try_index("#{f}#{DLEXT2}") || try_index(f)
end

def maybe_append_extension(f)
try_ext(f + DOT_RB) || try_ext(f + DLEXT) || try_ext(f + DLEXT2) || f
try_ext("#{f}#{DOT_RB}") || try_ext("#{f}#{DLEXT}") || try_ext("#{f}#{DLEXT2}") || f
end
else
def search_index(f)
try_index(f + DOT_RB) || try_index(f + DLEXT) || try_index(f)
try_index("#{f}#{DOT_RB}") || try_index("#{f}#{DLEXT}") || try_index(f)
end

def maybe_append_extension(f)
try_ext(f + DOT_RB) || try_ext(f + DLEXT) || f
try_ext("#{f}#{DOT_RB}") || try_ext("#{f}#{DLEXT}") || f
end
end

def try_index(f)
if (p = @index[f])
p + '/' + f
"#{p}/#{f}"
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/bootsnap/load_path_cache/core_ext/kernel_require.rb
Expand Up @@ -56,7 +56,7 @@ def load(path, wrap = false)
end

# load also allows relative paths from pwd even when not in $:
if File.exist?(relative = File.expand_path(path))
if File.exist?(relative = File.expand_path(path).freeze)
return load_without_bootsnap(relative, wrap)
end

Expand Down
2 changes: 1 addition & 1 deletion lib/bootsnap/load_path_cache/loaded_features_index.rb
Expand Up @@ -99,7 +99,7 @@ def register(short, long = nil)
altname = if extension_elidable?(short)
# Strip the extension off, e.g. 'bundler.rb' -> 'bundler'.
strip_extension_if_elidable(short)
elsif long && (ext = File.extname(long))
elsif long && (ext = File.extname(long.freeze))
# We already know the extension of the actual file this
# resolves to, so put that back on.
short + ext
Expand Down
4 changes: 2 additions & 2 deletions lib/bootsnap/load_path_cache/path.rb
Expand Up @@ -21,7 +21,7 @@ def volatile?
attr_reader(:path)

def initialize(path)
@path = path.to_s
@path = path.to_s.freeze
end

# True if the path exists, but represents a non-directory object
Expand Down Expand Up @@ -60,7 +60,7 @@ def entries_and_dirs(store)
end

def expanded_path
File.expand_path(path)
File.expand_path(path).freeze
end

private
Expand Down
3 changes: 2 additions & 1 deletion lib/bootsnap/load_path_cache/path_scanner.rb
Expand Up @@ -33,8 +33,9 @@ def self.call(path)
requirables = []

Dir.glob(path + ALL_FILES).each do |absolute_path|
absolute_path.freeze
next if contains_bundle_path && absolute_path.start_with?(BUNDLE_PATH)
relative_path = absolute_path.slice(relative_slice)
relative_path = absolute_path.slice(relative_slice).freeze

if File.directory?(absolute_path)
dirs << relative_path
Expand Down
10 changes: 5 additions & 5 deletions lib/bootsnap/load_path_cache/realpath_cache.rb
Expand Up @@ -15,15 +15,15 @@ def call(*key)

def realpath(caller_location, path)
base = File.dirname(caller_location)
file = find_file(File.expand_path(path, base))
dir = File.dirname(file)
File.join(dir, File.basename(file))
abspath = File.expand_path(path, base).freeze
find_file(abspath)
end

def find_file(name)
['', *CACHED_EXTENSIONS].each do |ext|
return File.realpath(name).freeze if File.exist?(name)
CACHED_EXTENSIONS.each do |ext|
filename = "#{name}#{ext}"
return File.realpath(filename) if File.exist?(filename)
return File.realpath(filename).freeze if File.exist?(filename)
end
name
end
Expand Down

0 comments on commit fc3acd5

Please sign in to comment.