Meetup #70
monochrome
fn add(jit: &mut monoasm::JitMemory, lhs: u64, rhs: u64) -> monoasm::DestLabel {
let overflow = jit.label();
monoasm! { jit,
subq R(lhs), 1;
addq R(lhs), R(rhs);
jo overflow;
}
overflow
}
improvement in monoasm
fn overflow(jit: &mut JitMemory, overflow: DestLabel, deopt: DestLabel) {
monoasm! {jit,
overflow:
movq rdi, (Value::symbol_from_str("_arith_overflow").id());
jmp deopt;
}
}
Problem
0f
80
xx
xx
xx
xx
label:
calculate displacement
Fixup
Solution
pub struct DestLabel(std::rc::Rc<std::cell::RefCell<LabelInfo>>);
optimization in monoruby:specialization
Integer#times
1000.times do |i|
puts i
end
class Integer
def times
..
end
end
do |i|
puts i
end
100.times do |i,j|
i+j
end
do |i,j|
i+j
end
100.times do |*i|
i
end
do |*i|
i
end
class Integer
def times
..
end
end
optimization in monoruby:specialization
1000.times do |i|
puts i
end
do |i|
puts i
end
def times
unless block_given?
return self.to_enum(:times)
end
i = 0
while i < self
yield i
i += 1
end
self
end
optimization in monoruby:specialization
1000.times do |i|
puts i
end
do |i|
puts i
end
def times
unless block_given?
return self.to_enum(:times)
end
i = 0
while i < 1000
yield i
i += 1
end
1000
end
self == 100
block_given? == true
optimization in monoruby:specialization
BEFORE
| | 3.4.1 --yjit| monoruby| monoruby --no-jit|
|:----------------------|------------:|------------:|-----------------:|
|integer_times | 76.192k| 138.327k| 108.395k|
|integer_step | 59.779k| 132.380k| 102.997k|
|array_each | 31.639k| 169.738k| 129.978k|
|array_map | 26.003k| 135.393k| 107.073k|
|array_map_ | 26.527k| 133.081k| 105.664k|
|array_each_with_index | 25.428k| 56.232k| 58.778k|
AFTER
| | 3.4.1 --yjit| monoruby| monoruby --no-jit|
|:----------------------|------------:|------------:|-----------------:|
|integer_times | 83.943k| 439.358k| 48.489k|
|integer_step | 69.005k| 439.353k| 47.679k|
|array_each | 35.721k| 185.300k| 40.710k|
|array_map | 29.547k| 131.570k| 30.951k|
|array_map_ | 29.705k| 165.150k| 32.933k|
|array_each_with_index | 27.931k| 182.880k| 26.492k|
optimization in monoruby:specialization
Class#new
class Class
def new(...)
o = allocate
o.initialize(...) if o.respond_to?(:initialize)
o
end
end