bootsnap 1.4.5 → 1.11.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +188 -0
- data/LICENSE.txt +1 -1
- data/README.md +48 -17
- data/exe/bootsnap +5 -0
- data/ext/bootsnap/bootsnap.c +297 -118
- data/ext/bootsnap/extconf.rb +22 -14
- data/lib/bootsnap/bundler.rb +2 -0
- data/lib/bootsnap/cli/worker_pool.rb +136 -0
- data/lib/bootsnap/cli.rb +281 -0
- data/lib/bootsnap/compile_cache/iseq.rb +63 -18
- data/lib/bootsnap/compile_cache/json.rb +88 -0
- data/lib/bootsnap/compile_cache/yaml.rb +331 -42
- data/lib/bootsnap/compile_cache.rb +22 -8
- data/lib/bootsnap/explicit_require.rb +5 -3
- data/lib/bootsnap/load_path_cache/cache.rb +75 -38
- data/lib/bootsnap/load_path_cache/change_observer.rb +6 -1
- data/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb +37 -64
- data/lib/bootsnap/load_path_cache/core_ext/loaded_features.rb +2 -0
- data/lib/bootsnap/load_path_cache/loaded_features_index.rb +62 -28
- data/lib/bootsnap/load_path_cache/path.rb +31 -6
- data/lib/bootsnap/load_path_cache/path_scanner.rb +54 -29
- data/lib/bootsnap/load_path_cache/store.rb +60 -16
- data/lib/bootsnap/load_path_cache.rb +17 -38
- data/lib/bootsnap/setup.rb +3 -36
- data/lib/bootsnap/version.rb +3 -1
- data/lib/bootsnap.rb +137 -36
- metadata +15 -99
- data/.github/CODEOWNERS +0 -2
- data/.github/probots.yml +0 -2
- data/.gitignore +0 -17
- data/.rubocop.yml +0 -20
- data/.travis.yml +0 -21
- data/CODE_OF_CONDUCT.md +0 -74
- data/CONTRIBUTING.md +0 -21
- data/Gemfile +0 -8
- data/README.jp.md +0 -231
- data/Rakefile +0 -12
- data/bin/ci +0 -10
- data/bin/console +0 -14
- data/bin/setup +0 -8
- data/bin/test-minimal-support +0 -7
- data/bin/testunit +0 -8
- data/bootsnap.gemspec +0 -45
- data/dev.yml +0 -10
- data/lib/bootsnap/load_path_cache/core_ext/active_support.rb +0 -106
- data/lib/bootsnap/load_path_cache/realpath_cache.rb +0 -32
- data/shipit.rubygems.yml +0 -0
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Bootsnap
|
2
4
|
module LoadPathCache
|
3
5
|
module ChangeObserver
|
@@ -14,18 +16,20 @@ module Bootsnap
|
|
14
16
|
@lpc_observer.push_paths(self, *entries.map(&:to_s))
|
15
17
|
super
|
16
18
|
end
|
19
|
+
alias_method :append, :push
|
17
20
|
|
18
21
|
def unshift(*entries)
|
19
22
|
@lpc_observer.unshift_paths(self, *entries.map(&:to_s))
|
20
23
|
super
|
21
24
|
end
|
25
|
+
alias_method :prepend, :unshift
|
22
26
|
|
23
27
|
def concat(entries)
|
24
28
|
@lpc_observer.push_paths(self, *entries.map(&:to_s))
|
25
29
|
super
|
26
30
|
end
|
27
31
|
|
28
|
-
# uniq! keeps the first
|
32
|
+
# uniq! keeps the first occurrence of each path, otherwise preserving
|
29
33
|
# order, preserving the effective load path
|
30
34
|
def uniq!(*args)
|
31
35
|
ret = super
|
@@ -54,6 +58,7 @@ module Bootsnap
|
|
54
58
|
|
55
59
|
def self.register(observer, arr)
|
56
60
|
return if arr.frozen? # can't register observer, but no need to.
|
61
|
+
|
57
62
|
arr.instance_variable_set(:@lpc_observer, observer)
|
58
63
|
arr.extend(ArrayMixin)
|
59
64
|
end
|
@@ -1,72 +1,45 @@
|
|
1
|
-
|
2
|
-
module LoadPathCache
|
3
|
-
module CoreExt
|
4
|
-
def self.make_load_error(path)
|
5
|
-
err = LoadError.new("cannot load such file -- #{path}")
|
6
|
-
err.instance_variable_set(Bootsnap::LoadPathCache::ERROR_TAG_IVAR, true)
|
7
|
-
err.define_singleton_method(:path) { path }
|
8
|
-
err
|
9
|
-
end
|
10
|
-
end
|
11
|
-
end
|
12
|
-
end
|
1
|
+
# frozen_string_literal: true
|
13
2
|
|
14
3
|
module Kernel
|
15
|
-
module_function
|
4
|
+
module_function
|
16
5
|
|
17
6
|
alias_method(:require_without_bootsnap, :require)
|
18
7
|
|
19
|
-
# Note that require registers to $LOADED_FEATURES while load does not.
|
20
|
-
def require_with_bootsnap_lfi(path, resolved = nil)
|
21
|
-
Bootsnap::LoadPathCache.loaded_features_index.register(path, resolved) do
|
22
|
-
require_without_bootsnap(resolved || path)
|
23
|
-
end
|
24
|
-
end
|
25
|
-
|
26
8
|
def require(path)
|
27
|
-
|
9
|
+
string_path = Bootsnap.rb_get_path(path)
|
10
|
+
return false if Bootsnap::LoadPathCache.loaded_features_index.key?(string_path)
|
28
11
|
|
29
|
-
|
30
|
-
|
12
|
+
resolved = Bootsnap::LoadPathCache.load_path_cache.find(string_path)
|
13
|
+
if Bootsnap::LoadPathCache::FALLBACK_SCAN.equal?(resolved)
|
14
|
+
if (cursor = Bootsnap::LoadPathCache.loaded_features_index.cursor(string_path))
|
15
|
+
ret = require_without_bootsnap(path)
|
16
|
+
resolved = Bootsnap::LoadPathCache.loaded_features_index.identify(string_path, cursor)
|
17
|
+
Bootsnap::LoadPathCache.loaded_features_index.register(string_path, resolved)
|
18
|
+
return ret
|
19
|
+
else
|
20
|
+
return require_without_bootsnap(path)
|
21
|
+
end
|
22
|
+
elsif false == resolved
|
23
|
+
return false
|
24
|
+
elsif resolved.nil?
|
25
|
+
error = LoadError.new(+"cannot load such file -- #{path}")
|
26
|
+
error.instance_variable_set(:@path, path)
|
27
|
+
raise error
|
28
|
+
else
|
29
|
+
# Note that require registers to $LOADED_FEATURES while load does not.
|
30
|
+
ret = require_without_bootsnap(resolved)
|
31
|
+
Bootsnap::LoadPathCache.loaded_features_index.register(string_path, resolved)
|
32
|
+
return ret
|
31
33
|
end
|
32
|
-
|
33
|
-
raise(Bootsnap::LoadPathCache::CoreExt.make_load_error(path))
|
34
|
-
rescue LoadError => e
|
35
|
-
e.instance_variable_set(Bootsnap::LoadPathCache::ERROR_TAG_IVAR, true)
|
36
|
-
raise(e)
|
37
|
-
rescue Bootsnap::LoadPathCache::ReturnFalse
|
38
|
-
false
|
39
|
-
rescue Bootsnap::LoadPathCache::FallbackScan
|
40
|
-
require_with_bootsnap_lfi(path)
|
41
|
-
end
|
42
|
-
|
43
|
-
alias_method(:require_relative_without_bootsnap, :require_relative)
|
44
|
-
def require_relative(path)
|
45
|
-
realpath = Bootsnap::LoadPathCache.realpath_cache.call(
|
46
|
-
caller_locations(1..1).first.absolute_path, path
|
47
|
-
)
|
48
|
-
require(realpath)
|
49
34
|
end
|
50
35
|
|
51
36
|
alias_method(:load_without_bootsnap, :load)
|
52
37
|
def load(path, wrap = false)
|
53
|
-
if (resolved = Bootsnap::LoadPathCache.load_path_cache.find(path))
|
54
|
-
|
38
|
+
if (resolved = Bootsnap::LoadPathCache.load_path_cache.find(Bootsnap.rb_get_path(path), try_extensions: false))
|
39
|
+
load_without_bootsnap(resolved, wrap)
|
40
|
+
else
|
41
|
+
load_without_bootsnap(path, wrap)
|
55
42
|
end
|
56
|
-
|
57
|
-
# load also allows relative paths from pwd even when not in $:
|
58
|
-
if File.exist?(relative = File.expand_path(path))
|
59
|
-
return load_without_bootsnap(relative, wrap)
|
60
|
-
end
|
61
|
-
|
62
|
-
raise(Bootsnap::LoadPathCache::CoreExt.make_load_error(path))
|
63
|
-
rescue LoadError => e
|
64
|
-
e.instance_variable_set(Bootsnap::LoadPathCache::ERROR_TAG_IVAR, true)
|
65
|
-
raise(e)
|
66
|
-
rescue Bootsnap::LoadPathCache::ReturnFalse
|
67
|
-
false
|
68
|
-
rescue Bootsnap::LoadPathCache::FallbackScan
|
69
|
-
load_without_bootsnap(path, wrap)
|
70
43
|
end
|
71
44
|
end
|
72
45
|
|
@@ -80,13 +53,13 @@ class Module
|
|
80
53
|
# The challenge is that we don't control the point at which the entry gets
|
81
54
|
# added to $LOADED_FEATURES and won't be able to hook that modification
|
82
55
|
# since it's done in C-land.
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
56
|
+
resolved = Bootsnap::LoadPathCache.load_path_cache.find(Bootsnap.rb_get_path(path))
|
57
|
+
if Bootsnap::LoadPathCache::FALLBACK_SCAN.equal?(resolved)
|
58
|
+
autoload_without_bootsnap(const, path)
|
59
|
+
elsif resolved == false
|
60
|
+
return false
|
61
|
+
else
|
62
|
+
autoload_without_bootsnap(const, resolved || path)
|
63
|
+
end
|
91
64
|
end
|
92
65
|
end
|
@@ -26,21 +26,22 @@ module Bootsnap
|
|
26
26
|
class LoadedFeaturesIndex
|
27
27
|
def initialize
|
28
28
|
@lfi = {}
|
29
|
-
@mutex =
|
29
|
+
@mutex = Mutex.new
|
30
30
|
|
31
31
|
# In theory the user could mutate $LOADED_FEATURES and invalidate our
|
32
|
-
# cache. If this ever comes up in practice
|
33
|
-
# enterprising reader, feels inclined to solve this problem
|
32
|
+
# cache. If this ever comes up in practice - or if you, the
|
33
|
+
# enterprising reader, feels inclined to solve this problem - we could
|
34
34
|
# parallel the work done with ChangeObserver on $LOAD_PATH to mirror
|
35
35
|
# updates to our @lfi.
|
36
36
|
$LOADED_FEATURES.each do |feat|
|
37
37
|
hash = feat.hash
|
38
38
|
$LOAD_PATH.each do |lpe|
|
39
39
|
next unless feat.start_with?(lpe)
|
40
|
+
|
40
41
|
# /a/b/lib/my/foo.rb
|
41
42
|
# ^^^^^^^^^
|
42
43
|
short = feat[(lpe.length + 1)..-1]
|
43
|
-
stripped =
|
44
|
+
stripped = strip_extension_if_elidable(short)
|
44
45
|
@lfi[short] = hash
|
45
46
|
@lfi[stripped] = hash
|
46
47
|
end
|
@@ -58,9 +59,9 @@ module Bootsnap
|
|
58
59
|
end
|
59
60
|
|
60
61
|
def purge_multi(features)
|
61
|
-
rejected_hashes = features.
|
62
|
+
rejected_hashes = features.each_with_object({}) { |f, h| h[f.hash] = true }
|
62
63
|
@mutex.synchronize do
|
63
|
-
@lfi.reject! { |_, hash| rejected_hashes.
|
64
|
+
@lfi.reject! { |_, hash| rejected_hashes.key?(hash) }
|
64
65
|
end
|
65
66
|
end
|
66
67
|
|
@@ -68,11 +69,30 @@ module Bootsnap
|
|
68
69
|
@mutex.synchronize { @lfi.key?(feature) }
|
69
70
|
end
|
70
71
|
|
72
|
+
def cursor(short)
|
73
|
+
unless Bootsnap.absolute_path?(short.to_s)
|
74
|
+
$LOADED_FEATURES.size
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
def identify(short, cursor)
|
79
|
+
$LOADED_FEATURES[cursor..-1].detect do |feat|
|
80
|
+
offset = 0
|
81
|
+
while (offset = feat.index(short, offset))
|
82
|
+
if feat.index(".", offset + 1) && !feat.index("/", offset + 2)
|
83
|
+
break true
|
84
|
+
else
|
85
|
+
offset += 1
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
71
91
|
# There is a relatively uncommon case where we could miss adding an
|
72
92
|
# entry:
|
73
93
|
#
|
74
94
|
# If the user asked for e.g. `require 'bundler'`, and we went through the
|
75
|
-
# `
|
95
|
+
# `FALLBACK_SCAN` pathway in `kernel_require.rb` and therefore did not
|
76
96
|
# pass `long` (the full expanded absolute path), then we did are not able
|
77
97
|
# to confidently add the `bundler.rb` form to @lfi.
|
78
98
|
#
|
@@ -82,25 +102,19 @@ module Bootsnap
|
|
82
102
|
# not quite right; or
|
83
103
|
# 2. Inspect $LOADED_FEATURES upon return from yield to find the matching
|
84
104
|
# entry.
|
85
|
-
def register(short, long
|
86
|
-
if
|
87
|
-
pat = %r{/#{Regexp.escape(short)}(\.[^/]+)?$}
|
88
|
-
len = $LOADED_FEATURES.size
|
89
|
-
ret = yield
|
90
|
-
long = $LOADED_FEATURES[len..-1].detect { |feat| feat =~ pat }
|
91
|
-
else
|
92
|
-
ret = yield
|
93
|
-
end
|
105
|
+
def register(short, long)
|
106
|
+
return if Bootsnap.absolute_path?(short)
|
94
107
|
|
95
108
|
hash = long.hash
|
96
109
|
|
97
|
-
#
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
#
|
110
|
+
# Do we have a filename with an elidable extension, e.g.,
|
111
|
+
# 'bundler.rb', or 'libgit2.so'?
|
112
|
+
altname = if extension_elidable?(short)
|
113
|
+
# Strip the extension off, e.g. 'bundler.rb' -> 'bundler'.
|
114
|
+
strip_extension_if_elidable(short)
|
115
|
+
elsif long && (ext = File.extname(long.freeze))
|
116
|
+
# We already know the extension of the actual file this
|
117
|
+
# resolves to, so put that back on.
|
104
118
|
short + ext
|
105
119
|
end
|
106
120
|
|
@@ -108,17 +122,37 @@ module Bootsnap
|
|
108
122
|
@lfi[short] = hash
|
109
123
|
(@lfi[altname] = hash) if altname
|
110
124
|
end
|
111
|
-
|
112
|
-
ret
|
113
125
|
end
|
114
126
|
|
115
127
|
private
|
116
128
|
|
117
|
-
STRIP_EXTENSION = /\.[^.]
|
129
|
+
STRIP_EXTENSION = /\.[^.]*?$/.freeze
|
118
130
|
private_constant(:STRIP_EXTENSION)
|
119
131
|
|
120
|
-
|
121
|
-
|
132
|
+
# Might Ruby automatically search for this extension if
|
133
|
+
# someone tries to 'require' the file without it? E.g. Ruby
|
134
|
+
# will implicitly try 'x.rb' if you ask for 'x'.
|
135
|
+
#
|
136
|
+
# This is complex and platform-dependent, and the Ruby docs are a little
|
137
|
+
# handwavy about what will be tried when and in what order.
|
138
|
+
# So optimistically pretend that all known elidable extensions
|
139
|
+
# will be tried on all platforms, and that people are unlikely
|
140
|
+
# to name files in a way that assumes otherwise.
|
141
|
+
# (E.g. It's unlikely that someone will know that their code
|
142
|
+
# will _never_ run on MacOS, and therefore think they can get away
|
143
|
+
# with calling a Ruby file 'x.dylib.rb' and then requiring it as 'x.dylib'.)
|
144
|
+
#
|
145
|
+
# See <https://ruby-doc.org/core-2.6.4/Kernel.html#method-i-require>.
|
146
|
+
def extension_elidable?(feature)
|
147
|
+
feature.to_s.end_with?(".rb", ".so", ".o", ".dll", ".dylib")
|
148
|
+
end
|
149
|
+
|
150
|
+
def strip_extension_if_elidable(feature)
|
151
|
+
if extension_elidable?(feature)
|
152
|
+
feature.sub(STRIP_EXTENSION, "")
|
153
|
+
else
|
154
|
+
feature
|
155
|
+
end
|
122
156
|
end
|
123
157
|
end
|
124
158
|
end
|
@@ -1,4 +1,6 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative("path_scanner")
|
2
4
|
|
3
5
|
module Bootsnap
|
4
6
|
module LoadPathCache
|
@@ -19,8 +21,26 @@ module Bootsnap
|
|
19
21
|
|
20
22
|
attr_reader(:path)
|
21
23
|
|
22
|
-
def initialize(path)
|
23
|
-
@path = path.to_s
|
24
|
+
def initialize(path, real: false)
|
25
|
+
@path = path.to_s.freeze
|
26
|
+
@real = real
|
27
|
+
end
|
28
|
+
|
29
|
+
def to_realpath
|
30
|
+
return self if @real
|
31
|
+
|
32
|
+
realpath = begin
|
33
|
+
File.realpath(path)
|
34
|
+
rescue Errno::ENOENT
|
35
|
+
return self
|
36
|
+
end
|
37
|
+
|
38
|
+
if realpath != path
|
39
|
+
Path.new(realpath, real: true)
|
40
|
+
else
|
41
|
+
@real = true
|
42
|
+
self
|
43
|
+
end
|
24
44
|
end
|
25
45
|
|
26
46
|
# True if the path exists, but represents a non-directory object
|
@@ -42,6 +62,7 @@ module Bootsnap
|
|
42
62
|
# set to zero anyway, just in case we change the stability heuristics.
|
43
63
|
_, entries, dirs = store.get(expanded_path)
|
44
64
|
return [entries, dirs] if entries # cache hit
|
65
|
+
|
45
66
|
entries, dirs = scan!
|
46
67
|
store.set(expanded_path, [0, entries, dirs])
|
47
68
|
return [entries, dirs]
|
@@ -59,7 +80,11 @@ module Bootsnap
|
|
59
80
|
end
|
60
81
|
|
61
82
|
def expanded_path
|
62
|
-
|
83
|
+
if @real
|
84
|
+
path
|
85
|
+
else
|
86
|
+
@expanded_path ||= File.expand_path(path).freeze
|
87
|
+
end
|
63
88
|
end
|
64
89
|
|
65
90
|
private
|
@@ -92,8 +117,8 @@ module Bootsnap
|
|
92
117
|
|
93
118
|
# Built-in ruby lib stuff doesn't change, but things can occasionally be
|
94
119
|
# installed into sitedir, which generally lives under libdir.
|
95
|
-
RUBY_LIBDIR = RbConfig::CONFIG[
|
96
|
-
RUBY_SITEDIR = RbConfig::CONFIG[
|
120
|
+
RUBY_LIBDIR = RbConfig::CONFIG["libdir"]
|
121
|
+
RUBY_SITEDIR = RbConfig::CONFIG["sitedir"]
|
97
122
|
|
98
123
|
def stability
|
99
124
|
@stability ||= begin
|
@@ -1,49 +1,74 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require_relative(
|
3
|
+
require_relative("../explicit_require")
|
4
4
|
|
5
5
|
module Bootsnap
|
6
6
|
module LoadPathCache
|
7
7
|
module PathScanner
|
8
|
-
ALL_FILES = "/{,**/*/**/}*"
|
9
8
|
REQUIRABLE_EXTENSIONS = [DOT_RB] + DL_EXTENSIONS
|
10
9
|
NORMALIZE_NATIVE_EXTENSIONS = !DL_EXTENSIONS.include?(LoadPathCache::DOT_SO)
|
11
|
-
ALTERNATIVE_NATIVE_EXTENSIONS_PATTERN = /\.(o|bundle|dylib)\z
|
10
|
+
ALTERNATIVE_NATIVE_EXTENSIONS_PATTERN = /\.(o|bundle|dylib)\z/.freeze
|
12
11
|
|
13
12
|
BUNDLE_PATH = if Bootsnap.bundler?
|
14
13
|
(Bundler.bundle_path.cleanpath.to_s << LoadPathCache::SLASH).freeze
|
15
14
|
else
|
16
|
-
|
15
|
+
""
|
17
16
|
end
|
18
17
|
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
18
|
+
class << self
|
19
|
+
def call(path)
|
20
|
+
path = File.expand_path(path.to_s).freeze
|
21
|
+
return [[], []] unless File.directory?(path)
|
22
|
+
|
23
|
+
# If the bundle path is a descendent of this path, we do additional
|
24
|
+
# checks to prevent recursing into the bundle path as we recurse
|
25
|
+
# through this path. We don't want to scan the bundle path because
|
26
|
+
# anything useful in it will be present on other load path items.
|
27
|
+
#
|
28
|
+
# This can happen if, for example, the user adds '.' to the load path,
|
29
|
+
# and the bundle path is '.bundle'.
|
30
|
+
contains_bundle_path = BUNDLE_PATH.start_with?(path)
|
31
|
+
|
32
|
+
dirs = []
|
33
|
+
requirables = []
|
34
|
+
walk(path, nil) do |relative_path, absolute_path, is_directory|
|
35
|
+
if is_directory
|
36
|
+
dirs << os_path(relative_path)
|
37
|
+
!contains_bundle_path || !absolute_path.start_with?(BUNDLE_PATH)
|
38
|
+
elsif relative_path.end_with?(*REQUIRABLE_EXTENSIONS)
|
39
|
+
requirables << os_path(relative_path)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
[requirables, dirs]
|
43
|
+
end
|
44
|
+
|
45
|
+
def walk(absolute_dir_path, relative_dir_path, &block)
|
46
|
+
Dir.foreach(absolute_dir_path) do |name|
|
47
|
+
next if name.start_with?(".")
|
48
|
+
|
49
|
+
relative_path = relative_dir_path ? File.join(relative_dir_path, name) : name
|
50
|
+
|
51
|
+
absolute_path = "#{absolute_dir_path}/#{name}"
|
52
|
+
if File.directory?(absolute_path)
|
53
|
+
if yield relative_path, absolute_path, true
|
54
|
+
walk(absolute_path, relative_path, &block)
|
55
|
+
end
|
56
|
+
else
|
57
|
+
yield relative_path, absolute_path, false
|
58
|
+
end
|
43
59
|
end
|
44
60
|
end
|
45
61
|
|
46
|
-
|
62
|
+
if RUBY_VERSION >= "3.1"
|
63
|
+
def os_path(path)
|
64
|
+
path.freeze
|
65
|
+
end
|
66
|
+
else
|
67
|
+
def os_path(path)
|
68
|
+
path.force_encoding(Encoding::US_ASCII) if path.ascii_only?
|
69
|
+
path.freeze
|
70
|
+
end
|
71
|
+
end
|
47
72
|
end
|
48
73
|
end
|
49
74
|
end
|
@@ -1,18 +1,21 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
|
3
|
+
require_relative("../explicit_require")
|
4
|
+
|
5
|
+
Bootsnap::ExplicitRequire.with_gems("msgpack") { require("msgpack") }
|
5
6
|
|
6
7
|
module Bootsnap
|
7
8
|
module LoadPathCache
|
8
9
|
class Store
|
10
|
+
VERSION_KEY = "__bootsnap_ruby_version__"
|
11
|
+
CURRENT_VERSION = "#{RUBY_REVISION}-#{RUBY_PLATFORM}".freeze # rubocop:disable Style/RedundantFreeze
|
12
|
+
|
9
13
|
NestedTransactionError = Class.new(StandardError)
|
10
14
|
SetOutsideTransactionNotAllowed = Class.new(StandardError)
|
11
15
|
|
12
16
|
def initialize(store_path)
|
13
17
|
@store_path = store_path
|
14
|
-
|
15
|
-
@txn_mutex = defined?(::Mutex) ? ::Mutex.new : ::Thread::Mutex.new
|
18
|
+
@txn_mutex = Mutex.new
|
16
19
|
@dirty = false
|
17
20
|
load_data
|
18
21
|
end
|
@@ -23,10 +26,11 @@ module Bootsnap
|
|
23
26
|
|
24
27
|
def fetch(key)
|
25
28
|
raise(SetOutsideTransactionNotAllowed) unless @txn_mutex.owned?
|
29
|
+
|
26
30
|
v = get(key)
|
27
31
|
unless v
|
28
|
-
@dirty = true
|
29
32
|
v = yield
|
33
|
+
mark_for_mutation!
|
30
34
|
@data[key] = v
|
31
35
|
end
|
32
36
|
v
|
@@ -34,14 +38,16 @@ module Bootsnap
|
|
34
38
|
|
35
39
|
def set(key, value)
|
36
40
|
raise(SetOutsideTransactionNotAllowed) unless @txn_mutex.owned?
|
41
|
+
|
37
42
|
if value != @data[key]
|
38
|
-
|
43
|
+
mark_for_mutation!
|
39
44
|
@data[key] = value
|
40
45
|
end
|
41
46
|
end
|
42
47
|
|
43
48
|
def transaction
|
44
49
|
raise(NestedTransactionError) if @txn_mutex.owned?
|
50
|
+
|
45
51
|
@txn_mutex.synchronize do
|
46
52
|
begin
|
47
53
|
yield
|
@@ -53,6 +59,11 @@ module Bootsnap
|
|
53
59
|
|
54
60
|
private
|
55
61
|
|
62
|
+
def mark_for_mutation!
|
63
|
+
@dirty = true
|
64
|
+
@data = @data.dup if @data.frozen?
|
65
|
+
end
|
66
|
+
|
56
67
|
def commit_transaction
|
57
68
|
if @dirty
|
58
69
|
dump_data
|
@@ -62,27 +73,60 @@ module Bootsnap
|
|
62
73
|
|
63
74
|
def load_data
|
64
75
|
@data = begin
|
65
|
-
|
66
|
-
|
76
|
+
data = File.open(@store_path, encoding: Encoding::BINARY) do |io|
|
77
|
+
MessagePack.load(io, freeze: true)
|
78
|
+
end
|
79
|
+
if data.is_a?(Hash) && data[VERSION_KEY] == CURRENT_VERSION
|
80
|
+
data
|
81
|
+
else
|
82
|
+
default_data
|
83
|
+
end
|
84
|
+
# handle malformed data due to upgrade incompatibility
|
67
85
|
rescue Errno::ENOENT, MessagePack::MalformedFormatError, MessagePack::UnknownExtTypeError, EOFError
|
68
|
-
|
69
|
-
rescue ArgumentError =>
|
70
|
-
|
86
|
+
default_data
|
87
|
+
rescue ArgumentError => error
|
88
|
+
if error.message =~ /negative array size/
|
89
|
+
default_data
|
90
|
+
else
|
91
|
+
raise
|
92
|
+
end
|
71
93
|
end
|
72
94
|
end
|
73
95
|
|
74
96
|
def dump_data
|
75
97
|
# Change contents atomically so other processes can't get invalid
|
76
98
|
# caches if they read at an inopportune time.
|
77
|
-
tmp = "#{@store_path}.#{Process.pid}.#{(rand *
|
78
|
-
|
99
|
+
tmp = "#{@store_path}.#{Process.pid}.#{(rand * 100_000).to_i}.tmp"
|
100
|
+
mkdir_p(File.dirname(tmp))
|
79
101
|
exclusive_write = File::Constants::CREAT | File::Constants::EXCL | File::Constants::WRONLY
|
80
102
|
# `encoding:` looks redundant wrt `binwrite`, but necessary on windows
|
81
103
|
# because binary is part of mode.
|
82
|
-
File.
|
83
|
-
|
104
|
+
File.open(tmp, mode: exclusive_write, encoding: Encoding::BINARY) do |io|
|
105
|
+
MessagePack.dump(@data, io)
|
106
|
+
end
|
107
|
+
File.rename(tmp, @store_path)
|
84
108
|
rescue Errno::EEXIST
|
85
109
|
retry
|
110
|
+
rescue SystemCallError
|
111
|
+
end
|
112
|
+
|
113
|
+
def default_data
|
114
|
+
{VERSION_KEY => CURRENT_VERSION}
|
115
|
+
end
|
116
|
+
|
117
|
+
def mkdir_p(path)
|
118
|
+
stack = []
|
119
|
+
until File.directory?(path)
|
120
|
+
stack.push path
|
121
|
+
path = File.dirname(path)
|
122
|
+
end
|
123
|
+
stack.reverse_each do |dir|
|
124
|
+
begin
|
125
|
+
Dir.mkdir(dir)
|
126
|
+
rescue SystemCallError
|
127
|
+
raise unless File.directory?(dir)
|
128
|
+
end
|
129
|
+
end
|
86
130
|
end
|
87
131
|
end
|
88
132
|
end
|