bootsnap-pr-184 1.3.1.pr.pre.184.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,280 @@
1
+ # Bootsnap [![Build Status](https://travis-ci.org/Shopify/bootsnap.svg?branch=master)](https://travis-ci.org/Shopify/bootsnap)
2
+
3
+ Bootsnap is a library that plugs into Ruby, with optional support for `ActiveSupport` and `YAML`,
4
+ to optimize and cache expensive computations. See [How Does This Work](#how-does-this-work).
5
+
6
+ #### Performance
7
+ - [Discourse](https://github.com/discourse/discourse) reports a boot time reduction of approximately 50%, from roughly 6 to 3 seconds on one machine;
8
+ - One of our smaller internal apps also sees a reduction of 50%, from 3.6 to 1.8 seconds;
9
+ - The core Shopify platform -- a rather large monolithic application -- boots about 75% faster, dropping from around 25s to 6.5s.
10
+
11
+ ## Usage
12
+
13
+ This gem works on macOS and Linux.
14
+
15
+ Add `bootsnap` to your `Gemfile`:
16
+
17
+ ```ruby
18
+ gem 'bootsnap', require: false
19
+ ```
20
+
21
+ If you are using Rails, add this to `config/boot.rb` immediately after `require 'bundler/setup'`:
22
+
23
+ ```ruby
24
+ require 'bootsnap/setup'
25
+ ```
26
+
27
+ It's technically possible to simply specify `gem 'bootsnap', require: 'bootsnap/setup'`, but it's
28
+ important to load Bootsnap as early as possible to get maximum performance improvement.
29
+
30
+ You can see how this require works [here](https://github.com/Shopify/bootsnap/blob/master/lib/bootsnap/setup.rb).
31
+
32
+ If you are not using Rails, or if you are but want more control over things, add this to your
33
+ application setup immediately after `require 'bundler/setup'` (i.e. as early as possible: the sooner
34
+ this is loaded, the sooner it can start optimizing things)
35
+
36
+ ```ruby
37
+ require 'bootsnap'
38
+ env = ENV['RAILS_ENV'] || "development"
39
+ Bootsnap.setup(
40
+ cache_dir: 'tmp/cache', # Path to your cache
41
+ development_mode: env == 'development', # Current working environment, e.g. RACK_ENV, RAILS_ENV, etc
42
+ load_path_cache: true, # Optimize the LOAD_PATH with a cache
43
+ autoload_paths_cache: true, # Optimize ActiveSupport autoloads with cache
44
+ disable_trace: true, # (Alpha) Set `RubyVM::InstructionSequence.compile_option = { trace_instruction: false }`
45
+ compile_cache_iseq: true, # Compile Ruby code into ISeq cache, breaks coverage reporting.
46
+ compile_cache_yaml: true # Compile YAML into a cache
47
+ )
48
+ ```
49
+
50
+ **Protip:** You can replace `require 'bootsnap'` with `BootLib::Require.from_gem('bootsnap',
51
+ 'bootsnap')` using [this trick](https://github.com/Shopify/bootsnap/wiki/Bootlib::Require). This
52
+ will help optimize boot time further if you have an extremely large `$LOAD_PATH`.
53
+
54
+ Note: Bootsnap and [Spring](https://github.com/rails/spring) are orthogonal tools. While Bootsnap
55
+ speeds up the loading of individual source files, Spring keeps a copy of a pre-booted Rails process
56
+ on hand to completely skip parts of the boot process the next time it's needed. The two tools work
57
+ well together, and are both included in a newly-generated Rails applications by default.
58
+
59
+ ### Environments
60
+
61
+ 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.
62
+
63
+ 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.
64
+
65
+ ## How does this work?
66
+
67
+ Bootsnap optimizes methods to cache results of expensive computations, and can be grouped
68
+ into two broad categories:
69
+
70
+ * [Path Pre-Scanning](#path-pre-scanning)
71
+ * `Kernel#require` and `Kernel#load` are modified to eliminate `$LOAD_PATH` scans.
72
+ * `ActiveSupport::Dependencies.{autoloadable_module?,load_missing_constant,depend_on}` are
73
+ overridden to eliminate scans of `ActiveSupport::Dependencies.autoload_paths`.
74
+ * [Compilation caching](#compilation-caching)
75
+ * `RubyVM::InstructionSequence.load_iseq` is implemented to cache the result of ruby bytecode
76
+ compilation.
77
+ * `YAML.load_file` is modified to cache the result of loading a YAML object in MessagePack format
78
+ (or Marshal, if the message uses types unsupported by MessagePack).
79
+
80
+ ### Path Pre-Scanning
81
+
82
+ *(This work is a minor evolution of [bootscale](https://github.com/byroot/bootscale)).*
83
+
84
+ Upon initialization of bootsnap or modification of the path (e.g. `$LOAD_PATH`),
85
+ `Bootsnap::LoadPathCache` will fetch a list of requirable entries from a cache, or, if necessary,
86
+ perform a full scan and cache the result.
87
+
88
+ Later, when we run (e.g.) `require 'foo'`, ruby *would* iterate through every item on our
89
+ `$LOAD_PATH` `['x', 'y', ...]`, looking for `x/foo.rb`, `y/foo.rb`, and so on. Bootsnap instead
90
+ looks at all the cached requirables for each `$LOAD_PATH` entry and substitutes the full expanded
91
+ path of the match ruby would have eventually chosen.
92
+
93
+ If you look at the syscalls generated by this behaviour, the net effect is that what would
94
+ previously look like this:
95
+
96
+ ```
97
+ open x/foo.rb # (fail)
98
+ # (imagine this with 500 $LOAD_PATH entries instead of two)
99
+ open y/foo.rb # (success)
100
+ close y/foo.rb
101
+ open y/foo.rb
102
+ ...
103
+ ```
104
+
105
+ becomes this:
106
+
107
+ ```
108
+ open y/foo.rb
109
+ ...
110
+ ```
111
+
112
+ Exactly the same strategy is employed for methods that traverse
113
+ `ActiveSupport::Dependencies.autoload_paths` if the `autoload_paths_cache` option is given to
114
+ `Bootsnap.setup`.
115
+
116
+ The following diagram flowcharts the overrides that make the `*_path_cache` features work.
117
+
118
+ ![Flowchart explaining
119
+ Bootsnap](https://cloud.githubusercontent.com/assets/3074765/24532120/eed94e64-158b-11e7-9137-438d759b2ac8.png)
120
+
121
+ Bootsnap classifies path entries into two categories: stable and volatile. Volatile entries are
122
+ scanned each time the application boots, and their caches are only valid for 30 seconds. Stable
123
+ entries do not expire -- once their contents has been scanned, it is assumed to never change.
124
+
125
+ The only directories considered "stable" are things under the Ruby install prefix
126
+ (`RbConfig::CONFIG['prefix']`, e.g. `/usr/local/ruby` or `~/.rubies/x.y.z`), and things under the
127
+ `Gem.path` (e.g. `~/.gem/ruby/x.y.z`) or `Bundler.bundle_path`. Everything else is considered
128
+ "volatile".
129
+
130
+ In addition to the [`Bootsnap::LoadPathCache::Cache`
131
+ source](https://github.com/Shopify/bootsnap/blob/master/lib/bootsnap/load_path_cache/cache.rb),
132
+ this diagram may help clarify how entry resolution works:
133
+
134
+ ![How path searching works](https://cloud.githubusercontent.com/assets/3074765/25388270/670b5652-299b-11e7-87fb-975647f68981.png)
135
+
136
+
137
+ It's also important to note how expensive `LoadError`s can be. If ruby invokes
138
+ `require 'something'`, but that file isn't on `$LOAD_PATH`, it takes `2 *
139
+ $LOAD_PATH.length` filesystem accesses to determine that. Bootsnap caches this
140
+ result too, raising a `LoadError` without touching the filesystem at all.
141
+
142
+ ### Compilation Caching
143
+
144
+ *(A more readable implementation of this concept can be found in
145
+ [yomikomu](https://github.com/ko1/yomikomu)).*
146
+
147
+ Ruby has complex grammar and parsing it is not a particularly cheap operation. Since 1.9, Ruby has
148
+ translated ruby source to an internal bytecode format, which is then executed by the Ruby VM. Since
149
+ 2.3.0, Ruby [exposes an API](https://ruby-doc.org/core-2.3.0/RubyVM/InstructionSequence.html) that
150
+ allows caching that bytecode. This allows us to bypass the relatively-expensive compilation step on
151
+ subsequent loads of the same file.
152
+
153
+ We also noticed that we spend a lot of time loading YAML documents during our application boot, and
154
+ that MessagePack and Marshal are *much* faster at deserialization than YAML, even with a fast
155
+ implementation. We use the same strategy of compilation caching for YAML documents, with the
156
+ equivalent of Ruby's "bytecode" format being a MessagePack document (or, in the case of YAML
157
+ documents with types unsupported by MessagePack, a Marshal stream).
158
+
159
+ These compilation results are stored in a cache directory, with filenames generated by taking a hash
160
+ of the full expanded path of the input file (FNV1a-64).
161
+
162
+ Whereas before, the sequence of syscalls generated to `require` a file would look like:
163
+
164
+ ```
165
+ open /c/foo.rb -> m
166
+ fstat64 m
167
+ close m
168
+ open /c/foo.rb -> o
169
+ fstat64 o
170
+ fstat64 o
171
+ read o
172
+ read o
173
+ ...
174
+ close o
175
+ ```
176
+
177
+ With bootsnap, we get:
178
+
179
+ ```
180
+ open /c/foo.rb -> n
181
+ fstat64 n
182
+ close n
183
+ open /c/foo.rb -> n
184
+ fstat64 n
185
+ open (cache) -> m
186
+ read m
187
+ read m
188
+ close m
189
+ close n
190
+ ```
191
+
192
+ This may look worse at a glance, but underlies a large performance difference.
193
+
194
+ *(The first three syscalls in both listings -- `open`, `fstat64`, `close` -- are not inherently
195
+ useful. [This ruby patch](https://bugs.ruby-lang.org/issues/13378) optimizes them out when coupled
196
+ with bootsnap.)*
197
+
198
+ Bootsnap writes a cache file containing a 64 byte header followed by the cache contents. The header
199
+ is a cache key including several fields:
200
+
201
+ * `version`, hardcoded in bootsnap. Essentially a schema version;
202
+ * `os_version`, A hash of the current kernel version (on macOS, BSD) or glibc version (on Linux);
203
+ * `compile_option`, which changes with `RubyVM::InstructionSequence.compile_option` does;
204
+ * `ruby_revision`, the version of Ruby this was compiled with;
205
+ * `size`, the size of the source file;
206
+ * `mtime`, the last-modification timestamp of the source file when it was compiled; and
207
+ * `data_size`, the number of bytes following the header, which we need to read it into a buffer.
208
+
209
+ If the key is valid, the result is loaded from the value. Otherwise, it is regenerated and clobbers
210
+ the current cache.
211
+
212
+ ### Putting it all together
213
+
214
+ Imagine we have this file structure:
215
+
216
+ ```
217
+ /
218
+ ├── a
219
+ ├── b
220
+ └── c
221
+ └── foo.rb
222
+ ```
223
+
224
+ And this `$LOAD_PATH`:
225
+
226
+ ```
227
+ ["/a", "/b", "/c"]
228
+ ```
229
+
230
+ When we call `require 'foo'` without bootsnap, Ruby would generate this sequence of syscalls:
231
+
232
+
233
+ ```
234
+ open /a/foo.rb -> -1
235
+ open /b/foo.rb -> -1
236
+ open /c/foo.rb -> n
237
+ close n
238
+ open /c/foo.rb -> m
239
+ fstat64 m
240
+ close m
241
+ open /c/foo.rb -> o
242
+ fstat64 o
243
+ fstat64 o
244
+ read o
245
+ read o
246
+ ...
247
+ close o
248
+ ```
249
+
250
+ With bootsnap, we get:
251
+
252
+ ```
253
+ open /c/foo.rb -> n
254
+ fstat64 n
255
+ close n
256
+ open /c/foo.rb -> n
257
+ fstat64 n
258
+ open (cache) -> m
259
+ read m
260
+ read m
261
+ close m
262
+ close n
263
+ ```
264
+
265
+ If we call `require 'nope'` without bootsnap, we get:
266
+
267
+ ```
268
+ open /a/nope.rb -> -1
269
+ open /b/nope.rb -> -1
270
+ open /c/nope.rb -> -1
271
+ open /a/nope.bundle -> -1
272
+ open /b/nope.bundle -> -1
273
+ open /c/nope.bundle -> -1
274
+ ```
275
+
276
+ ...and if we call `require 'nope'` *with* bootsnap, we get...
277
+
278
+ ```
279
+ # (nothing!)
280
+ ```
@@ -0,0 +1,12 @@
1
+ require 'rake/extensiontask'
2
+ require 'bundler/gem_tasks'
3
+
4
+ gemspec = Gem::Specification.load('bootsnap.gemspec')
5
+ Rake::ExtensionTask.new do |ext|
6
+ ext.name = 'bootsnap'
7
+ ext.ext_dir = 'ext/bootsnap'
8
+ ext.lib_dir = 'lib/bootsnap'
9
+ ext.gem_spec = gemspec
10
+ end
11
+
12
+ task(default: :compile)
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "bootsnap"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,8 @@
1
+ #!/bin/bash
2
+
3
+ if [[ $# -eq 0 ]]; then
4
+ exec ruby -I"test" -w -e 'Dir.glob("./test/**/*_test.rb").each { |f| require f }' -- "$@"
5
+ else
6
+ path=$1
7
+ exec ruby -I"test" -w -e "require '${path#test/}'" -- "$@"
8
+ fi
@@ -0,0 +1,39 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'bootsnap/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "bootsnap-pr-184"
8
+ spec.version = Bootsnap::VERSION
9
+ spec.authors = ["Burke Libbey"]
10
+ spec.email = ["burke.libbey@shopify.com"]
11
+
12
+ spec.license = "MIT"
13
+
14
+ spec.summary = "Boot large ruby/rails apps faster"
15
+ spec.description = spec.summary
16
+ spec.homepage = "https://github.com/Shopify/bootsnap"
17
+
18
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
19
+ f.match(%r{^(test|spec|features)/})
20
+ end
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.required_ruby_version = '>= 2.0.0'
24
+
25
+ if RUBY_PLATFORM =~ /java/
26
+ spec.platform = 'java'
27
+ else
28
+ spec.platform = Gem::Platform::RUBY
29
+ spec.extensions = ['ext/bootsnap/extconf.rb']
30
+ end
31
+
32
+ spec.add_development_dependency "bundler", '~> 1'
33
+ spec.add_development_dependency 'rake', '~> 10.0'
34
+ spec.add_development_dependency 'rake-compiler', '~> 0'
35
+ spec.add_development_dependency "minitest", "~> 5.0"
36
+ spec.add_development_dependency "mocha", "~> 1.2"
37
+
38
+ spec.add_runtime_dependency "msgpack", "~> 1.0"
39
+ end
data/dev.yml ADDED
@@ -0,0 +1,10 @@
1
+ env:
2
+ BOOTSNAP_PEDANTIC: '1'
3
+
4
+ up:
5
+ - ruby: 2.3.3
6
+ - bundler
7
+ commands:
8
+ build: rake compile
9
+ test: 'rake compile && exec bin/testunit'
10
+ style: 'exec rubocop -D'
@@ -0,0 +1,793 @@
1
+ /*
2
+ * Suggested reading order:
3
+ * 1. Skim Init_bootsnap
4
+ * 2. Skim bs_fetch
5
+ * 3. The rest of everything
6
+ *
7
+ * Init_bootsnap sets up the ruby objects and binds bs_fetch to
8
+ * Bootsnap::CompileCache::Native.fetch.
9
+ *
10
+ * bs_fetch is the ultimate caller for for just about every other function in
11
+ * here.
12
+ */
13
+
14
+ #include "bootsnap.h"
15
+ #include "ruby.h"
16
+ #include <stdint.h>
17
+ #include <sys/types.h>
18
+ #include <errno.h>
19
+ #include <fcntl.h>
20
+ #include <sys/stat.h>
21
+ #ifndef _WIN32
22
+ #include <sys/utsname.h>
23
+ #endif
24
+
25
+ /* 1000 is an arbitrary limit; FNV64 plus some slashes brings the cap down to
26
+ * 981 for the cache dir */
27
+ #define MAX_CACHEPATH_SIZE 1000
28
+ #define MAX_CACHEDIR_SIZE 981
29
+
30
+ #define KEY_SIZE 64
31
+
32
+ /*
33
+ * An instance of this key is written as the first 64 bytes of each cache file.
34
+ * The mtime and size members track whether the file contents have changed, and
35
+ * the version, ruby_platform, compile_option, and ruby_revision members track
36
+ * changes to the environment that could invalidate compile results without
37
+ * file contents having changed. The data_size member is not truly part of the
38
+ * "key". Really, this could be called a "header" with the first six members
39
+ * being an embedded "key" struct and an additional data_size member.
40
+ *
41
+ * The data_size indicates the remaining number of bytes in the cache file
42
+ * after the header (the size of the cached artifact).
43
+ *
44
+ * After data_size, the struct is padded to 64 bytes.
45
+ */
46
+ struct bs_cache_key {
47
+ uint32_t version;
48
+ uint32_t ruby_platform;
49
+ uint32_t compile_option;
50
+ uint32_t ruby_revision;
51
+ uint64_t size;
52
+ uint64_t mtime;
53
+ uint64_t data_size; /* not used for equality */
54
+ uint8_t pad[24];
55
+ } __attribute__((packed));
56
+
57
+ /*
58
+ * If the struct padding isn't correct to pad the key to 64 bytes, refuse to
59
+ * compile.
60
+ */
61
+ #define STATIC_ASSERT(X) STATIC_ASSERT2(X,__LINE__)
62
+ #define STATIC_ASSERT2(X,L) STATIC_ASSERT3(X,L)
63
+ #define STATIC_ASSERT3(X,L) STATIC_ASSERT_MSG(X,at_line_##L)
64
+ #define STATIC_ASSERT_MSG(COND,MSG) typedef char static_assertion_##MSG[(!!(COND))*2-1]
65
+ STATIC_ASSERT(sizeof(struct bs_cache_key) == KEY_SIZE);
66
+
67
+ /* Effectively a schema version. Bumping invalidates all previous caches */
68
+ static const uint32_t current_version = 2;
69
+
70
+ /* hash of e.g. "x86_64-darwin17", invalidating when ruby is recompiled on a
71
+ * new OS ABI, etc. */
72
+ static uint32_t current_ruby_platform;
73
+ /* Invalidates cache when switching ruby versions */
74
+ static uint32_t current_ruby_revision;
75
+ /* Invalidates cache when RubyVM::InstructionSequence.compile_option changes */
76
+ static uint32_t current_compile_option_crc32 = 0;
77
+
78
+ /* Bootsnap::CompileCache::{Native, Uncompilable} */
79
+ static VALUE rb_mBootsnap;
80
+ static VALUE rb_mBootsnap_CompileCache;
81
+ static VALUE rb_mBootsnap_CompileCache_Native;
82
+ static VALUE rb_eBootsnap_CompileCache_Uncompilable;
83
+ static ID uncompilable;
84
+
85
+ /* Functions exposed as module functions on Bootsnap::CompileCache::Native */
86
+ static VALUE bs_compile_option_crc32_set(VALUE self, VALUE crc32_v);
87
+ static VALUE bs_rb_fetch(VALUE self, VALUE cachedir_v, VALUE path_v, VALUE handler);
88
+
89
+ /* Helpers */
90
+ static uint64_t fnv1a_64(const char *str);
91
+ static void bs_cache_path(const char * cachedir, const char * path, char ** cache_path);
92
+ static int bs_read_key(int fd, struct bs_cache_key * key);
93
+ static int cache_key_equal(struct bs_cache_key * k1, struct bs_cache_key * k2);
94
+ static VALUE bs_fetch(char * path, VALUE path_v, char * cache_path, VALUE handler);
95
+ static int open_current_file(char * path, struct bs_cache_key * key, char ** errno_provenance);
96
+ static int fetch_cached_data(int fd, ssize_t data_size, VALUE handler, VALUE * output_data, int * exception_tag, char ** errno_provenance);
97
+ static uint32_t get_ruby_platform(void);
98
+
99
+ /*
100
+ * Helper functions to call ruby methods on handler object without crashing on
101
+ * exception.
102
+ */
103
+ static int bs_storage_to_output(VALUE handler, VALUE storage_data, VALUE * output_data);
104
+ static VALUE prot_storage_to_output(VALUE arg);
105
+ static VALUE prot_input_to_output(VALUE arg);
106
+ static void bs_input_to_output(VALUE handler, VALUE input_data, VALUE * output_data, int * exception_tag);
107
+ static VALUE prot_input_to_storage(VALUE arg);
108
+ static int bs_input_to_storage(VALUE handler, VALUE input_data, VALUE pathval, VALUE * storage_data);
109
+ struct s2o_data;
110
+ struct i2o_data;
111
+ struct i2s_data;
112
+
113
+ /* https://bugs.ruby-lang.org/issues/13667 */
114
+ extern VALUE rb_get_coverages(void);
115
+ static VALUE
116
+ bs_rb_coverage_running(VALUE self)
117
+ {
118
+ VALUE cov = rb_get_coverages();
119
+ return RTEST(cov) ? Qtrue : Qfalse;
120
+ }
121
+
122
+ /*
123
+ * Ruby C extensions are initialized by calling Init_<extname>.
124
+ *
125
+ * This sets up the module hierarchy and attaches functions as methods.
126
+ *
127
+ * We also populate some semi-static information about the current OS and so on.
128
+ */
129
+ void
130
+ Init_bootsnap(void)
131
+ {
132
+ rb_mBootsnap = rb_define_module("Bootsnap");
133
+ rb_mBootsnap_CompileCache = rb_define_module_under(rb_mBootsnap, "CompileCache");
134
+ rb_mBootsnap_CompileCache_Native = rb_define_module_under(rb_mBootsnap_CompileCache, "Native");
135
+ rb_eBootsnap_CompileCache_Uncompilable = rb_define_class_under(rb_mBootsnap_CompileCache, "Uncompilable", rb_eStandardError);
136
+
137
+ current_ruby_revision = FIX2INT(rb_const_get(rb_cObject, rb_intern("RUBY_REVISION")));
138
+ current_ruby_platform = get_ruby_platform();
139
+
140
+ uncompilable = rb_intern("__bootsnap_uncompilable__");
141
+
142
+ rb_define_module_function(rb_mBootsnap_CompileCache_Native, "coverage_running?", bs_rb_coverage_running, 0);
143
+ rb_define_module_function(rb_mBootsnap_CompileCache_Native, "fetch", bs_rb_fetch, 3);
144
+ rb_define_module_function(rb_mBootsnap_CompileCache_Native, "compile_option_crc32=", bs_compile_option_crc32_set, 1);
145
+ }
146
+
147
+ /*
148
+ * Bootsnap's ruby code registers a hook that notifies us via this function
149
+ * when compile_option changes. These changes invalidate all existing caches.
150
+ *
151
+ * Note that on 32-bit platforms, a CRC32 can't be represented in a Fixnum, but
152
+ * can be represented by a uint.
153
+ */
154
+ static VALUE
155
+ bs_compile_option_crc32_set(VALUE self, VALUE crc32_v)
156
+ {
157
+ if (!RB_TYPE_P(crc32_v, T_BIGNUM) && !RB_TYPE_P(crc32_v, T_FIXNUM)) {
158
+ Check_Type(crc32_v, T_FIXNUM);
159
+ }
160
+ current_compile_option_crc32 = NUM2UINT(crc32_v);
161
+ return Qnil;
162
+ }
163
+
164
+ /*
165
+ * We use FNV1a-64 to derive cache paths. The choice is somewhat arbitrary but
166
+ * it has several nice properties:
167
+ *
168
+ * - Tiny implementation
169
+ * - No external dependency
170
+ * - Solid performance
171
+ * - Solid randomness
172
+ * - 32 bits doesn't feel collision-resistant enough; 64 is nice.
173
+ */
174
+ static uint64_t
175
+ fnv1a_64_iter(uint64_t h, const char *str)
176
+ {
177
+ unsigned char *s = (unsigned char *)str;
178
+
179
+ while (*s) {
180
+ h ^= (uint64_t)*s++;
181
+ h += (h << 1) + (h << 4) + (h << 5) + (h << 7) + (h << 8) + (h << 40);
182
+ }
183
+
184
+ return h;
185
+ }
186
+
187
+ static uint64_t
188
+ fnv1a_64(const char *str)
189
+ {
190
+ uint64_t h = (uint64_t)0xcbf29ce484222325ULL;
191
+ return fnv1a_64_iter(h, str);
192
+ }
193
+
194
+ /*
195
+ * When ruby's version doesn't change, but it's recompiled on a different OS
196
+ * (or OS version), we need to invalidate the cache.
197
+ *
198
+ * We actually factor in some extra information here, to be extra confident
199
+ * that we don't try to re-use caches that will not be compatible, by factoring
200
+ * in utsname.version.
201
+ */
202
+ static uint32_t
203
+ get_ruby_platform(void)
204
+ {
205
+ uint64_t hash;
206
+ VALUE ruby_platform;
207
+
208
+ ruby_platform = rb_const_get(rb_cObject, rb_intern("RUBY_PLATFORM"));
209
+ hash = fnv1a_64(RSTRING_PTR(ruby_platform));
210
+
211
+ #ifdef _WIN32
212
+ return (uint32_t)(hash >> 32) ^ (uint32_t)GetVersion();
213
+ #else
214
+ struct utsname utsname;
215
+
216
+ /* Not worth crashing if this fails; lose extra cache invalidation potential */
217
+ if (uname(&utsname) >= 0) {
218
+ hash = fnv1a_64_iter(hash, utsname.version);
219
+ }
220
+
221
+ return (uint32_t)(hash >> 32);
222
+ #endif
223
+ }
224
+
225
+ /*
226
+ * Given a cache root directory and the full path to a file being cached,
227
+ * generate a path under the cache directory at which the cached artifact will
228
+ * be stored.
229
+ *
230
+ * The path will look something like: <cachedir>/12/34567890abcdef
231
+ */
232
+ static void
233
+ bs_cache_path(const char * cachedir, const char * path, char ** cache_path)
234
+ {
235
+ uint64_t hash = fnv1a_64(path);
236
+
237
+ uint8_t first_byte = (hash >> (64 - 8));
238
+ uint64_t remainder = hash & 0x00ffffffffffffff;
239
+
240
+ sprintf(*cache_path, "%s/%02x/%014llx", cachedir, first_byte, remainder);
241
+ }
242
+
243
+ /*
244
+ * Test whether a newly-generated cache key based on the file as it exists on
245
+ * disk matches the one that was generated when the file was cached (or really
246
+ * compare any two keys).
247
+ *
248
+ * The data_size member is not compared, as it serves more of a "header"
249
+ * function.
250
+ */
251
+ static int
252
+ cache_key_equal(struct bs_cache_key * k1, struct bs_cache_key * k2)
253
+ {
254
+ return (
255
+ k1->version == k2->version &&
256
+ k1->ruby_platform == k2->ruby_platform &&
257
+ k1->compile_option == k2->compile_option &&
258
+ k1->ruby_revision == k2->ruby_revision &&
259
+ k1->size == k2->size &&
260
+ k1->mtime == k2->mtime
261
+ );
262
+ }
263
+
264
+ /*
265
+ * Entrypoint for Bootsnap::CompileCache::Native.fetch. The real work is done
266
+ * in bs_fetch; this function just performs some basic typechecks and
267
+ * conversions on the ruby VALUE arguments before passing them along.
268
+ */
269
+ static VALUE
270
+ bs_rb_fetch(VALUE self, VALUE cachedir_v, VALUE path_v, VALUE handler)
271
+ {
272
+ Check_Type(cachedir_v, T_STRING);
273
+ Check_Type(path_v, T_STRING);
274
+
275
+ if (RSTRING_LEN(cachedir_v) > MAX_CACHEDIR_SIZE) {
276
+ rb_raise(rb_eArgError, "cachedir too long");
277
+ }
278
+
279
+ char * cachedir = RSTRING_PTR(cachedir_v);
280
+ char * path = RSTRING_PTR(path_v);
281
+ char cache_path[MAX_CACHEPATH_SIZE];
282
+
283
+ { /* generate cache path to cache_path */
284
+ char * tmp = (char *)&cache_path;
285
+ bs_cache_path(cachedir, path, &tmp);
286
+ }
287
+
288
+ return bs_fetch(path, path_v, cache_path, handler);
289
+ }
290
+
291
+ /*
292
+ * Open the file we want to load/cache and generate a cache key for it if it
293
+ * was loaded.
294
+ */
295
+ static int
296
+ open_current_file(char * path, struct bs_cache_key * key, char ** errno_provenance)
297
+ {
298
+ struct stat statbuf;
299
+ int fd;
300
+
301
+ fd = open(path, O_RDONLY);
302
+ if (fd < 0) {
303
+ *errno_provenance = (char *)"bs_fetch:open_current_file:open";
304
+ return fd;
305
+ }
306
+ #ifdef _WIN32
307
+ setmode(fd, O_BINARY);
308
+ #endif
309
+
310
+ if (fstat(fd, &statbuf) < 0) {
311
+ *errno_provenance = (char *)"bs_fetch:open_current_file:fstat";
312
+ close(fd);
313
+ return -1;
314
+ }
315
+
316
+ key->version = current_version;
317
+ key->ruby_platform = current_ruby_platform;
318
+ key->compile_option = current_compile_option_crc32;
319
+ key->ruby_revision = current_ruby_revision;
320
+ key->size = (uint64_t)statbuf.st_size;
321
+ key->mtime = (uint64_t)statbuf.st_mtime;
322
+
323
+ return fd;
324
+ }
325
+
326
+ #define ERROR_WITH_ERRNO -1
327
+ #define CACHE_MISSING_OR_INVALID -2
328
+
329
+ /*
330
+ * Read the cache key from the given fd, which must have position 0 (e.g.
331
+ * freshly opened file).
332
+ *
333
+ * Possible return values:
334
+ * - 0 (OK, key was loaded)
335
+ * - CACHE_MISSING_OR_INVALID (-2)
336
+ * - ERROR_WITH_ERRNO (-1, errno is set)
337
+ */
338
+ static int
339
+ bs_read_key(int fd, struct bs_cache_key * key)
340
+ {
341
+ ssize_t nread = read(fd, key, KEY_SIZE);
342
+ if (nread < 0) return ERROR_WITH_ERRNO;
343
+ if (nread < KEY_SIZE) return CACHE_MISSING_OR_INVALID;
344
+ return 0;
345
+ }
346
+
347
+ /*
348
+ * Open the cache file at a given path, if it exists, and read its key into the
349
+ * struct.
350
+ *
351
+ * Possible return values:
352
+ * - 0 (OK, key was loaded)
353
+ * - CACHE_MISSING_OR_INVALID (-2)
354
+ * - ERROR_WITH_ERRNO (-1, errno is set)
355
+ */
356
+ static int
357
+ open_cache_file(const char * path, struct bs_cache_key * key, char ** errno_provenance)
358
+ {
359
+ int fd, res;
360
+
361
+ fd = open(path, O_RDONLY);
362
+ if (fd < 0) {
363
+ *errno_provenance = (char *)"bs_fetch:open_cache_file:open";
364
+ if (errno == ENOENT) return CACHE_MISSING_OR_INVALID;
365
+ return ERROR_WITH_ERRNO;
366
+ }
367
+ #ifdef _WIN32
368
+ setmode(fd, O_BINARY);
369
+ #endif
370
+
371
+ res = bs_read_key(fd, key);
372
+ if (res < 0) {
373
+ *errno_provenance = (char *)"bs_fetch:open_cache_file:read";
374
+ close(fd);
375
+ return res;
376
+ }
377
+
378
+ return fd;
379
+ }
380
+
381
+ /*
382
+ * The cache file is laid out like:
383
+ * 0...64 : bs_cache_key
384
+ * 64..-1 : cached artifact
385
+ *
386
+ * This function takes a file descriptor whose position is pre-set to 64, and
387
+ * the data_size (corresponding to the remaining number of bytes) listed in the
388
+ * cache header.
389
+ *
390
+ * We load the text from this file into a buffer, and pass it to the ruby-land
391
+ * handler with exception handling via the exception_tag param.
392
+ *
393
+ * Data is returned via the output_data parameter, which, if there's no error
394
+ * or exception, will be the final data returnable to the user.
395
+ */
396
+ static int
397
+ fetch_cached_data(int fd, ssize_t data_size, VALUE handler, VALUE * output_data, int * exception_tag, char ** errno_provenance)
398
+ {
399
+ char * data = NULL;
400
+ ssize_t nread;
401
+ int ret;
402
+
403
+ VALUE storage_data;
404
+
405
+ if (data_size > 100000000000) {
406
+ *errno_provenance = (char *)"bs_fetch:fetch_cached_data:datasize";
407
+ errno = EINVAL; /* because wtf? */
408
+ ret = -1;
409
+ goto done;
410
+ }
411
+ data = ALLOC_N(char, data_size);
412
+ nread = read(fd, data, data_size);
413
+ if (nread < 0) {
414
+ *errno_provenance = (char *)"bs_fetch:fetch_cached_data:read";
415
+ ret = -1;
416
+ goto done;
417
+ }
418
+ if (nread != data_size) {
419
+ ret = CACHE_MISSING_OR_INVALID;
420
+ goto done;
421
+ }
422
+
423
+ storage_data = rb_str_new_static(data, data_size);
424
+
425
+ *exception_tag = bs_storage_to_output(handler, storage_data, output_data);
426
+ ret = 0;
427
+ done:
428
+ if (data != NULL) xfree(data);
429
+ return ret;
430
+ }
431
+
432
+ /*
433
+ * Like mkdir -p, this recursively creates directory parents of a file. e.g.
434
+ * given /a/b/c, creates /a and /a/b.
435
+ */
436
+ static int
437
+ mkpath(char * file_path, mode_t mode)
438
+ {
439
+ /* It would likely be more efficient to count back until we
440
+ * find a component that *does* exist, but this will only run
441
+ * at most 256 times, so it seems not worthwhile to change. */
442
+ char * p;
443
+ for (p = strchr(file_path + 1, '/'); p; p = strchr(p + 1, '/')) {
444
+ *p = '\0';
445
+ #ifdef _WIN32
446
+ if (mkdir(file_path) == -1) {
447
+ #else
448
+ if (mkdir(file_path, mode) == -1) {
449
+ #endif
450
+ if (errno != EEXIST) {
451
+ *p = '/';
452
+ return -1;
453
+ }
454
+ }
455
+ *p = '/';
456
+ }
457
+ return 0;
458
+ }
459
+
460
+ /*
461
+ * Write a cache header/key and a compiled artifact to a given cache path by
462
+ * writing to a tmpfile and then renaming the tmpfile over top of the final
463
+ * path.
464
+ */
465
+ static int
466
+ atomic_write_cache_file(char * path, struct bs_cache_key * key, VALUE data, char ** errno_provenance)
467
+ {
468
+ char template[MAX_CACHEPATH_SIZE + 20];
469
+ char * dest;
470
+ char * tmp_path;
471
+ int fd, ret;
472
+ ssize_t nwrite;
473
+
474
+ dest = strncpy(template, path, MAX_CACHEPATH_SIZE);
475
+ strcat(dest, ".tmp.XXXXXX");
476
+
477
+ tmp_path = mktemp(template);
478
+ fd = open(tmp_path, O_WRONLY | O_CREAT, 0664);
479
+ if (fd < 0) {
480
+ if (mkpath(path, 0775) < 0) {
481
+ *errno_provenance = (char *)"bs_fetch:atomic_write_cache_file:mkpath";
482
+ return -1;
483
+ }
484
+ fd = open(tmp_path, O_WRONLY | O_CREAT, 0664);
485
+ if (fd < 0) {
486
+ *errno_provenance = (char *)"bs_fetch:atomic_write_cache_file:open";
487
+ return -1;
488
+ }
489
+ }
490
+ #ifdef _WIN32
491
+ setmode(fd, O_BINARY);
492
+ #endif
493
+
494
+ key->data_size = RSTRING_LEN(data);
495
+ nwrite = write(fd, key, KEY_SIZE);
496
+ if (nwrite < 0) {
497
+ *errno_provenance = (char *)"bs_fetch:atomic_write_cache_file:write";
498
+ return -1;
499
+ }
500
+ if (nwrite != KEY_SIZE) {
501
+ *errno_provenance = (char *)"bs_fetch:atomic_write_cache_file:keysize";
502
+ errno = EIO; /* Lies but whatever */
503
+ return -1;
504
+ }
505
+
506
+ nwrite = write(fd, RSTRING_PTR(data), RSTRING_LEN(data));
507
+ if (nwrite < 0) return -1;
508
+ if (nwrite != RSTRING_LEN(data)) {
509
+ *errno_provenance = (char *)"bs_fetch:atomic_write_cache_file:writelength";
510
+ errno = EIO; /* Lies but whatever */
511
+ return -1;
512
+ }
513
+
514
+ close(fd);
515
+ ret = rename(tmp_path, path);
516
+ if (ret < 0) {
517
+ *errno_provenance = (char *)"bs_fetch:atomic_write_cache_file:rename";
518
+ }
519
+ return ret;
520
+ }
521
+
522
+
523
+ /* Read contents from an fd, whose contents are asserted to be +size+ bytes
524
+ * long, into a buffer */
525
+ static ssize_t
526
+ bs_read_contents(int fd, size_t size, char ** contents, char ** errno_provenance)
527
+ {
528
+ ssize_t nread;
529
+ *contents = ALLOC_N(char, size);
530
+ nread = read(fd, *contents, size);
531
+ if (nread < 0) {
532
+ *errno_provenance = (char *)"bs_fetch:bs_read_contents:read";
533
+ }
534
+ return nread;
535
+ }
536
+
537
+ /*
538
+ * This is the meat of the extension. bs_fetch is
539
+ * Bootsnap::CompileCache::Native.fetch.
540
+ *
541
+ * There are three "formats" in use here:
542
+ * 1. "input" format, which is what we load from the source file;
543
+ * 2. "storage" format, which we write to the cache;
544
+ * 3. "output" format, which is what we return.
545
+ *
546
+ * E.g., For ISeq compilation:
547
+ * input: ruby source, as text
548
+ * storage: binary string (RubyVM::InstructionSequence#to_binary)
549
+ * output: Instance of RubyVM::InstructionSequence
550
+ *
551
+ * And for YAML:
552
+ * input: yaml as text
553
+ * storage: MessagePack or Marshal text
554
+ * output: ruby object, loaded from yaml/messagepack/marshal
555
+ *
556
+ * A handler<I,S,O> passed in must support three messages:
557
+ * * storage_to_output(S) -> O
558
+ * * input_to_output(I) -> O
559
+ * * input_to_storage(I) -> S
560
+ * (input_to_storage may raise Bootsnap::CompileCache::Uncompilable, which
561
+ * will prevent caching and cause output to be generated with
562
+ * input_to_output)
563
+ *
564
+ * The semantics of this function are basically:
565
+ *
566
+ * return storage_to_output(cache[path]) if cache[path]
567
+ * storage = input_to_storage(input)
568
+ * cache[path] = storage
569
+ * return storage_to_output(storage)
570
+ *
571
+ * Or expanded a bit:
572
+ *
573
+ * - Check if the cache file exists and is up to date.
574
+ * - If it is, load this data to storage_data.
575
+ * - return storage_to_output(storage_data)
576
+ * - Read the file to input_data
577
+ * - Generate storage_data using input_to_storage(input_data)
578
+ * - Write storage_data data, with a cache key, to the cache file.
579
+ * - Return storage_to_output(storage_data)
580
+ */
581
+ static VALUE
582
+ bs_fetch(char * path, VALUE path_v, char * cache_path, VALUE handler)
583
+ {
584
+ struct bs_cache_key cached_key, current_key;
585
+ char * contents = NULL;
586
+ int cache_fd = -1, current_fd = -1;
587
+ int res, valid_cache = 0, exception_tag = 0;
588
+ char * errno_provenance = NULL;
589
+
590
+ VALUE input_data; /* data read from source file, e.g. YAML or ruby source */
591
+ VALUE storage_data; /* compiled data, e.g. msgpack / binary iseq */
592
+ VALUE output_data; /* return data, e.g. ruby hash or loaded iseq */
593
+
594
+ VALUE exception; /* ruby exception object to raise instead of returning */
595
+
596
+ /* Open the source file and generate a cache key for it */
597
+ current_fd = open_current_file(path, &current_key, &errno_provenance);
598
+ if (current_fd < 0) goto fail_errno;
599
+
600
+ /* Open the cache key if it exists, and read its cache key in */
601
+ cache_fd = open_cache_file(cache_path, &cached_key, &errno_provenance);
602
+ if (cache_fd == CACHE_MISSING_OR_INVALID) {
603
+ /* This is ok: valid_cache remains false, we re-populate it. */
604
+ } else if (cache_fd < 0) {
605
+ goto fail_errno;
606
+ } else {
607
+ /* True if the cache existed and no invalidating changes have occurred since
608
+ * it was generated. */
609
+ valid_cache = cache_key_equal(&current_key, &cached_key);
610
+ }
611
+
612
+ if (valid_cache) {
613
+ /* Fetch the cache data and return it if we're able to load it successfully */
614
+ res = fetch_cached_data(
615
+ cache_fd, (ssize_t)cached_key.data_size, handler,
616
+ &output_data, &exception_tag, &errno_provenance
617
+ );
618
+ if (exception_tag != 0) goto raise;
619
+ else if (res == CACHE_MISSING_OR_INVALID) valid_cache = 0;
620
+ else if (res == ERROR_WITH_ERRNO) goto fail_errno;
621
+ else if (!NIL_P(output_data)) goto succeed; /* fast-path, goal */
622
+ }
623
+ close(cache_fd);
624
+ cache_fd = -1;
625
+ /* Cache is stale, invalid, or missing. Regenerate and write it out. */
626
+
627
+ /* Read the contents of the source file into a buffer */
628
+ if (bs_read_contents(current_fd, current_key.size, &contents, &errno_provenance) < 0) goto fail_errno;
629
+ input_data = rb_str_new_static(contents, current_key.size);
630
+
631
+ /* Try to compile the input_data using input_to_storage(input_data) */
632
+ exception_tag = bs_input_to_storage(handler, input_data, path_v, &storage_data);
633
+ if (exception_tag != 0) goto raise;
634
+ /* If input_to_storage raised Bootsnap::CompileCache::Uncompilable, don't try
635
+ * to cache anything; just return input_to_output(input_data) */
636
+ if (storage_data == uncompilable) {
637
+ bs_input_to_output(handler, input_data, &output_data, &exception_tag);
638
+ if (exception_tag != 0) goto raise;
639
+ goto succeed;
640
+ }
641
+ /* If storage_data isn't a string, we can't cache it */
642
+ if (!RB_TYPE_P(storage_data, T_STRING)) goto invalid_type_storage_data;
643
+
644
+ /* Write the cache key and storage_data to the cache directory */
645
+ res = atomic_write_cache_file(cache_path, &current_key, storage_data, &errno_provenance);
646
+ if (res < 0) goto fail_errno;
647
+
648
+ /* Having written the cache, now convert storage_data to output_data */
649
+ exception_tag = bs_storage_to_output(handler, storage_data, &output_data);
650
+ if (exception_tag != 0) goto raise;
651
+
652
+ /* If output_data is nil, delete the cache entry and generate the output
653
+ * using input_to_output */
654
+ if (NIL_P(output_data)) {
655
+ if (unlink(cache_path) < 0) {
656
+ errno_provenance = (char *)"bs_fetch:unlink";
657
+ goto fail_errno;
658
+ }
659
+ bs_input_to_output(handler, input_data, &output_data, &exception_tag);
660
+ if (exception_tag != 0) goto raise;
661
+ }
662
+
663
+ goto succeed; /* output_data is now the correct return. */
664
+
665
+ #define CLEANUP \
666
+ if (contents != NULL) xfree(contents); \
667
+ if (current_fd >= 0) close(current_fd); \
668
+ if (cache_fd >= 0) close(cache_fd);
669
+
670
+ succeed:
671
+ CLEANUP;
672
+ return output_data;
673
+ fail_errno:
674
+ CLEANUP;
675
+ exception = rb_syserr_new(errno, errno_provenance);
676
+ rb_exc_raise(exception);
677
+ __builtin_unreachable();
678
+ raise:
679
+ CLEANUP;
680
+ rb_jump_tag(exception_tag);
681
+ __builtin_unreachable();
682
+ invalid_type_storage_data:
683
+ CLEANUP;
684
+ Check_Type(storage_data, T_STRING);
685
+ __builtin_unreachable();
686
+
687
+ #undef CLEANUP
688
+ }
689
+
690
+ /*****************************************************************************/
691
+ /********************* Handler Wrappers **************************************/
692
+ /*****************************************************************************
693
+ * Everything after this point in the file is just wrappers to deal with ruby's
694
+ * clunky method of handling exceptions from ruby methods invoked from C:
695
+ *
696
+ * In order to call a ruby method from C, while protecting against crashing in
697
+ * the event of an exception, we must call the method with rb_protect().
698
+ *
699
+ * rb_protect takes a C function and precisely one argument; however, we want
700
+ * to pass multiple arguments, so we must create structs to wrap them up.
701
+ *
702
+ * These functions return an exception_tag, which, if non-zero, indicates an
703
+ * exception that should be jumped to with rb_jump_tag after cleaning up
704
+ * allocated resources.
705
+ */
706
+
707
+ struct s2o_data {
708
+ VALUE handler;
709
+ VALUE storage_data;
710
+ };
711
+
712
+ struct i2o_data {
713
+ VALUE handler;
714
+ VALUE input_data;
715
+ };
716
+
717
+ struct i2s_data {
718
+ VALUE handler;
719
+ VALUE input_data;
720
+ VALUE pathval;
721
+ };
722
+
723
+ static VALUE
724
+ prot_storage_to_output(VALUE arg)
725
+ {
726
+ struct s2o_data * data = (struct s2o_data *)arg;
727
+ return rb_funcall(data->handler, rb_intern("storage_to_output"), 1, data->storage_data);
728
+ }
729
+
730
+ static int
731
+ bs_storage_to_output(VALUE handler, VALUE storage_data, VALUE * output_data)
732
+ {
733
+ int state;
734
+ struct s2o_data s2o_data = {
735
+ .handler = handler,
736
+ .storage_data = storage_data,
737
+ };
738
+ *output_data = rb_protect(prot_storage_to_output, (VALUE)&s2o_data, &state);
739
+ return state;
740
+ }
741
+
742
+ static void
743
+ bs_input_to_output(VALUE handler, VALUE input_data, VALUE * output_data, int * exception_tag)
744
+ {
745
+ struct i2o_data i2o_data = {
746
+ .handler = handler,
747
+ .input_data = input_data,
748
+ };
749
+ *output_data = rb_protect(prot_input_to_output, (VALUE)&i2o_data, exception_tag);
750
+ }
751
+
752
+ static VALUE
753
+ prot_input_to_output(VALUE arg)
754
+ {
755
+ struct i2o_data * data = (struct i2o_data *)arg;
756
+ return rb_funcall(data->handler, rb_intern("input_to_output"), 1, data->input_data);
757
+ }
758
+
759
+ static VALUE
760
+ try_input_to_storage(VALUE arg)
761
+ {
762
+ struct i2s_data * data = (struct i2s_data *)arg;
763
+ return rb_funcall(data->handler, rb_intern("input_to_storage"), 2, data->input_data, data->pathval);
764
+ }
765
+
766
+ static VALUE
767
+ rescue_input_to_storage(VALUE arg)
768
+ {
769
+ return uncompilable;
770
+ }
771
+
772
+ static VALUE
773
+ prot_input_to_storage(VALUE arg)
774
+ {
775
+ struct i2s_data * data = (struct i2s_data *)arg;
776
+ return rb_rescue2(
777
+ try_input_to_storage, (VALUE)data,
778
+ rescue_input_to_storage, Qnil,
779
+ rb_eBootsnap_CompileCache_Uncompilable, 0);
780
+ }
781
+
782
+ static int
783
+ bs_input_to_storage(VALUE handler, VALUE input_data, VALUE pathval, VALUE * storage_data)
784
+ {
785
+ int state;
786
+ struct i2s_data i2s_data = {
787
+ .handler = handler,
788
+ .input_data = input_data,
789
+ .pathval = pathval,
790
+ };
791
+ *storage_data = rb_protect(prot_input_to_storage, (VALUE)&i2s_data, &state);
792
+ return state;
793
+ }