Class: Bootsnap::LoadPathCache::LoadedFeaturesIndex
- Inherits:
-
Object
- Object
- Bootsnap::LoadPathCache::LoadedFeaturesIndex
- Defined in:
- lib/bootsnap/load_path_cache/loaded_features_index.rb
Overview
LoadedFeaturesIndex partially mirrors an internal structure in ruby that we can’t easily obtain an interface to.
This works around an issue where, without bootsnap, ruby knows that it has already required a file by its short name (e.g. require ‘bundler’) if a new instance of bundler is added to the $LOAD_PATH which resolves to a different absolute path. This class makes bootsnap smart enough to realize that it has already loaded ‘bundler’, and not just ‘/path/to/bundler’.
If you disable LoadedFeaturesIndex, you can see the problem this solves by:
-
‘require ’a’‘
-
Prepend a new $LOAD_PATH element containing an ‘a.rb`
-
‘require ’a’‘
Ruby returns false from step 3. With bootsnap but with no LoadedFeaturesIndex, this loads two different
`a.rb`s.
With bootsnap and with LoadedFeaturesIndex, this skips the second load,
returning false like ruby.
Instance Method Summary collapse
-
#initialize ⇒ LoadedFeaturesIndex
constructor
A new instance of LoadedFeaturesIndex.
- #key?(feature) ⇒ Boolean
-
#purge(feature) ⇒ Object
We’ve optimized for initialize and register to be fast, and purge to be tolerable.
- #purge_multi(features) ⇒ Object
-
#register(short, long = nil) ⇒ Object
There is a relatively uncommon case where we could miss adding an entry:.
Constructor Details
#initialize ⇒ LoadedFeaturesIndex
Returns a new instance of LoadedFeaturesIndex.
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/bootsnap/load_path_cache/loaded_features_index.rb', line 27 def initialize @lfi = {} @mutex = defined?(::Mutex) ? ::Mutex.new : ::Thread::Mutex.new # TODO: Remove once Ruby 2.2 support is dropped. # In theory the user could mutate $LOADED_FEATURES and invalidate our # cache. If this ever comes up in practice — or if you, the # enterprising reader, feels inclined to solve this problem — we could # parallel the work done with ChangeObserver on $LOAD_PATH to mirror # updates to our @lfi. $LOADED_FEATURES.each do |feat| hash = feat.hash $LOAD_PATH.each do |lpe| next unless feat.start_with?(lpe) # /a/b/lib/my/foo.rb # ^^^^^^^^^ short = feat[(lpe.length + 1)..-1] stripped = strip_extension_if_elidable(short) @lfi[short] = hash @lfi[stripped] = hash end end end |
Instance Method Details
#key?(feature) ⇒ Boolean
67 68 69 |
# File 'lib/bootsnap/load_path_cache/loaded_features_index.rb', line 67 def key?(feature) @mutex.synchronize { @lfi.key?(feature) } end |
#purge(feature) ⇒ Object
We’ve optimized for initialize and register to be fast, and purge to be tolerable. If access patterns make this not-okay, we can lazy-invert the LFI on first purge and work from there.
53 54 55 56 57 58 |
# File 'lib/bootsnap/load_path_cache/loaded_features_index.rb', line 53 def purge(feature) @mutex.synchronize do feat_hash = feature.hash @lfi.reject! { |_, hash| hash == feat_hash } end end |
#purge_multi(features) ⇒ Object
60 61 62 63 64 65 |
# File 'lib/bootsnap/load_path_cache/loaded_features_index.rb', line 60 def purge_multi(features) rejected_hashes = features.map(&:hash).to_set @mutex.synchronize do @lfi.reject! { |_, hash| rejected_hashes.include?(hash) } end end |
#register(short, long = nil) ⇒ Object
There is a relatively uncommon case where we could miss adding an entry:
If the user asked for e.g. ‘require ’bundler’‘, and we went through the `FallbackScan` pathway in `kernel_require.rb` and therefore did not pass `long` (the full expanded absolute path), then we did are not able to confidently add the `bundler.rb` form to @lfi.
We could either:
-
Just add ‘bundler.rb`, `bundler.so`, and so on, which is close but not quite right; or
-
Inspect $LOADED_FEATURES upon return from yield to find the matching entry.
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/bootsnap/load_path_cache/loaded_features_index.rb', line 85 def register(short, long = nil) if long.nil? pat = %r{/#{Regexp.escape(short)}(\.[^/]+)?$} len = $LOADED_FEATURES.size ret = yield long = $LOADED_FEATURES[len..-1].detect { |feat| feat =~ pat } else ret = yield end hash = long.hash # Do we have a filename with an elidable extension, e.g., # 'bundler.rb', or 'libgit2.so'? 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.freeze)) # We already know the extension of the actual file this # resolves to, so put that back on. short + ext end @mutex.synchronize do @lfi[short] = hash (@lfi[altname] = hash) if altname end ret end |