Skip to content

Instantly share code, notes, and snippets.

Avatar

Benoit Daloze eregon

View GitHub Profile
View use_memory.rb
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
View immutable_classes.md

immutable = deeply frozen

Immutable classes

Immutable core classes

  • TrueClass
  • FalseClass
  • NilClass
  • Integer
View time_output.rb
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 / inline_cext.rb
Created Jan 28, 2021
Mixing Ruby code and C extension code in a single file
View inline_cext.rb
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
@eregon
eregon / _table.md
Last active Oct 31, 2020
Benchmark results on the benchmark from https://chrisseaton.com/truffleruby/ruby-stm/
View _table.md
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 / 2.3.txt
Last active Mar 28, 2022
Core library methods defined in Ruby and with a #source_location file starting with `<internal`, by Ruby implementation and version
View 2.3.txt
$ ruby -ve 'puts ObjectSpace.each_object(Module).flat_map { |mod| mod.instance_methods(false).map { |name| mod.instance_method(name) }.select { |meth| meth.source_location && meth.source_location[0].start_with?("<internal:") }.map { |meth| "#{meth} #{meth.source_location.join(":")}" } }'
ruby 2.3.8p459 (2018-10-18 revision 65136) [x86_64-linux]
#<UnboundMethod: #<Class:Thread>#exclusive> <internal:prelude>:12
#<UnboundMethod: IO#read_nonblock> <internal:prelude>:75
#<UnboundMethod: IO#write_nonblock> <internal:prelude>:133
View results.txt
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 / benchmark-block
Last active Sep 19, 2020
benchmark block - a prototype to benchmark a block without the block overhead
View benchmark-block
#!/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]
@eregon
eregon / truffle-material.md
Last active Mar 16, 2020 — forked from smarr/truffle-material.md
Truffle: Languages and Material
View truffle-material.md
View diff.diff
--- full.txt 2020-03-05 17:04:32.230794000 +0900
+++ short.txt 2020-03-05 17:04:38.445322000 +0900
@@ -1,179 +1,3 @@
-1.8.5-p52
-1.8.5-p113
-1.8.5-p114
-1.8.5-p115
-1.8.5-p231
-1.8.6
-1.8.6-p36