Module: Bootsnap::CompileCache::ISeq

Defined in:
lib/bootsnap/compile_cache/iseq.rb

Defined Under Namespace

Modules: InstructionSequenceMixin Classes: Compiler

Constant Summary collapse

DEFAULT =
Compiler.new
FROZEN_STRING_LITERAL =
Compiler.new("-fstr", {frozen_string_literal: true}.freeze)
COVERAGE_SUPPORTED =
RUBY_VERSION >= "4.0.4"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.cache_dirObject

Returns the value of attribute cache_dir.



10
11
12
# File 'lib/bootsnap/compile_cache/iseq.rb', line 10

def cache_dir
  @cache_dir
end

.compiler_selectorObject

Returns the value of attribute compiler_selector.



11
12
13
# File 'lib/bootsnap/compile_cache/iseq.rb', line 11

def compiler_selector
  @compiler_selector
end

.default_compilerObject

Returns the value of attribute default_compiler.



11
12
13
# File 'lib/bootsnap/compile_cache/iseq.rb', line 11

def default_compiler
  @default_compiler
end

Class Method Details

.compile_option_updatedObject



180
181
182
183
184
# File 'lib/bootsnap/compile_cache/iseq.rb', line 180

def self.compile_option_updated
  option = RubyVM::InstructionSequence.compile_option
  crc = Zlib.crc32(option.inspect)
  Bootsnap::CompileCache::Native.compile_option_crc32 = crc
end

.coverage_on?Boolean

Returns:

  • (Boolean)


155
156
157
# File 'lib/bootsnap/compile_cache/iseq.rb', line 155

def self.coverage_on?
  defined?(Coverage) && Coverage.running?
end

.fetch(path, cache_dir: ISeq.cache_dir) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/bootsnap/compile_cache/iseq.rb', line 115

def self.fetch(path, cache_dir: ISeq.cache_dir)
  compiler = compiler_selector&.call(path) || default_compiler

  # Having coverage enabled prevents iseq dumping/loading.
  if coverage_on?
    return nil if compiler.equal?(DEFAULT)

    if COVERAGE_SUPPORTED
      return compiler.input_to_output(File.read(path.to_s), path.to_s, nil)
    elsif !@coverage_support_warning_emitted
      @coverage_support_warning_emitted = true
      warn(<<~MSG)
        Using `Bootsnap.enable_frozen_string_literal` with code coverage enabled is only supported on Ruby 4.0.4+.
        Files loaded while coverage is on, will have mutable string literals.
      MSG
    end

    return nil
  end

  Bootsnap::CompileCache::Native.fetch(
    cache_dir,
    compiler.namespace,
    path.to_s,
    compiler,
    nil,
  )
end

.install!(cache_dir) ⇒ Object



187
188
189
190
191
192
193
194
195
196
197
# File 'lib/bootsnap/compile_cache/iseq.rb', line 187

def self.install!(cache_dir)
  Bootsnap::CompileCache::ISeq.cache_dir = cache_dir

  return unless supported?

  Bootsnap::CompileCache::ISeq.compile_option_updated

  class << RubyVM::InstructionSequence
    prepend(InstructionSequenceMixin)
  end
end

.precompile(path) ⇒ Object



144
145
146
147
148
149
150
151
152
# File 'lib/bootsnap/compile_cache/iseq.rb', line 144

def self.precompile(path)
  compiler = compiler_selector&.call(path) || default_compiler
  Bootsnap::CompileCache::Native.precompile(
    cache_dir,
    compiler.namespace,
    path.to_s,
    compiler,
  )
end

.supported?Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/bootsnap/compile_cache/iseq.rb', line 17

def supported?
  CompileCache.supported? && defined?(RubyVM)
end