Skip to content

Instantly share code, notes, and snippets.

View eregon's full-sized avatar

Benoit Daloze eregon

View GitHub Profile
@eregon
eregon / 0_core_methods_in_ruby.md
Last active January 30, 2024 23:15
Core library methods defined with Ruby code and with a #source_location file starting with `<internal:`, by Ruby implementation and version
Ruby Core methods written in Ruby Total number of core methods % written in Ruby
CRuby 2.3 3 1637 0.2%
CRuby 2.4 3 1636 0.2%
CRuby 2.5 6 1680 0.4%
CRuby 2.6 5 1767 0.3%
CRuby 2.7 38 1802 2.1%
CRuby 3.0 89 1830 4.9%
CRuby 3.1 112 1884 5.9%
CRuby 3.2 128 1924 6.7%
@eregon
eregon / ruby_parsers_list.md
Last active June 28, 2022 10:52
A list of Ruby parsers
@eregon
eregon / inline_cext.rb
Created January 28, 2021 14:15
Mixing Ruby code and C extension code in a single file
require 'tmpdir'
require 'rbconfig'
def inline_c_extension(c_code)
Dir.mktmpdir('inline_c_extension') do |dir|
File.write("#{dir}/cext.c", c_code)
File.write("#{dir}/extconf.rb", <<~RUBY)
require 'mkmf'
create_makefile('cext')
RUBY
def Process.rss
Integer(`ps -o rss= -p #{Process.pid}`) * 1024
end
def mb(bytes)
return '?' if bytes == nil or bytes < 0
mb = bytes / 1024.0 / 1024.0
"#{mb.to_i} MB"
end

immutable = deeply frozen

Immutable classes

Immutable core classes

  • TrueClass
  • FalseClass
  • NilClass
  • Integer
command = ARGV
start = Process.clock_gettime Process::CLOCK_MONOTONIC, :millisecond
IO.popen(command, err: [:child, :out]) do |io|
while line = io.gets
now = Process.clock_gettime Process::CLOCK_MONOTONIC, :millisecond
puts "[#{"%5d" % (now - start)}] #{line}"
end
end
@eregon
eregon / _table.md
Last active October 31, 2020 15:04
Benchmark results on the benchmark from https://chrisseaton.com/truffleruby/ruby-stm/
Ruby Implementation Speedup relative to 2.7.2
Ruby 2.7.2 1.00x
Ruby 2.7.2 --jit 1.16x
Ruby 3 dev 0.81x
Ruby 3 dev --jit 1.08x
TruffleRuby CE 20.2 --jvm 19.73x
TruffleRuby EE 20.2 --jvm 31.54x

The median i/s result out of 3 is used to compute speedups.

@eregon
eregon / benchmark-block
Last active September 19, 2020 14:07
benchmark block - a prototype to benchmark a block without the block overhead
#!/usr/bin/env ruby
private def benchmark(name = nil, &block)
raise "needs a block" unless block
binding = block.binding
file, line = block.source_location
start_line = line - 1
lines = File.readlines(file)
indent = lines.fetch(start_line)[/^(\s+)/, 1]
I added `x.compare!` in bin/benchmark
$ cd ext
$ rake
$ cd ..
$ gem install ffi-compiler benchmark-ips
$ ruby -Ilib bin/benchmark
ruby 2.6.6p146 (2020-03-31 revision 67876) [x86_64-linux]
@eregon
eregon / truffle-material.md
Last active March 16, 2020 10:07 — forked from smarr/truffle-material.md
Truffle: Languages and Material