bootsnap 1.4.9.rc1 → 1.7.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 63428d06eb0e8716ec1536736f8470e040811d59dbb111498961395c748d5290
4
- data.tar.gz: 757c007d11519164e8b47e35ae9c42f65209191062d0a420f288c130ac52b349
3
+ metadata.gz: ac5be912b9602b8b20f12255208a6a6d0373d64167092e3267b2494376f079f0
4
+ data.tar.gz: 5f18b74f0c0670b86d40d4f41df87869ce5f145cbc3d71b64eddcee59b63fe12
5
5
  SHA512:
6
- metadata.gz: 77ace9ac04ca94e5eca439bf64e0ace07b93349ba5f71c14a3d753ca3b131a6c8f0e1422ad70c9c2872e60698bcfda5343ae895bbdb01538036a185eb5d57752
7
- data.tar.gz: c18a585ba2fc19dc3f12844efba09ec20198736c00a9c08c7b3c7a9cb8c266445f9c03e3cb38f3785ecdee52e6f2eb1dc4947d46b6b98082d38fe7f0dac3098d
6
+ metadata.gz: e7abd18f5a495b63083d5b4ae9b105acc7f80c2813fa2c40594d2d34998037e6f433b91bf9ab24e021ca3d727368e5580972fc48b9382a86d30a45e03c8274f6
7
+ data.tar.gz: 87aaef6229c936321f8940a28f453446a75fd2b6935e13872c1aa7455990c579c1f3f1ebea3ec6416e9f1b3af18c0468d2a1cae2bc4fafb44f4a2c972e4f4688
data/CHANGELOG.md CHANGED
@@ -1,3 +1,34 @@
1
+ # Unreleased
2
+
3
+ # 1.7.0
4
+
5
+ * Fix detection of YAML files in gems.
6
+ * Adds an instrumentation API to monitor cache misses.
7
+ * Allow to control the behavior of `require 'bootsnap/setup'` using environment variables.
8
+ * Deprecate the `disable_trace` option.
9
+ * Deprecate the `ActiveSupport::Dependencies` (AKA Classic autoloader) integration. (#344)
10
+
11
+ # 1.6.0
12
+
13
+ * Fix a Ruby 2.7/3.0 issue with `YAML.load_file` keyword arguments. (#342)
14
+ * `bootsnap precompile` CLI use multiple processes to complete faster. (#341)
15
+ * `bootsnap precompile` CLI also precompile YAML files. (#340)
16
+ * Changed the load path cache directory from `$BOOTSNAP_CACHE_DIR/bootsnap-load-path-cache` to `$BOOTSNAP_CACHE_DIR/bootsnap/load-path-cache` for ease of use. (#334)
17
+ * Changed the compile cache directory from `$BOOTSNAP_CACHE_DIR/bootsnap-compile-cache` to `$BOOTSNAP_CACHE_DIR/bootsnap/compile-cache` for ease of use. (#334)
18
+
19
+ # 1.5.1
20
+
21
+ * Workaround a Ruby bug in InstructionSequence.compile_file. (#332)
22
+
23
+ # 1.5.0
24
+
25
+ * Add a command line to statically precompile the ISeq cache. (#326)
26
+
27
+ # 1.4.9
28
+
29
+ * [Windows support](https://github.com/Shopify/bootsnap/pull/319)
30
+ * [Fix potential crash](https://github.com/Shopify/bootsnap/pull/322)
31
+
1
32
  # 1.4.8
2
33
 
3
34
  * [Prevent FallbackScan from polluting exception cause](https://github.com/Shopify/bootsnap/pull/314)
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
- # Bootsnap [![Build Status](https://travis-ci.org/Shopify/bootsnap.svg?branch=master)](https://travis-ci.org/Shopify/bootsnap)
1
+ # Bootsnap [![Actions Status](https://github.com/Shopify/bootsnap/workflows/ci/badge.svg)](https://github.com/Shopify/bootsnap/actions)
2
2
 
3
- Bootsnap is a library that plugs into Ruby, with optional support for `ActiveSupport` and `YAML`,
3
+ Bootsnap is a library that plugs into Ruby, with optional support for `YAML`,
4
4
  to optimize and cache expensive computations. See [How Does This Work](#how-does-this-work).
5
5
 
6
6
  #### Performance
@@ -11,7 +11,7 @@ to optimize and cache expensive computations. See [How Does This Work](#how-does
11
11
  - The core Shopify platform -- a rather large monolithic application -- boots about 75% faster,
12
12
  dropping from around 25s to 6.5s.
13
13
  * In Shopify core (a large app), about 25% of this gain can be attributed to `compile_cache_*`
14
- features; 75% to path caching, and ~1% to `disable_trace`. This is fairly representative.
14
+ features; 75% to path caching. This is fairly representative.
15
15
 
16
16
  ## Usage
17
17
 
@@ -29,7 +29,8 @@ If you are using Rails, add this to `config/boot.rb` immediately after `require
29
29
  require 'bootsnap/setup'
30
30
  ```
31
31
 
32
- Note that bootsnap writes to `tmp/cache`, and that directory *must* be writable. Rails will fail to
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
33
34
  boot if it is not. If this is unacceptable (e.g. you are running in a read-only container and
34
35
  unwilling to mount in a writable tmpdir), you should remove this line or wrap it in a conditional.
35
36
 
@@ -53,15 +54,11 @@ Bootsnap.setup(
53
54
  cache_dir: 'tmp/cache', # Path to your cache
54
55
  development_mode: env == 'development', # Current working environment, e.g. RACK_ENV, RAILS_ENV, etc
55
56
  load_path_cache: true, # Optimize the LOAD_PATH with a cache
56
- autoload_paths_cache: true, # Optimize ActiveSupport autoloads with cache
57
- disable_trace: true, # Set `RubyVM::InstructionSequence.compile_option = { trace_instruction: false }`
58
57
  compile_cache_iseq: true, # Compile Ruby code into ISeq cache, breaks coverage reporting.
59
58
  compile_cache_yaml: true # Compile YAML into a cache
60
59
  )
61
60
  ```
62
61
 
63
- **Note that `disable_trace` will break debuggers and tracing.**
64
-
65
62
  **Protip:** You can replace `require 'bootsnap'` with `BootLib::Require.from_gem('bootsnap',
66
63
  'bootsnap')` using [this trick](https://github.com/Shopify/bootsnap/wiki/Bootlib::Require). This
67
64
  will help optimize boot time further if you have an extremely large `$LOAD_PATH`.
@@ -71,12 +68,39 @@ speeds up the loading of individual source files, Spring keeps a copy of a pre-b
71
68
  on hand to completely skip parts of the boot process the next time it's needed. The two tools work
72
69
  well together, and are both included in a newly-generated Rails applications by default.
73
70
 
71
+ ### Environment variables
72
+
73
+ `require 'bootsnap/setup'` behavior can be changed using environment variables:
74
+
75
+ - `BOOTSNAP_CACHE_DIR` allows to define the cache location.
76
+ - `DISABLE_BOOTSNAP` allows to entirely disable bootsnap.
77
+ - `DISABLE_BOOTSNAP_LOAD_PATH_CACHE` allows to disable load path caching.
78
+ - `DISABLE_BOOTSNAP_COMPILE_CACHE` allows to disable ISeq and YAML caches.
79
+ - `BOOTSNAP_LOG` configure bootsnap to log all caches misses to STDERR.
80
+
74
81
  ### Environments
75
82
 
76
83
  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.
77
84
 
78
85
  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.
79
86
 
87
+ ### Instrumentation
88
+
89
+ Bootsnap cache misses can be monitored though a callback:
90
+
91
+ ```ruby
92
+ Bootsnap.instrumentation = ->(event, path) { puts "#{event} #{path}" }
93
+ ```
94
+
95
+ `event` is either `:miss` or `:stale`. You can also call `Bootsnap.log!` as a shortcut to
96
+ log all events to STDERR.
97
+
98
+ To turn instrumentation back off you can set it to nil:
99
+
100
+ ```ruby
101
+ Bootsnap.instrumentation = nil
102
+ ```
103
+
80
104
  ## How does this work?
81
105
 
82
106
  Bootsnap optimizes methods to cache results of expensive computations, and can be grouped
@@ -84,8 +108,6 @@ into two broad categories:
84
108
 
85
109
  * [Path Pre-Scanning](#path-pre-scanning)
86
110
  * `Kernel#require` and `Kernel#load` are modified to eliminate `$LOAD_PATH` scans.
87
- * `ActiveSupport::Dependencies.{autoloadable_module?,load_missing_constant,depend_on}` are
88
- overridden to eliminate scans of `ActiveSupport::Dependencies.autoload_paths`.
89
111
  * [Compilation caching](#compilation-caching)
90
112
  * `RubyVM::InstructionSequence.load_iseq` is implemented to cache the result of ruby bytecode
91
113
  compilation.
@@ -124,10 +146,6 @@ open y/foo.rb
124
146
  ...
125
147
  ```
126
148
 
127
- Exactly the same strategy is employed for methods that traverse
128
- `ActiveSupport::Dependencies.autoload_paths` if the `autoload_paths_cache` option is given to
129
- `Bootsnap.setup`.
130
-
131
149
  The following diagram flowcharts the overrides that make the `*_path_cache` features work.
132
150
 
133
151
  ![Flowchart explaining
@@ -294,6 +312,19 @@ open /c/nope.bundle -> -1
294
312
  # (nothing!)
295
313
  ```
296
314
 
315
+ ## Precompilation
316
+
317
+ In development environments the bootsnap compilation cache is generated on the fly when source files are loaded.
318
+ But in production environments, such as docker images, you might need to precompile the cache.
319
+
320
+ To do so you can use the `bootsnap precompile` command.
321
+
322
+ Example:
323
+
324
+ ```bash
325
+ $ bundle exec bootsnap precompile --gemfile app/ lib/
326
+ ```
327
+
297
328
  ## When not to use Bootsnap
298
329
 
299
330
  *Alternative engines*: Bootsnap is pretty reliant on MRI features, and parts are disabled entirely on alternative ruby
data/exe/bootsnap ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bootsnap/cli'
5
+ exit Bootsnap::CLI.new(ARGV).run
@@ -14,6 +14,7 @@
14
14
  #include "bootsnap.h"
15
15
  #include "ruby.h"
16
16
  #include <stdint.h>
17
+ #include <stdbool.h>
17
18
  #include <sys/types.h>
18
19
  #include <errno.h>
19
20
  #include <fcntl.h>
@@ -34,6 +35,10 @@
34
35
 
35
36
  #define MAX_CREATE_TEMPFILE_ATTEMPT 3
36
37
 
38
+ #ifndef RB_UNLIKELY
39
+ #define RB_UNLIKELY(x) (x)
40
+ #endif
41
+
37
42
  /*
38
43
  * An instance of this key is written as the first 64 bytes of each cache file.
39
44
  * The mtime and size members track whether the file contents have changed, and
@@ -70,7 +75,7 @@ struct bs_cache_key {
70
75
  STATIC_ASSERT(sizeof(struct bs_cache_key) == KEY_SIZE);
71
76
 
72
77
  /* Effectively a schema version. Bumping invalidates all previous caches */
73
- static const uint32_t current_version = 2;
78
+ static const uint32_t current_version = 3;
74
79
 
75
80
  /* hash of e.g. "x86_64-darwin17", invalidating when ruby is recompiled on a
76
81
  * new OS ABI, etc. */
@@ -88,17 +93,24 @@ static VALUE rb_mBootsnap_CompileCache;
88
93
  static VALUE rb_mBootsnap_CompileCache_Native;
89
94
  static VALUE rb_eBootsnap_CompileCache_Uncompilable;
90
95
  static ID uncompilable;
96
+ static ID instrumentation_method;
97
+ static VALUE sym_miss;
98
+ static VALUE sym_stale;
99
+ static bool instrumentation_enabled = false;
91
100
 
92
101
  /* Functions exposed as module functions on Bootsnap::CompileCache::Native */
102
+ static VALUE bs_instrumentation_enabled_set(VALUE self, VALUE enabled);
93
103
  static VALUE bs_compile_option_crc32_set(VALUE self, VALUE crc32_v);
94
104
  static VALUE bs_rb_fetch(VALUE self, VALUE cachedir_v, VALUE path_v, VALUE handler, VALUE args);
105
+ static VALUE bs_rb_precompile(VALUE self, VALUE cachedir_v, VALUE path_v, VALUE handler);
95
106
 
96
107
  /* Helpers */
97
108
  static uint64_t fnv1a_64(const char *str);
98
- static void bs_cache_path(const char * cachedir, const char * path, const char * extra, char (* cache_path)[MAX_CACHEPATH_SIZE]);
109
+ static void bs_cache_path(const char * cachedir, const char * path, char (* cache_path)[MAX_CACHEPATH_SIZE]);
99
110
  static int bs_read_key(int fd, struct bs_cache_key * key);
100
111
  static int cache_key_equal(struct bs_cache_key * k1, struct bs_cache_key * k2);
101
112
  static VALUE bs_fetch(char * path, VALUE path_v, char * cache_path, VALUE handler, VALUE args);
113
+ static VALUE bs_precompile(char * path, VALUE path_v, char * cache_path, VALUE handler);
102
114
  static int open_current_file(char * path, struct bs_cache_key * key, const char ** errno_provenance);
103
115
  static int fetch_cached_data(int fd, ssize_t data_size, VALUE handler, VALUE args, VALUE * output_data, int * exception_tag, const char ** errno_provenance);
104
116
  static uint32_t get_ruby_revision(void);
@@ -146,15 +158,31 @@ Init_bootsnap(void)
146
158
  current_ruby_platform = get_ruby_platform();
147
159
 
148
160
  uncompilable = rb_intern("__bootsnap_uncompilable__");
161
+ instrumentation_method = rb_intern("_instrument");
162
+
163
+ sym_miss = ID2SYM(rb_intern("miss"));
164
+ rb_global_variable(&sym_miss);
149
165
 
166
+ sym_stale = ID2SYM(rb_intern("stale"));
167
+ rb_global_variable(&sym_stale);
168
+
169
+ rb_define_module_function(rb_mBootsnap, "instrumentation_enabled=", bs_instrumentation_enabled_set, 1);
150
170
  rb_define_module_function(rb_mBootsnap_CompileCache_Native, "coverage_running?", bs_rb_coverage_running, 0);
151
171
  rb_define_module_function(rb_mBootsnap_CompileCache_Native, "fetch", bs_rb_fetch, 4);
172
+ rb_define_module_function(rb_mBootsnap_CompileCache_Native, "precompile", bs_rb_precompile, 3);
152
173
  rb_define_module_function(rb_mBootsnap_CompileCache_Native, "compile_option_crc32=", bs_compile_option_crc32_set, 1);
153
174
 
154
175
  current_umask = umask(0777);
155
176
  umask(current_umask);
156
177
  }
157
178
 
179
+ static VALUE
180
+ bs_instrumentation_enabled_set(VALUE self, VALUE enabled)
181
+ {
182
+ instrumentation_enabled = RTEST(enabled);
183
+ return enabled;
184
+ }
185
+
158
186
  /*
159
187
  * Bootsnap's ruby code registers a hook that notifies us via this function
160
188
  * when compile_option changes. These changes invalidate all existing caches.
@@ -264,13 +292,9 @@ get_ruby_platform(void)
264
292
  * The path will look something like: <cachedir>/12/34567890abcdef
265
293
  */
266
294
  static void
267
- bs_cache_path(const char * cachedir, const char * path, const char * extra, char (* cache_path)[MAX_CACHEPATH_SIZE])
295
+ bs_cache_path(const char * cachedir, const char * path, char (* cache_path)[MAX_CACHEPATH_SIZE])
268
296
  {
269
297
  uint64_t hash = fnv1a_64(path);
270
- if (extra) {
271
- hash ^= fnv1a_64(extra);
272
- }
273
-
274
298
  uint8_t first_byte = (hash >> (64 - 8));
275
299
  uint64_t remainder = hash & 0x00ffffffffffffff;
276
300
 
@@ -318,18 +342,39 @@ bs_rb_fetch(VALUE self, VALUE cachedir_v, VALUE path_v, VALUE handler, VALUE arg
318
342
  char * cachedir = RSTRING_PTR(cachedir_v);
319
343
  char * path = RSTRING_PTR(path_v);
320
344
  char cache_path[MAX_CACHEPATH_SIZE];
321
- char * extra = NULL;
322
- if (!NIL_P(args)) {
323
- VALUE args_serial = rb_marshal_dump(args, Qnil);
324
- extra = RSTRING_PTR(args_serial);
325
- }
326
345
 
327
346
  /* generate cache path to cache_path */
328
- bs_cache_path(cachedir, path, extra, &cache_path);
347
+ bs_cache_path(cachedir, path, &cache_path);
329
348
 
330
349
  return bs_fetch(path, path_v, cache_path, handler, args);
331
350
  }
332
351
 
352
+ /*
353
+ * Entrypoint for Bootsnap::CompileCache::Native.precompile.
354
+ * Similar to fetch, but it only generate the cache if missing
355
+ * and doesn't return the content.
356
+ */
357
+ static VALUE
358
+ bs_rb_precompile(VALUE self, VALUE cachedir_v, VALUE path_v, VALUE handler)
359
+ {
360
+ FilePathValue(path_v);
361
+
362
+ Check_Type(cachedir_v, T_STRING);
363
+ Check_Type(path_v, T_STRING);
364
+
365
+ if (RSTRING_LEN(cachedir_v) > MAX_CACHEDIR_SIZE) {
366
+ rb_raise(rb_eArgError, "cachedir too long");
367
+ }
368
+
369
+ char * cachedir = RSTRING_PTR(cachedir_v);
370
+ char * path = RSTRING_PTR(path_v);
371
+ char cache_path[MAX_CACHEPATH_SIZE];
372
+
373
+ /* generate cache path to cache_path */
374
+ bs_cache_path(cachedir, path, &cache_path);
375
+
376
+ return bs_precompile(path, path_v, cache_path, handler);
377
+ }
333
378
  /*
334
379
  * Open the file we want to load/cache and generate a cache key for it if it
335
380
  * was loaded.
@@ -366,7 +411,8 @@ open_current_file(char * path, struct bs_cache_key * key, const char ** errno_pr
366
411
  }
367
412
 
368
413
  #define ERROR_WITH_ERRNO -1
369
- #define CACHE_MISSING_OR_INVALID -2
414
+ #define CACHE_MISS -2
415
+ #define CACHE_STALE -3
370
416
 
371
417
  /*
372
418
  * Read the cache key from the given fd, which must have position 0 (e.g.
@@ -374,15 +420,16 @@ open_current_file(char * path, struct bs_cache_key * key, const char ** errno_pr
374
420
  *
375
421
  * Possible return values:
376
422
  * - 0 (OK, key was loaded)
377
- * - CACHE_MISSING_OR_INVALID (-2)
378
423
  * - ERROR_WITH_ERRNO (-1, errno is set)
424
+ * - CACHE_MISS (-2)
425
+ * - CACHE_STALE (-3)
379
426
  */
380
427
  static int
381
428
  bs_read_key(int fd, struct bs_cache_key * key)
382
429
  {
383
430
  ssize_t nread = read(fd, key, KEY_SIZE);
384
431
  if (nread < 0) return ERROR_WITH_ERRNO;
385
- if (nread < KEY_SIZE) return CACHE_MISSING_OR_INVALID;
432
+ if (nread < KEY_SIZE) return CACHE_STALE;
386
433
  return 0;
387
434
  }
388
435
 
@@ -392,7 +439,8 @@ bs_read_key(int fd, struct bs_cache_key * key)
392
439
  *
393
440
  * Possible return values:
394
441
  * - 0 (OK, key was loaded)
395
- * - CACHE_MISSING_OR_INVALID (-2)
442
+ * - CACHE_MISS (-2)
443
+ * - CACHE_STALE (-3)
396
444
  * - ERROR_WITH_ERRNO (-1, errno is set)
397
445
  */
398
446
  static int
@@ -403,7 +451,7 @@ open_cache_file(const char * path, struct bs_cache_key * key, const char ** errn
403
451
  fd = open(path, O_RDONLY);
404
452
  if (fd < 0) {
405
453
  *errno_provenance = "bs_fetch:open_cache_file:open";
406
- if (errno == ENOENT) return CACHE_MISSING_OR_INVALID;
454
+ if (errno == ENOENT) return CACHE_MISS;
407
455
  return ERROR_WITH_ERRNO;
408
456
  }
409
457
  #ifdef _WIN32
@@ -458,7 +506,7 @@ fetch_cached_data(int fd, ssize_t data_size, VALUE handler, VALUE args, VALUE *
458
506
  goto done;
459
507
  }
460
508
  if (nread != data_size) {
461
- ret = CACHE_MISSING_OR_INVALID;
509
+ ret = CACHE_STALE;
462
510
  goto done;
463
511
  }
464
512
 
@@ -652,14 +700,22 @@ bs_fetch(char * path, VALUE path_v, char * cache_path, VALUE handler, VALUE args
652
700
 
653
701
  /* Open the cache key if it exists, and read its cache key in */
654
702
  cache_fd = open_cache_file(cache_path, &cached_key, &errno_provenance);
655
- if (cache_fd == CACHE_MISSING_OR_INVALID) {
703
+ if (cache_fd == CACHE_MISS || cache_fd == CACHE_STALE) {
656
704
  /* This is ok: valid_cache remains false, we re-populate it. */
705
+ if (RB_UNLIKELY(instrumentation_enabled)) {
706
+ rb_funcall(rb_mBootsnap, instrumentation_method, 2, cache_fd == CACHE_MISS ? sym_miss : sym_stale, path_v);
707
+ }
657
708
  } else if (cache_fd < 0) {
658
709
  goto fail_errno;
659
710
  } else {
660
711
  /* True if the cache existed and no invalidating changes have occurred since
661
712
  * it was generated. */
662
713
  valid_cache = cache_key_equal(&current_key, &cached_key);
714
+ if (RB_UNLIKELY(instrumentation_enabled)) {
715
+ if (!valid_cache) {
716
+ rb_funcall(rb_mBootsnap, instrumentation_method, 2, sym_stale, path_v);
717
+ }
718
+ }
663
719
  }
664
720
 
665
721
  if (valid_cache) {
@@ -668,10 +724,10 @@ bs_fetch(char * path, VALUE path_v, char * cache_path, VALUE handler, VALUE args
668
724
  cache_fd, (ssize_t)cached_key.data_size, handler, args,
669
725
  &output_data, &exception_tag, &errno_provenance
670
726
  );
671
- if (exception_tag != 0) goto raise;
672
- else if (res == CACHE_MISSING_OR_INVALID) valid_cache = 0;
673
- else if (res == ERROR_WITH_ERRNO) goto fail_errno;
674
- else if (!NIL_P(output_data)) goto succeed; /* fast-path, goal */
727
+ if (exception_tag != 0) goto raise;
728
+ else if (res == CACHE_MISS || res == CACHE_STALE) valid_cache = 0;
729
+ else if (res == ERROR_WITH_ERRNO) goto fail_errno;
730
+ else if (!NIL_P(output_data)) goto succeed; /* fast-path, goal */
675
731
  }
676
732
  close(cache_fd);
677
733
  cache_fd = -1;
@@ -740,6 +796,79 @@ invalid_type_storage_data:
740
796
  #undef CLEANUP
741
797
  }
742
798
 
799
+ static VALUE
800
+ bs_precompile(char * path, VALUE path_v, char * cache_path, VALUE handler)
801
+ {
802
+ struct bs_cache_key cached_key, current_key;
803
+ char * contents = NULL;
804
+ int cache_fd = -1, current_fd = -1;
805
+ int res, valid_cache = 0, exception_tag = 0;
806
+ const char * errno_provenance = NULL;
807
+
808
+ VALUE input_data; /* data read from source file, e.g. YAML or ruby source */
809
+ VALUE storage_data; /* compiled data, e.g. msgpack / binary iseq */
810
+
811
+ /* Open the source file and generate a cache key for it */
812
+ current_fd = open_current_file(path, &current_key, &errno_provenance);
813
+ if (current_fd < 0) goto fail;
814
+
815
+ /* Open the cache key if it exists, and read its cache key in */
816
+ cache_fd = open_cache_file(cache_path, &cached_key, &errno_provenance);
817
+ if (cache_fd == CACHE_MISS || cache_fd == CACHE_STALE) {
818
+ /* This is ok: valid_cache remains false, we re-populate it. */
819
+ } else if (cache_fd < 0) {
820
+ goto fail;
821
+ } else {
822
+ /* True if the cache existed and no invalidating changes have occurred since
823
+ * it was generated. */
824
+ valid_cache = cache_key_equal(&current_key, &cached_key);
825
+ }
826
+
827
+ if (valid_cache) {
828
+ goto succeed;
829
+ }
830
+
831
+ close(cache_fd);
832
+ cache_fd = -1;
833
+ /* Cache is stale, invalid, or missing. Regenerate and write it out. */
834
+
835
+ /* Read the contents of the source file into a buffer */
836
+ if (bs_read_contents(current_fd, current_key.size, &contents, &errno_provenance) < 0) goto fail;
837
+ input_data = rb_str_new(contents, current_key.size);
838
+
839
+ /* Try to compile the input_data using input_to_storage(input_data) */
840
+ exception_tag = bs_input_to_storage(handler, Qnil, input_data, path_v, &storage_data);
841
+ if (exception_tag != 0) goto fail;
842
+
843
+ /* If input_to_storage raised Bootsnap::CompileCache::Uncompilable, don't try
844
+ * to cache anything; just return false */
845
+ if (storage_data == uncompilable) {
846
+ goto fail;
847
+ }
848
+ /* If storage_data isn't a string, we can't cache it */
849
+ if (!RB_TYPE_P(storage_data, T_STRING)) goto fail;
850
+
851
+ /* Write the cache key and storage_data to the cache directory */
852
+ res = atomic_write_cache_file(cache_path, &current_key, storage_data, &errno_provenance);
853
+ if (res < 0) goto fail;
854
+
855
+ goto succeed;
856
+
857
+ #define CLEANUP \
858
+ if (contents != NULL) xfree(contents); \
859
+ if (current_fd >= 0) close(current_fd); \
860
+ if (cache_fd >= 0) close(cache_fd);
861
+
862
+ succeed:
863
+ CLEANUP;
864
+ return Qtrue;
865
+ fail:
866
+ CLEANUP;
867
+ return Qfalse;
868
+ #undef CLEANUP
869
+ }
870
+
871
+
743
872
  /*****************************************************************************/
744
873
  /********************* Handler Wrappers **************************************/
745
874
  /*****************************************************************************
@@ -771,7 +900,6 @@ struct i2o_data {
771
900
 
772
901
  struct i2s_data {
773
902
  VALUE handler;
774
- VALUE args;
775
903
  VALUE input_data;
776
904
  VALUE pathval;
777
905
  };
@@ -789,7 +917,7 @@ bs_storage_to_output(VALUE handler, VALUE args, VALUE storage_data, VALUE * outp
789
917
  int state;
790
918
  struct s2o_data s2o_data = {
791
919
  .handler = handler,
792
- .args = args,
920
+ .args = args,
793
921
  .storage_data = storage_data,
794
922
  };
795
923
  *output_data = rb_protect(prot_storage_to_output, (VALUE)&s2o_data, &state);
@@ -818,7 +946,7 @@ static VALUE
818
946
  try_input_to_storage(VALUE arg)
819
947
  {
820
948
  struct i2s_data * data = (struct i2s_data *)arg;
821
- return rb_funcall(data->handler, rb_intern("input_to_storage"), 3, data->input_data, data->pathval, data->args);
949
+ return rb_funcall(data->handler, rb_intern("input_to_storage"), 2, data->input_data, data->pathval);
822
950
  }
823
951
 
824
952
  static VALUE
@@ -843,7 +971,6 @@ bs_input_to_storage(VALUE handler, VALUE args, VALUE input_data, VALUE pathval,
843
971
  int state;
844
972
  struct i2s_data i2s_data = {
845
973
  .handler = handler,
846
- .args = args,
847
974
  .input_data = input_data,
848
975
  .pathval = pathval,
849
976
  };