From 17ad3cf27c3958a7611986247070a1de90439f89 Mon Sep 17 00:00:00 2001 From: Jean Boussier Date: Sun, 28 Jun 2020 23:20:27 +0200 Subject: [PATCH] Reduce allocations by freezing paths --- lib/bootsnap/load_path_cache/cache.rb | 12 ++++++------ .../load_path_cache/core_ext/kernel_require.rb | 2 +- .../load_path_cache/loaded_features_index.rb | 2 +- lib/bootsnap/load_path_cache/path.rb | 4 ++-- lib/bootsnap/load_path_cache/path_scanner.rb | 3 ++- lib/bootsnap/load_path_cache/realpath_cache.rb | 10 +++++----- 6 files changed, 17 insertions(+), 16 deletions(-) diff --git a/lib/bootsnap/load_path_cache/cache.rb b/lib/bootsnap/load_path_cache/cache.rb index 96d264c7..082f6c4a 100644 --- a/lib/bootsnap/load_path_cache/cache.rb +++ b/lib/bootsnap/load_path_cache/cache.rb @@ -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 @@ -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 diff --git a/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb b/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb index 30298126..7fbcc258 100644 --- a/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb +++ b/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb @@ -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 diff --git a/lib/bootsnap/load_path_cache/loaded_features_index.rb b/lib/bootsnap/load_path_cache/loaded_features_index.rb index 714b72da..f4c3f42a 100644 --- a/lib/bootsnap/load_path_cache/loaded_features_index.rb +++ b/lib/bootsnap/load_path_cache/loaded_features_index.rb @@ -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 diff --git a/lib/bootsnap/load_path_cache/path.rb b/lib/bootsnap/load_path_cache/path.rb index f93831e3..b2e13d3d 100644 --- a/lib/bootsnap/load_path_cache/path.rb +++ b/lib/bootsnap/load_path_cache/path.rb @@ -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 @@ -60,7 +60,7 @@ def entries_and_dirs(store) end def expanded_path - File.expand_path(path) + File.expand_path(path).freeze end private diff --git a/lib/bootsnap/load_path_cache/path_scanner.rb b/lib/bootsnap/load_path_cache/path_scanner.rb index 57ef11be..10992dc5 100644 --- a/lib/bootsnap/load_path_cache/path_scanner.rb +++ b/lib/bootsnap/load_path_cache/path_scanner.rb @@ -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 diff --git a/lib/bootsnap/load_path_cache/realpath_cache.rb b/lib/bootsnap/load_path_cache/realpath_cache.rb index f831e705..3c120cc1 100644 --- a/lib/bootsnap/load_path_cache/realpath_cache.rb +++ b/lib/bootsnap/load_path_cache/realpath_cache.rb @@ -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