Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions numeric.rb
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,44 @@ def denominator
end

with_jit do
if Primitive.rb_builtin_basic_definition_p(:step)
undef :step

def step(*args)
Primitive.attr! :inline_block, :c_trace

unless defined?(yield) &&
Primitive.cexpr!('RBOOL(RARRAY_LEN(args) == 2)')
return super
end

_to, _by = args
unless Primitive.cexpr! 'RBOOL(FIXNUM_P(self) && FIXNUM_P(_to) && FIXNUM_P(_by) && FIX2LONG(_by) != 0)'
return super
end

from = self
if Primitive.cexpr! 'RBOOL(FIX2LONG(_by) > 0)'
while Primitive.cexpr! 'RBOOL(FIX2LONG(from) <= FIX2LONG(_to))'
yield from
break if Primitive.cexpr! 'RBOOL(FIX2LONG(_to) - FIX2LONG(from) < FIX2LONG(_by))'

from = Primitive.cexpr! 'LONG2FIX(FIX2LONG(from) + FIX2LONG(_by))'
end
else
while Primitive.cexpr! 'RBOOL(FIX2LONG(from) >= FIX2LONG(_to))'
yield from
break if Primitive.cexpr! 'RBOOL(FIX2LONG(from) - FIX2LONG(_to) < -FIX2LONG(_by))'

from = Primitive.cexpr! 'LONG2FIX(FIX2LONG(from) + FIX2LONG(_by))'
end
end
self
end

ruby2_keywords :step # Preserve keyword arguments when falling back to super.
end

if Primitive.rb_builtin_basic_definition_p(:downto)
undef :downto

Expand Down
19 changes: 19 additions & 0 deletions zjit/src/codegen_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1002,6 +1002,25 @@ fn test_yield_inline_self_is_captured_self() {
assert_snapshot!(assert_compiles("C.new(21).go(Y)"), @"42");
}

#[test]
fn test_integer_step_fixnum_fastpath() {
with_inlining_threshold(200, || {
eval(r#"
def sum_steps(from, to, by)
sum = 0
from.step(to, by) { |i| sum += i }
sum
end
"#);

assert_snapshot!(assert_inlines(r#"
[sum_steps(1, 9, 2), sum_steps(9, 1, -2), sum_steps(2, 1, 3), sum_steps(1, 9, 2)]
"#), @"[25, 25, 0, 25]");
let sum_iseq = get_method_iseq("self", "sum_steps");
assert!(!get_or_create_iseq_payload(sum_iseq).versions.is_empty(), "expected sum_steps to be compiled with Integer#step");
});
}

#[test]
fn test_yield_iseq_guard_miss_recompiles() {
set_call_threshold(2);
Expand Down
Loading