Skip to content

Commit 5688434

Browse files
committed
Testing out an optimized respond_to? instruction
1 parent c936699 commit 5688434

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed

compile.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3482,6 +3482,29 @@ iseq_peephole_optimize(rb_iseq_t *iseq, LINK_ELEMENT *list, const int do_tailcal
34823482
}
34833483
}
34843484

3485+
if (IS_INSN_ID(iobj, send)) {
3486+
const struct rb_callinfo *ci = (struct rb_callinfo *)OPERAND_AT(iobj, 0);
3487+
const rb_iseq_t *blockiseq = (rb_iseq_t *)OPERAND_AT(iobj, 1);
3488+
3489+
// == disasm: #<ISeq:<main>@../test.rb:2 (2,0)-(2,76)>
3490+
// 0000 getglobal :$stdout ( 2)[Li]
3491+
// 0002 putobject :write
3492+
// 0004 opt_send_without_block <calldata!mid:respond_to?, argc:1, ARGS_SIMPLE>
3493+
// 0006 branchunless 14
3494+
// 0008 putself
3495+
// 0009 putchilledstring "Did you know you can write to $stdout?"
3496+
// 0011 opt_send_without_block <calldata!mid:puts, argc:1, FCALL|ARGS_SIMPLE>
3497+
// 0013 leave
3498+
// 0014 putnil
3499+
// 0015 leave
3500+
if (vm_ci_simple(ci) && vm_ci_argc(ci) == 1 && blockiseq == NULL && vm_ci_mid(ci) == idRespond_to) {
3501+
iobj->insn_id = BIN(opt_respond_to);
3502+
iobj->operand_size = 1;
3503+
iobj->operands = compile_data_calloc2(iseq, iobj->operand_size, sizeof(VALUE));
3504+
iobj->operands[0] = (VALUE)ci;
3505+
}
3506+
}
3507+
34853508
/*
34863509
* newhash 0
34873510
* send <calldata!mid:freeze, argc:0, ARGS_SIMPLE>, nil

insns.def

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -949,6 +949,16 @@ opt_hash_freeze
949949
}
950950
}
951951

952+
DEFINE_INSN
953+
opt_respond_to
954+
(CALL_DATA cd)
955+
(VALUE recv, VALUE mid)
956+
(VALUE val)
957+
{
958+
val = vm_opt_respond_to(recv, mid);
959+
CALL_SIMPLE_METHOD();
960+
}
961+
952962
DEFINE_INSN
953963
opt_str_freeze
954964
(VALUE str, CALL_DATA cd)

vm_insnhelper.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6133,6 +6133,18 @@ vm_opt_ary_freeze(VALUE ary, int bop, ID id)
61336133
}
61346134
}
61356135

6136+
static VALUE
6137+
vm_opt_respond_to(VALUE recv, VALUE mid)
6138+
{
6139+
if (SYMBOL_P(mid)) {
6140+
printf("symbol:");
6141+
} else if (STRING_P(mid)) {
6142+
printf("string:");
6143+
}
6144+
printf("%s\n", rb_builtin_type_name(TYPE(recv)));
6145+
return Qundef;
6146+
}
6147+
61366148
static VALUE
61376149
vm_opt_hash_freeze(VALUE hash, int bop, ID id)
61386150
{

0 commit comments

Comments
 (0)