Skip to content

Commit

Permalink
Segregate the various compile caches
Browse files Browse the repository at this point in the history
  • Loading branch information
byroot committed Jan 14, 2022
1 parent a95c258 commit d767c21
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 17 deletions.
6 changes: 3 additions & 3 deletions lib/bootsnap/cli.rb
Expand Up @@ -138,7 +138,7 @@ def precompile_yaml_files(load_paths, exclude: self.exclude)

def precompile_yaml(*yaml_files)
Array(yaml_files).each do |yaml_file|
if CompileCache::YAML.precompile(yaml_file, cache_dir: cache_dir)
if CompileCache::YAML.precompile(yaml_file)
STDERR.puts(yaml_file) if verbose
end
end
Expand All @@ -161,7 +161,7 @@ def precompile_json_files(load_paths, exclude: self.exclude)

def precompile_json(*json_files)
Array(json_files).each do |json_file|
if CompileCache::JSON.precompile(json_file, cache_dir: cache_dir)
if CompileCache::JSON.precompile(json_file)
STDERR.puts(json_file) if verbose
end
end
Expand All @@ -183,7 +183,7 @@ def precompile_ruby_files(load_paths, exclude: self.exclude)

def precompile_ruby(*ruby_files)
Array(ruby_files).each do |ruby_file|
if CompileCache::ISeq.precompile(ruby_file, cache_dir: cache_dir)
if CompileCache::ISeq.precompile(ruby_file)
STDERR.puts(ruby_file) if verbose
end
end
Expand Down
8 changes: 6 additions & 2 deletions lib/bootsnap/compile_cache/iseq.rb
Expand Up @@ -7,7 +7,11 @@ module Bootsnap
module CompileCache
module ISeq
class << self
attr_accessor(:cache_dir)
attr_reader(:cache_dir)

def cache_dir=(cache_dir)
@cache_dir = cache_dir.end_with?("/") ? "#{cache_dir}iseq" : "#{cache_dir}-iseq"
end
end

has_ruby_bug_18250 = begin # https://bugs.ruby-lang.org/issues/18250
Expand Down Expand Up @@ -61,7 +65,7 @@ def self.fetch(path, cache_dir: ISeq.cache_dir)
)
end

def self.precompile(path, cache_dir: ISeq.cache_dir)
def self.precompile(path)
Bootsnap::CompileCache::Native.precompile(
cache_dir,
path.to_s,
Expand Down
9 changes: 7 additions & 2 deletions lib/bootsnap/compile_cache/json.rb
Expand Up @@ -6,7 +6,12 @@ module Bootsnap
module CompileCache
module JSON
class << self
attr_accessor(:msgpack_factory, :cache_dir, :supported_options)
attr_accessor(:msgpack_factory, :supported_options)
attr_reader(:cache_dir)

def cache_dir=(cache_dir)
@cache_dir = cache_dir.end_with?("/") ? "#{cache_dir}json" : "#{cache_dir}-json"
end

def input_to_storage(payload, _)
obj = ::JSON.parse(payload)
Expand All @@ -24,7 +29,7 @@ def input_to_output(data, kwargs)
::JSON.parse(data, **(kwargs || {}))
end

def precompile(path, cache_dir: self.cache_dir)
def precompile(path)
Bootsnap::CompileCache::Native.precompile(
cache_dir,
path.to_s,
Expand Down
10 changes: 7 additions & 3 deletions lib/bootsnap/compile_cache/yaml.rb
Expand Up @@ -6,10 +6,14 @@ module Bootsnap
module CompileCache
module YAML
class << self
attr_accessor(:msgpack_factory, :cache_dir, :supported_options)
attr_reader(:implementation)
attr_accessor(:msgpack_factory, :supported_options)
attr_reader(:implementation, :cache_dir)

def precompile(path, cache_dir: YAML.cache_dir)
def cache_dir=(cache_dir)
@cache_dir = cache_dir.end_with?("/") ? "#{cache_dir}yaml" : "#{cache_dir}-yaml"
end

def precompile(path)
Bootsnap::CompileCache::Native.precompile(
cache_dir,
path.to_s,
Expand Down
10 changes: 5 additions & 5 deletions test/cli_test.rb
Expand Up @@ -14,7 +14,7 @@ def setup

def test_precompile_single_file
path = Help.set_file("a.rb", "a = a = 3", 100)
CompileCache::ISeq.expects(:precompile).with(File.expand_path(path), cache_dir: @cache_dir)
CompileCache::ISeq.expects(:precompile).with(File.expand_path(path))
assert_equal 0, CLI.new(["precompile", "-j", "0", path]).run
end

Expand All @@ -28,16 +28,16 @@ def test_precompile_directory
path_a = Help.set_file("foo/a.rb", "a = a = 3", 100)
path_b = Help.set_file("foo/b.rb", "b = b = 3", 100)

CompileCache::ISeq.expects(:precompile).with(File.expand_path(path_a), cache_dir: @cache_dir)
CompileCache::ISeq.expects(:precompile).with(File.expand_path(path_b), cache_dir: @cache_dir)
CompileCache::ISeq.expects(:precompile).with(File.expand_path(path_a))
CompileCache::ISeq.expects(:precompile).with(File.expand_path(path_b))
assert_equal 0, CLI.new(["precompile", "-j", "0", "foo"]).run
end

def test_precompile_exclude
path_a = Help.set_file("foo/a.rb", "a = a = 3", 100)
Help.set_file("foo/b.rb", "b = b = 3", 100)

CompileCache::ISeq.expects(:precompile).with(File.expand_path(path_a), cache_dir: @cache_dir)
CompileCache::ISeq.expects(:precompile).with(File.expand_path(path_a))
assert_equal 0, CLI.new(["precompile", "-j", "0", "--exclude", "b.rb", "foo"]).run
end

Expand All @@ -47,7 +47,7 @@ def test_precompile_gemfile

def test_precompile_yaml
path = Help.set_file("a.yaml", "foo: bar", 100)
CompileCache::YAML.expects(:precompile).with(File.expand_path(path), cache_dir: @cache_dir)
CompileCache::YAML.expects(:precompile).with(File.expand_path(path))
assert_equal 0, CLI.new(["precompile", "-j", "0", path]).run
end

Expand Down
2 changes: 1 addition & 1 deletion test/compile_cache_test.rb
Expand Up @@ -114,7 +114,7 @@ def test_recache_when_size_different

def test_invalid_cache_file
path = Help.set_file("a.rb", "a = a = 3", 100)
cp = Help.cache_path(@tmp_dir, path)
cp = Help.cache_path("#{@tmp_dir}-iseq", path)
FileUtils.mkdir_p(File.dirname(cp))
File.write(cp, "nope")
load(path)
Expand Down
2 changes: 1 addition & 1 deletion test/helper_test.rb
Expand Up @@ -7,7 +7,7 @@ class HelperTest < MiniTest::Test

def test_validate_cache_path
path = Help.set_file("a.rb", "a = a = 3", 100)
cp = Help.cache_path(@tmp_dir, path)
cp = Help.cache_path("#{@tmp_dir}-iseq", path)
load(path)
assert_equal(true, File.file?(cp))
end
Expand Down

0 comments on commit d767c21

Please sign in to comment.