Skip to content

Instantly share code, notes, and snippets.

@adam12
Forked from havenwood/spike.rb
Created April 11, 2024 14:02
Show Gist options
  • Save adam12/0198c53f7365f2a095e5399b81758704 to your computer and use it in GitHub Desktop.
Save adam12/0198c53f7365f2a095e5399b81758704 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