summaryrefslogtreecommitdiff
path: root/compile.c
diff options
context:
space:
mode:
authortmm1 <tmm1@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2014-01-10 04:54:08 +0000
committertmm1 <tmm1@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2014-01-10 04:54:08 +0000
commit58f800a278b8b70463f4afdbb23a918d8ab441ff (patch)
treed7b70525c3d84e8a4f7e3792bc06a5962481d497 /compile.c
parent9bfaeffd53d4e17b0a24281e244e2483f28f5827 (diff)
insns.def: add opt path for Hash#[] and Hash#[]= used with str literal keys
* insns.def (opt_aref_with): new instruction to optimize Hash#[], removing any allocation overhead when used with a string literal key. Patch by normalperson (Eric Wong). [ruby-core:59640] [Bug #9382] * insns.def (opt_aset_with): new instruction to optimize Hash#[]= * compile.c (iseq_compile_each): compiler shortcuts for new instructions * hash.c (static VALUE rb_hash_compare_by_id_p): fix documentation for Hash#compare_by_identity to reflect frozen string sharing * test/ruby/test_hash.rb (class TestHash): test for new behavior git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@44551 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'compile.c')
-rw-r--r--compile.c39
1 files changed, 39 insertions, 0 deletions
diff --git a/compile.c b/compile.c
index 5b28401..849d4a5 100644
--- a/compile.c
+++ b/compile.c
@@ -4319,6 +4319,9 @@ iseq_compile_each(rb_iseq_t *iseq, LINK_ANCHOR *ret, NODE * node, int poped)
break;
}
case NODE_CALL:
+ /* optimization shortcut
+ * "literal".freeze -> opt_str_freeze("literal")
+ */
if (node->nd_recv && nd_type(node->nd_recv) == NODE_STR &&
node->nd_mid == idFreeze && node->nd_args == NULL)
{
@@ -4330,6 +4333,23 @@ iseq_compile_each(rb_iseq_t *iseq, LINK_ANCHOR *ret, NODE * node, int poped)
}
break;
}
+ /* optimization shortcut
+ * obj["literal"] -> opt_aref_with(obj, "literal")
+ */
+ if (node->nd_mid == idAREF && node->nd_recv != (NODE *)1 && node->nd_args &&
+ nd_type(node->nd_args) == NODE_ARRAY && node->nd_args->nd_alen == 1 &&
+ nd_type(node->nd_args->nd_head) == NODE_STR)
+ {
+ VALUE str = rb_fstring(node->nd_args->nd_head->nd_lit);
+ node->nd_args->nd_head->nd_lit = str;
+ COMPILE(ret, "recv", node->nd_recv);
+ ADD_INSN2(ret, line, opt_aref_with,
+ new_callinfo(iseq, idAREF, 1, 0, 0), str);
+ if (poped) {
+ ADD_INSN(ret, line, pop);
+ }
+ break;
+ }
case NODE_FCALL:
case NODE_VCALL:{ /* VCALL: variable or call */
/*
@@ -5300,6 +5320,25 @@ iseq_compile_each(rb_iseq_t *iseq, LINK_ANCHOR *ret, NODE * node, int poped)
VALUE flag = 0;
VALUE argc;
+ /* optimization shortcut
+ * obj["literal"] = value -> opt_aset_with(obj, "literal", value)
+ */
+ if (node->nd_mid == idASET && node->nd_recv != (NODE *)1 && node->nd_args &&
+ nd_type(node->nd_args) == NODE_ARRAY && node->nd_args->nd_alen == 2 &&
+ nd_type(node->nd_args->nd_head) == NODE_STR)
+ {
+ VALUE str = rb_fstring(node->nd_args->nd_head->nd_lit);
+ node->nd_args->nd_head->nd_lit = str;
+ COMPILE(ret, "recv", node->nd_recv);
+ COMPILE(ret, "value", node->nd_args->nd_next->nd_head);
+ ADD_INSN2(ret, line, opt_aset_with,
+ new_callinfo(iseq, idASET, 2, 0, 0), str);
+ if (poped) {
+ ADD_INSN(ret, line, pop);
+ }
+ break;
+ }
+
INIT_ANCHOR(recv);
INIT_ANCHOR(args);
argc = setup_args(iseq, args, node->nd_args, &flag);