Skip to content

Commit

Permalink
* compile.c: Use rb_fstring() to de-duplicate string literals in code…
Browse files Browse the repository at this point in the history
…. [ruby-core:58599] [Bug ruby#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
  • Loading branch information
tmm1 committed Nov 26, 2013
1 parent 599c3a9 commit cbb56e3
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 15 deletions.
11 changes: 11 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
Wed Nov 27 08:24:49 2013 Aman Gupta <ruby@tmm1.net>

* 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 <aaron@tenderlovemaking.com>

* ext/psych/lib/psych.rb: psych version 2.0.2
Expand Down
23 changes: 12 additions & 11 deletions compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) \
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
}
Expand All @@ -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 {
Expand Down Expand Up @@ -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;
Expand All @@ -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));
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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);
}
Expand Down
6 changes: 4 additions & 2 deletions iseq.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
3 changes: 1 addition & 2 deletions re.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
5 changes: 5 additions & 0 deletions string.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions vm_eval.c
Original file line number Diff line number Diff line change
Expand Up @@ -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++;
Expand Down

0 comments on commit cbb56e3

Please sign in to comment.