Skip to content

Instantly share code, notes, and snippets.

@havenwood
Last active April 11, 2024 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save havenwood/414367192cf22f66d01a3117064713e7 to your computer and use it in GitHub Desktop.
Save havenwood/414367192cf22f66d01a3117064713e7 to your computer and use it in GitHub Desktop.
A spike showing compiling IR binaries for all gems
# frozen_string_literal: true
require 'fileutils'
require 'find'
require 'zlib'
binaries_dir = File.join Gem.dir, 'binaries'
gems_dir = File.join Gem.dir, 'gems'
Dir['*/', base: gems_dir].each do |gem|
gem_dir = File.join gems_dir, gem
gem_binary_dir = File.join binaries_dir, gem
FileUtils.mkdir_p gem_binary_dir
Find.find gem_dir do |path|
# Skip dotfile directories.
if File.directory? path
Find.prune if File.basename(path).start_with?('.')
next
end
next unless File.extname(path) == '.rb'
begin
binary = RubyVM::InstructionSequence.compile_file(path).to_binary
rescue SyntaxError
warn "Failed to compile: #{path}"
next
end
relative_path = path.delete_prefix(gem_dir)
binary_filename = "#{relative_path.chomp('.rb')}.yarb.gz"
binary_path = File.join gem_binary_dir, binary_filename
FileUtils.mkdir_p File.dirname binary_path
Zlib::GzipWriter.open(binary_path) { |gz| gz.write binary }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment