Skip to content

Instantly share code, notes, and snippets.

@havenwood havenwood/spike.rb
Created Sep 29, 2019

Embed
What would you like to do?
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'
binary = RubyVM::InstructionSequence.compile_file(path).to_binary
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
You can’t perform that action at this time.