Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce allocations by freezing paths #311

Merged
merged 2 commits into from Jun 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 4 additions & 2 deletions .travis.yml
Expand Up @@ -6,8 +6,10 @@ os:
- osx

rvm:
- ruby-2.4
- ruby-2.5
- '2.4'
- '2.5'
- '2.6'
- '2.7'
- ruby-head

matrix:
Expand Down
6 changes: 5 additions & 1 deletion Rakefile
Expand Up @@ -10,4 +10,8 @@ Rake::ExtensionTask.new do |ext|
ext.gem_spec = gemspec
end

task(default: :compile)
task :test do
sh 'bin/testunit'
end

task(default: %i(compile test))
3 changes: 1 addition & 2 deletions bin/ci
Expand Up @@ -5,6 +5,5 @@ set -euxo pipefail
if [[ "${MINIMAL_SUPPORT-0}" -eq 1 ]]; then
exec bin/test-minimal-support
else
rake
exec bin/testunit
exec rake
fi
2 changes: 1 addition & 1 deletion bootsnap.gemspec
Expand Up @@ -37,7 +37,7 @@ Gem::Specification.new do |spec|
end

spec.add_development_dependency("bundler")
spec.add_development_dependency('rake', '~> 10.0')
spec.add_development_dependency('rake')
spec.add_development_dependency('rake-compiler', '~> 0')
spec.add_development_dependency("minitest", "~> 5.0")
spec.add_development_dependency("mocha", "~> 1.2")
Expand Down
2 changes: 1 addition & 1 deletion ext/bootsnap/bootsnap.c
Expand Up @@ -800,7 +800,7 @@ try_input_to_storage(VALUE arg)
}

static VALUE
rescue_input_to_storage(VALUE arg)
rescue_input_to_storage(VALUE arg, VALUE e)
{
return uncompilable;
}
Expand Down
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