From cbb56e30a4bc3d0f1446bdd3fb41f14a362e94fb Mon Sep 17 00:00:00 2001 From: tmm1 Date: Tue, 26 Nov 2013 23:30:25 +0000 Subject: * compile.c: Use rb_fstring() to de-duplicate string literals in code. [ruby-core:58599] [Bug #9159] [ruby-core:54405] * iseq.c (prepare_iseq_build): De-duplicate iseq labels and source locations. * re.c (rb_reg_initialize): Use rb_fstring() for regex string. * string.c (rb_fstring): Handle non-string and already-fstr arguments. * vm_eval.c (eval_string_with_cref): De-duplicate eval source filename. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@43866 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 11 +++++++++++ compile.c | 23 ++++++++++++----------- iseq.c | 6 ++++-- re.c | 3 +-- string.c | 5 +++++ vm_eval.c | 5 +++++ 6 files changed, 38 insertions(+), 15 deletions(-) diff --git a/ChangeLog b/ChangeLog index 6fbe4a1d75..eaca5c6698 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,14 @@ +Wed Nov 27 08:24:49 2013 Aman Gupta + + * compile.c: Use rb_fstring() to de-duplicate string literals in code. + [ruby-core:58599] [Bug #9159] [ruby-core:54405] + * iseq.c (prepare_iseq_build): De-duplicate iseq labels and source + locations. + * re.c (rb_reg_initialize): Use rb_fstring() for regex string. + * string.c (rb_fstring): Handle non-string and already-fstr arguments. + * vm_eval.c (eval_string_with_cref): De-duplicate eval source + filename. + Wed Nov 27 07:13:54 2013 Aaron Patterson * ext/psych/lib/psych.rb: psych version 2.0.2 diff --git a/compile.c b/compile.c index 2b58462d16..677eddeade 100644 --- a/compile.c +++ b/compile.c @@ -171,10 +171,10 @@ r_value(VALUE value) (((rb_iseq_t*)DATA_PTR(iseq))->location.absolute_path) #define NEW_ISEQVAL(node, name, type, line_no) \ - new_child_iseq(iseq, (node), (name), 0, (type), (line_no)) + new_child_iseq(iseq, (node), rb_fstring(name), 0, (type), (line_no)) #define NEW_CHILD_ISEQVAL(node, name, type, line_no) \ - new_child_iseq(iseq, (node), (name), iseq->self, (type), (line_no)) + new_child_iseq(iseq, (node), rb_fstring(name), iseq->self, (type), (line_no)) /* add instructions */ #define ADD_SEQ(seq1, seq2) \ @@ -2254,15 +2254,16 @@ compile_dstr_fragments(rb_iseq_t *iseq, LINK_ANCHOR *ret, NODE *node, int *cntp) debugp_param("nd_lit", lit); if (!NIL_P(lit)) { - hide_obj(lit); cnt++; + if (RB_TYPE_P(lit, T_STRING)) + lit = node->nd_lit = rb_fstring(node->nd_lit); ADD_INSN1(ret, nd_line(node), putobject, lit); } while (list) { node = list->nd_head; if (nd_type(node) == NODE_STR) { - hide_obj(node->nd_lit); + node->nd_lit = rb_fstring(node->nd_lit); ADD_INSN1(ret, nd_line(node), putobject, node->nd_lit); } else { @@ -2515,7 +2516,7 @@ case_when_optimizable_literal(NODE * node) break; } case NODE_STR: - return node->nd_lit; + return node->nd_lit = rb_fstring(node->nd_lit); } return Qundef; } @@ -2542,8 +2543,8 @@ when_vals(rb_iseq_t *iseq, LINK_ANCHOR *cond_seq, NODE *vals, LABEL *l1, int onl ADD_INSN(cond_seq, nd_line(val), dup); /* dup target */ if (nd_type(val) == NODE_STR) { + val->nd_lit = rb_fstring(val->nd_lit); debugp_param("nd_lit", val->nd_lit); - OBJ_FREEZE(val->nd_lit); ADD_INSN1(cond_seq, nd_line(val), putobject, val->nd_lit); } else { @@ -4798,9 +4799,9 @@ iseq_compile_each(rb_iseq_t *iseq, LINK_ANCHOR *ret, NODE * node, int poped) break; } case NODE_STR:{ + node->nd_lit = rb_fstring(node->nd_lit); debugp_param("nd_lit", node->nd_lit); if (!poped) { - OBJ_FREEZE(node->nd_lit); ADD_INSN1(ret, line, putstring, node->nd_lit); } break; @@ -4814,7 +4815,7 @@ iseq_compile_each(rb_iseq_t *iseq, LINK_ANCHOR *ret, NODE * node, int poped) break; } case NODE_XSTR:{ - OBJ_FREEZE(node->nd_lit); + node->nd_lit = rb_fstring(node->nd_lit); ADD_CALL_RECEIVER(ret, line); ADD_INSN1(ret, line, putobject, node->nd_lit); ADD_CALL(ret, line, ID2SYM(idBackquote), INT2FIX(1)); @@ -4908,7 +4909,7 @@ iseq_compile_each(rb_iseq_t *iseq, LINK_ANCHOR *ret, NODE * node, int poped) } case NODE_DEFN:{ VALUE iseqval = NEW_ISEQVAL(node->nd_defn, - rb_str_dup(rb_id2str(node->nd_mid)), + rb_id2str(node->nd_mid), ISEQ_TYPE_METHOD, line); debugp_param("defn/iseq", iseqval); @@ -4928,7 +4929,7 @@ iseq_compile_each(rb_iseq_t *iseq, LINK_ANCHOR *ret, NODE * node, int poped) } case NODE_DEFS:{ VALUE iseqval = NEW_ISEQVAL(node->nd_defn, - rb_str_dup(rb_id2str(node->nd_mid)), + rb_id2str(node->nd_mid), ISEQ_TYPE_METHOD, line); debugp_param("defs/iseq", iseqval); @@ -5530,7 +5531,7 @@ rb_insns_name_array(void) VALUE ary = rb_ary_new(); int i; for (i = 0; i < numberof(insn_name_info); i++) { - rb_ary_push(ary, rb_obj_freeze(rb_str_new2(insn_name_info[i]))); + rb_ary_push(ary, rb_fstring(rb_str_new2(insn_name_info[i]))); } return rb_obj_freeze(ary); } diff --git a/iseq.c b/iseq.c index 2db1d3be56..df1c16132f 100644 --- a/iseq.c +++ b/iseq.c @@ -264,8 +264,10 @@ prepare_iseq_build(rb_iseq_t *iseq, OBJ_WRITE(iseq->self, &iseq->klass, 0); set_relation(iseq, parent); - OBJ_FREEZE(name); - OBJ_FREEZE(path); + name = rb_fstring(name); + path = rb_fstring(path); + if (RTEST(absolute_path)) + absolute_path = rb_fstring(absolute_path); iseq_location_setup(iseq, path, absolute_path, name, first_lineno); if (iseq != iseq->local_iseq) { diff --git a/re.c b/re.c index a7074d8282..f69eb09b10 100644 --- a/re.c +++ b/re.c @@ -2460,8 +2460,7 @@ rb_reg_initialize(VALUE obj, const char *s, long len, rb_encoding *enc, options & ARG_REG_OPTION_MASK, err, sourcefile, sourceline); if (!re->ptr) return -1; - OBJ_WRITE(obj, &re->src, rb_enc_str_new(s, len, enc)); - OBJ_FREEZE(re->src); + OBJ_WRITE(obj, &re->src, rb_fstring(rb_enc_str_new(s, len, enc))); RB_GC_GUARD(unescaped); return 0; } diff --git a/string.c b/string.c index fa5d2990fb..c6c1497ea4 100644 --- a/string.c +++ b/string.c @@ -136,6 +136,11 @@ VALUE rb_fstring(VALUE str) { st_data_t fstr; + Check_Type(str, T_STRING); + + if (FL_TEST(str, RSTRING_FSTR)) + return str; + if (st_lookup(frozen_strings, (st_data_t)str, &fstr)) { str = (VALUE)fstr; /* because of lazy sweep, str may be unmaked already and swept diff --git a/vm_eval.c b/vm_eval.c index 24bac12bbf..6c248e4eb8 100644 --- a/vm_eval.c +++ b/vm_eval.c @@ -1239,6 +1239,11 @@ eval_string_with_cref(VALUE self, VALUE src, VALUE scope, NODE *const cref_arg, fname = rb_usascii_str_new_cstr("(eval)"); } + if (RTEST(fname)) + fname = rb_fstring(fname); + if (RTEST(absolute_path)) + absolute_path = rb_fstring(absolute_path); + /* make eval iseq */ th->parse_in_eval++; th->mild_compile_error++; -- cgit v1.2.3