ed-precompiled_bootsnap 1.18.6-arm64-darwin
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/CHANGELOG.md +401 -0
- data/LICENSE.txt +22 -0
- data/README.md +358 -0
- data/exe/bootsnap +5 -0
- data/ext/bootsnap/bootsnap.c +1135 -0
- data/ext/bootsnap/bootsnap.h +6 -0
- data/ext/bootsnap/extconf.rb +33 -0
- data/lib/bootsnap/3.0/bootsnap.bundle +0 -0
- data/lib/bootsnap/3.1/bootsnap.bundle +0 -0
- data/lib/bootsnap/3.2/bootsnap.bundle +0 -0
- data/lib/bootsnap/3.3/bootsnap.bundle +0 -0
- data/lib/bootsnap/3.4/bootsnap.bundle +0 -0
- data/lib/bootsnap/bootsnap_ext.rb +7 -0
- data/lib/bootsnap/bundler.rb +16 -0
- data/lib/bootsnap/cli/worker_pool.rb +208 -0
- data/lib/bootsnap/cli.rb +285 -0
- data/lib/bootsnap/compile_cache/iseq.rb +123 -0
- data/lib/bootsnap/compile_cache/json.rb +89 -0
- data/lib/bootsnap/compile_cache/yaml.rb +337 -0
- data/lib/bootsnap/compile_cache.rb +52 -0
- data/lib/bootsnap/explicit_require.rb +56 -0
- data/lib/bootsnap/load_path_cache/cache.rb +244 -0
- data/lib/bootsnap/load_path_cache/change_observer.rb +84 -0
- data/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb +37 -0
- data/lib/bootsnap/load_path_cache/core_ext/loaded_features.rb +19 -0
- data/lib/bootsnap/load_path_cache/loaded_features_index.rb +159 -0
- data/lib/bootsnap/load_path_cache/path.rb +136 -0
- data/lib/bootsnap/load_path_cache/path_scanner.rb +81 -0
- data/lib/bootsnap/load_path_cache/store.rb +132 -0
- data/lib/bootsnap/load_path_cache.rb +80 -0
- data/lib/bootsnap/setup.rb +5 -0
- data/lib/bootsnap/version.rb +5 -0
- data/lib/bootsnap.rb +164 -0
- metadata +95 -0
data/README.md
ADDED
@@ -0,0 +1,358 @@
|
|
1
|
+
# Bootsnap [](https://github.com/rails/bootsnap/actions)
|
2
|
+
|
3
|
+
Bootsnap is a library that plugs into Ruby, with optional support for `YAML` and `JSON`,
|
4
|
+
to optimize and cache expensive computations. See [How Does This Work](#how-does-this-work).
|
5
|
+
|
6
|
+
#### Performance
|
7
|
+
|
8
|
+
- [Discourse](https://github.com/discourse/discourse) reports a boot time reduction of approximately
|
9
|
+
50%, from roughly 6 to 3 seconds on one machine;
|
10
|
+
- One of our smaller internal apps also sees a reduction of 50%, from 3.6 to 1.8 seconds;
|
11
|
+
- The core Shopify platform -- a rather large monolithic application -- boots about 75% faster,
|
12
|
+
dropping from around 25s to 6.5s.
|
13
|
+
* In Shopify core (a large app), about 25% of this gain can be attributed to `compile_cache_*`
|
14
|
+
features; 75% to path caching. This is fairly representative.
|
15
|
+
|
16
|
+
## Usage
|
17
|
+
|
18
|
+
This gem works on macOS and Linux.
|
19
|
+
|
20
|
+
Add `bootsnap` to your `Gemfile`:
|
21
|
+
|
22
|
+
```ruby
|
23
|
+
gem 'bootsnap', require: false
|
24
|
+
```
|
25
|
+
|
26
|
+
If you are using Rails, add this to `config/boot.rb` immediately after `require 'bundler/setup'`:
|
27
|
+
|
28
|
+
```ruby
|
29
|
+
require 'bootsnap/setup'
|
30
|
+
```
|
31
|
+
|
32
|
+
Note that bootsnap writes to `tmp/cache` (or the path specified by `ENV['BOOTSNAP_CACHE_DIR']`),
|
33
|
+
and that directory *must* be writable. Rails will fail to
|
34
|
+
boot if it is not. If this is unacceptable (e.g. you are running in a read-only container and
|
35
|
+
unwilling to mount in a writable tmpdir), you should remove this line or wrap it in a conditional.
|
36
|
+
|
37
|
+
**Note also that bootsnap will never clean up its own cache: this is left up to you. Depending on your
|
38
|
+
deployment strategy, you may need to periodically purge `tmp/cache/bootsnap*`. If you notice deploys
|
39
|
+
getting progressively slower, this is almost certainly the cause.**
|
40
|
+
|
41
|
+
It's technically possible to simply specify `gem 'bootsnap', require: 'bootsnap/setup'`, but it's
|
42
|
+
important to load Bootsnap as early as possible to get maximum performance improvement.
|
43
|
+
|
44
|
+
You can see how this require works [here](https://github.com/rails/bootsnap/blob/main/lib/bootsnap/setup.rb).
|
45
|
+
|
46
|
+
If you are not using Rails, or if you are but want more control over things, add this to your
|
47
|
+
application setup immediately after `require 'bundler/setup'` (i.e. as early as possible: the sooner
|
48
|
+
this is loaded, the sooner it can start optimizing things)
|
49
|
+
|
50
|
+
```ruby
|
51
|
+
require 'bootsnap'
|
52
|
+
env = ENV['RAILS_ENV'] || "development"
|
53
|
+
Bootsnap.setup(
|
54
|
+
cache_dir: 'tmp/cache', # Path to your cache
|
55
|
+
ignore_directories: ['node_modules'], # Directory names to skip.
|
56
|
+
development_mode: env == 'development', # Current working environment, e.g. RACK_ENV, RAILS_ENV, etc
|
57
|
+
load_path_cache: true, # Optimize the LOAD_PATH with a cache
|
58
|
+
compile_cache_iseq: true, # Compile Ruby code into ISeq cache, breaks coverage reporting.
|
59
|
+
compile_cache_yaml: true, # Compile YAML into a cache
|
60
|
+
compile_cache_json: true, # Compile JSON into a cache
|
61
|
+
readonly: true, # Use the caches but don't update them on miss or stale entries.
|
62
|
+
)
|
63
|
+
```
|
64
|
+
|
65
|
+
**Protip:** You can replace `require 'bootsnap'` with `BootLib::Require.from_gem('bootsnap',
|
66
|
+
'bootsnap')` using [this trick](https://github.com/rails/bootsnap/wiki/Bootlib::Require). This
|
67
|
+
will help optimize boot time further if you have an extremely large `$LOAD_PATH`.
|
68
|
+
|
69
|
+
Note: Bootsnap and [Spring](https://github.com/rails/spring) are orthogonal tools. While Bootsnap
|
70
|
+
speeds up the loading of individual source files, Spring keeps a copy of a pre-booted Rails process
|
71
|
+
on hand to completely skip parts of the boot process the next time it's needed. The two tools work
|
72
|
+
well together.
|
73
|
+
|
74
|
+
### Environment variables
|
75
|
+
|
76
|
+
`require 'bootsnap/setup'` behavior can be changed using environment variables:
|
77
|
+
|
78
|
+
- `BOOTSNAP_CACHE_DIR` allows to define the cache location.
|
79
|
+
- `DISABLE_BOOTSNAP` allows to entirely disable bootsnap.
|
80
|
+
- `DISABLE_BOOTSNAP_LOAD_PATH_CACHE` allows to disable load path caching.
|
81
|
+
- `DISABLE_BOOTSNAP_COMPILE_CACHE` allows to disable ISeq and YAML caches.
|
82
|
+
- `BOOTSNAP_READONLY` configure bootsnap to not update the cache on miss or stale entries.
|
83
|
+
- `BOOTSNAP_LOG` configure bootsnap to log all caches misses to STDERR.
|
84
|
+
- `BOOTSNAP_STATS` log hit rate statistics on exit. Can't be used if `BOOTSNAP_LOG` is enabled.
|
85
|
+
- `BOOTSNAP_IGNORE_DIRECTORIES` a comma separated list of directories that shouldn't be scanned.
|
86
|
+
Useful when you have large directories of non-ruby files inside `$LOAD_PATH`.
|
87
|
+
It defaults to ignore any directory named `node_modules`.
|
88
|
+
|
89
|
+
### Environments
|
90
|
+
|
91
|
+
All Bootsnap features are enabled in development, test, production, and all other environments according to the configuration in the setup. At Shopify, we use this gem safely in all environments without issue.
|
92
|
+
|
93
|
+
If you would like to disable any feature for a certain environment, we suggest changing the configuration to take into account the appropriate ENV var or configuration according to your needs.
|
94
|
+
|
95
|
+
### Instrumentation
|
96
|
+
|
97
|
+
Bootsnap cache misses can be monitored though a callback:
|
98
|
+
|
99
|
+
```ruby
|
100
|
+
Bootsnap.instrumentation = ->(event, path) { puts "#{event} #{path}" }
|
101
|
+
```
|
102
|
+
|
103
|
+
`event` is either `:hit`, `:miss`, `:stale` or `:revalidated`.
|
104
|
+
You can also call `Bootsnap.log!` as a shortcut to log all events to STDERR.
|
105
|
+
|
106
|
+
To turn instrumentation back off you can set it to nil:
|
107
|
+
|
108
|
+
```ruby
|
109
|
+
Bootsnap.instrumentation = nil
|
110
|
+
```
|
111
|
+
|
112
|
+
## How does this work?
|
113
|
+
|
114
|
+
Bootsnap optimizes methods to cache results of expensive computations, and can be grouped
|
115
|
+
into two broad categories:
|
116
|
+
|
117
|
+
* [Path Pre-Scanning](#path-pre-scanning)
|
118
|
+
* `Kernel#require` and `Kernel#load` are modified to eliminate `$LOAD_PATH` scans.
|
119
|
+
* [Compilation caching](#compilation-caching)
|
120
|
+
* `RubyVM::InstructionSequence.load_iseq` is implemented to cache the result of ruby bytecode
|
121
|
+
compilation.
|
122
|
+
* `YAML.load_file` is modified to cache the result of loading a YAML object in MessagePack format
|
123
|
+
(or Marshal, if the message uses types unsupported by MessagePack).
|
124
|
+
* `JSON.load_file` is modified to cache the result of loading a JSON object in MessagePack format
|
125
|
+
|
126
|
+
### Path Pre-Scanning
|
127
|
+
|
128
|
+
*(This work is a minor evolution of [bootscale](https://github.com/byroot/bootscale)).*
|
129
|
+
|
130
|
+
Upon initialization of bootsnap or modification of the path (e.g. `$LOAD_PATH`),
|
131
|
+
`Bootsnap::LoadPathCache` will fetch a list of requirable entries from a cache, or, if necessary,
|
132
|
+
perform a full scan and cache the result.
|
133
|
+
|
134
|
+
Later, when we run (e.g.) `require 'foo'`, ruby *would* iterate through every item on our
|
135
|
+
`$LOAD_PATH` `['x', 'y', ...]`, looking for `x/foo.rb`, `y/foo.rb`, and so on. Bootsnap instead
|
136
|
+
looks at all the cached requirables for each `$LOAD_PATH` entry and substitutes the full expanded
|
137
|
+
path of the match ruby would have eventually chosen.
|
138
|
+
|
139
|
+
If you look at the syscalls generated by this behaviour, the net effect is that what would
|
140
|
+
previously look like this:
|
141
|
+
|
142
|
+
```
|
143
|
+
open x/foo.rb # (fail)
|
144
|
+
# (imagine this with 500 $LOAD_PATH entries instead of two)
|
145
|
+
open y/foo.rb # (success)
|
146
|
+
close y/foo.rb
|
147
|
+
open y/foo.rb
|
148
|
+
...
|
149
|
+
```
|
150
|
+
|
151
|
+
becomes this:
|
152
|
+
|
153
|
+
```
|
154
|
+
open y/foo.rb
|
155
|
+
...
|
156
|
+
```
|
157
|
+
|
158
|
+
The following diagram flowcharts the overrides that make the `*_path_cache` features work.
|
159
|
+
|
160
|
+

|
162
|
+
|
163
|
+
Bootsnap classifies path entries into two categories: stable and volatile. Volatile entries are
|
164
|
+
scanned each time the application boots, and their caches are only valid for 30 seconds. Stable
|
165
|
+
entries do not expire -- once their contents has been scanned, it is assumed to never change.
|
166
|
+
|
167
|
+
The only directories considered "stable" are things under the Ruby install prefix
|
168
|
+
(`RbConfig::CONFIG['prefix']`, e.g. `/usr/local/ruby` or `~/.rubies/x.y.z`), and things under the
|
169
|
+
`Gem.path` (e.g. `~/.gem/ruby/x.y.z`) or `Bundler.bundle_path`. Everything else is considered
|
170
|
+
"volatile".
|
171
|
+
|
172
|
+
In addition to the [`Bootsnap::LoadPathCache::Cache`
|
173
|
+
source](https://github.com/rails/bootsnap/blob/main/lib/bootsnap/load_path_cache/cache.rb),
|
174
|
+
this diagram may help clarify how entry resolution works:
|
175
|
+
|
176
|
+

|
177
|
+
|
178
|
+
|
179
|
+
It's also important to note how expensive `LoadError`s can be. If ruby invokes
|
180
|
+
`require 'something'`, but that file isn't on `$LOAD_PATH`, it takes `2 *
|
181
|
+
$LOAD_PATH.length` filesystem accesses to determine that. Bootsnap caches this
|
182
|
+
result too, raising a `LoadError` without touching the filesystem at all.
|
183
|
+
|
184
|
+
### Compilation Caching
|
185
|
+
|
186
|
+
*(A more readable implementation of this concept can be found in
|
187
|
+
[yomikomu](https://github.com/ko1/yomikomu)).*
|
188
|
+
|
189
|
+
Ruby has complex grammar and parsing it is not a particularly cheap operation. Since 1.9, Ruby has
|
190
|
+
translated ruby source to an internal bytecode format, which is then executed by the Ruby VM. Since
|
191
|
+
2.3.0, Ruby [exposes an API](https://docs.ruby-lang.org/en/master/RubyVM/InstructionSequence.html) that
|
192
|
+
allows caching that bytecode. This allows us to bypass the relatively-expensive compilation step on
|
193
|
+
subsequent loads of the same file.
|
194
|
+
|
195
|
+
We also noticed that we spend a lot of time loading YAML and JSON documents during our application boot, and
|
196
|
+
that MessagePack and Marshal are *much* faster at deserialization than YAML and JSON, even with a fast
|
197
|
+
implementation. We use the same strategy of compilation caching for YAML and JSON documents, with the
|
198
|
+
equivalent of Ruby's "bytecode" format being a MessagePack document (or, in the case of YAML
|
199
|
+
documents with types unsupported by MessagePack, a Marshal stream).
|
200
|
+
|
201
|
+
These compilation results are stored in a cache directory, with filenames generated by taking a hash
|
202
|
+
of the full expanded path of the input file (FNV1a-64).
|
203
|
+
|
204
|
+
Whereas before, the sequence of syscalls generated to `require` a file would look like:
|
205
|
+
|
206
|
+
```
|
207
|
+
open /c/foo.rb -> m
|
208
|
+
fstat64 m
|
209
|
+
close m
|
210
|
+
open /c/foo.rb -> o
|
211
|
+
fstat64 o
|
212
|
+
fstat64 o
|
213
|
+
read o
|
214
|
+
read o
|
215
|
+
...
|
216
|
+
close o
|
217
|
+
```
|
218
|
+
|
219
|
+
With bootsnap, we get:
|
220
|
+
|
221
|
+
```
|
222
|
+
open /c/foo.rb -> n
|
223
|
+
fstat64 n
|
224
|
+
close n
|
225
|
+
open /c/foo.rb -> n
|
226
|
+
fstat64 n
|
227
|
+
open (cache) -> m
|
228
|
+
read m
|
229
|
+
read m
|
230
|
+
close m
|
231
|
+
close n
|
232
|
+
```
|
233
|
+
|
234
|
+
This may look worse at a glance, but underlies a large performance difference.
|
235
|
+
|
236
|
+
*(The first three syscalls in both listings -- `open`, `fstat64`, `close` -- are not inherently
|
237
|
+
useful. [This ruby patch](https://bugs.ruby-lang.org/issues/13378) optimizes them out when coupled
|
238
|
+
with bootsnap.)*
|
239
|
+
|
240
|
+
Bootsnap writes a cache file containing a 64 byte header followed by the cache contents. The header
|
241
|
+
is a cache key including several fields:
|
242
|
+
|
243
|
+
* `version`, hardcoded in bootsnap. Essentially a schema version;
|
244
|
+
* `ruby_platform`, A hash of `RUBY_PLATFORM` (e.g. x86_64-linux-gnu) variable.
|
245
|
+
* `compile_option`, which changes with `RubyVM::InstructionSequence.compile_option` does;
|
246
|
+
* `ruby_revision`, A hash of `RUBY_REVISION`, the exact version of Ruby;
|
247
|
+
* `size`, the size of the source file;
|
248
|
+
* `mtime`, the last-modification timestamp of the source file when it was compiled; and
|
249
|
+
* `data_size`, the number of bytes following the header, which we need to read it into a buffer.
|
250
|
+
|
251
|
+
If the key is valid, the result is loaded from the value. Otherwise, it is regenerated and clobbers
|
252
|
+
the current cache.
|
253
|
+
|
254
|
+
### Putting it all together
|
255
|
+
|
256
|
+
Imagine we have this file structure:
|
257
|
+
|
258
|
+
```
|
259
|
+
/
|
260
|
+
├── a
|
261
|
+
├── b
|
262
|
+
└── c
|
263
|
+
└── foo.rb
|
264
|
+
```
|
265
|
+
|
266
|
+
And this `$LOAD_PATH`:
|
267
|
+
|
268
|
+
```
|
269
|
+
["/a", "/b", "/c"]
|
270
|
+
```
|
271
|
+
|
272
|
+
When we call `require 'foo'` without bootsnap, Ruby would generate this sequence of syscalls:
|
273
|
+
|
274
|
+
|
275
|
+
```
|
276
|
+
open /a/foo.rb -> -1
|
277
|
+
open /b/foo.rb -> -1
|
278
|
+
open /c/foo.rb -> n
|
279
|
+
close n
|
280
|
+
open /c/foo.rb -> m
|
281
|
+
fstat64 m
|
282
|
+
close m
|
283
|
+
open /c/foo.rb -> o
|
284
|
+
fstat64 o
|
285
|
+
fstat64 o
|
286
|
+
read o
|
287
|
+
read o
|
288
|
+
...
|
289
|
+
close o
|
290
|
+
```
|
291
|
+
|
292
|
+
With bootsnap, we get:
|
293
|
+
|
294
|
+
```
|
295
|
+
open /c/foo.rb -> n
|
296
|
+
fstat64 n
|
297
|
+
close n
|
298
|
+
open /c/foo.rb -> n
|
299
|
+
fstat64 n
|
300
|
+
open (cache) -> m
|
301
|
+
read m
|
302
|
+
read m
|
303
|
+
close m
|
304
|
+
close n
|
305
|
+
```
|
306
|
+
|
307
|
+
If we call `require 'nope'` without bootsnap, we get:
|
308
|
+
|
309
|
+
```
|
310
|
+
open /a/nope.rb -> -1
|
311
|
+
open /b/nope.rb -> -1
|
312
|
+
open /c/nope.rb -> -1
|
313
|
+
open /a/nope.bundle -> -1
|
314
|
+
open /b/nope.bundle -> -1
|
315
|
+
open /c/nope.bundle -> -1
|
316
|
+
```
|
317
|
+
|
318
|
+
...and if we call `require 'nope'` *with* bootsnap, we get...
|
319
|
+
|
320
|
+
```
|
321
|
+
# (nothing!)
|
322
|
+
```
|
323
|
+
|
324
|
+
## Precompilation
|
325
|
+
|
326
|
+
In development environments the bootsnap compilation cache is generated on the fly when source files are loaded.
|
327
|
+
But in production environments, such as docker images, you might need to precompile the cache.
|
328
|
+
|
329
|
+
To do so you can use the `bootsnap precompile` command.
|
330
|
+
|
331
|
+
Example:
|
332
|
+
|
333
|
+
```bash
|
334
|
+
$ bundle exec bootsnap precompile --gemfile app/ lib/ config/
|
335
|
+
```
|
336
|
+
|
337
|
+
## Known issues
|
338
|
+
|
339
|
+
### QEMU environments
|
340
|
+
|
341
|
+
When building cross-platform Docker images, QEMU is often used for emulation and can be the source of a limitation that causes forked processes to hang. While Bootsnap includes automatic detection for this issue (as of [PR #501](https://github.com/rails/bootsnap/pull/501)), the detection may not always be sufficient.
|
342
|
+
|
343
|
+
If you encounter hangs during precompilation in QEMU-based environments (such as when using Docker buildx for cross-platform builds), you can work around this by disabling parallelization with the `-j 0` option:
|
344
|
+
|
345
|
+
```bash
|
346
|
+
$ bundle exec bootsnap precompile -j 0 --gemfile app/ lib/ config/
|
347
|
+
```
|
348
|
+
|
349
|
+
See [Issue #495](https://github.com/rails/bootsnap/issues/495) for more details about this QEMU-related issue.
|
350
|
+
|
351
|
+
## When not to use Bootsnap
|
352
|
+
|
353
|
+
*Alternative engines*: Bootsnap is pretty reliant on MRI features, and parts are disabled entirely on alternative ruby
|
354
|
+
engines.
|
355
|
+
|
356
|
+
*Non-local filesystems*: Bootsnap depends on `tmp/cache` (or whatever you set its cache directory
|
357
|
+
to) being on a relatively fast filesystem. If you put it on a network mount, bootsnap is very likely
|
358
|
+
to slow your application down quite a lot.
|