Skip to content

Commit cbb56e3

Browse files
committed
* compile.c: Use rb_fstring() to de-duplicate string literals in code. [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
1 parent 599c3a9 commit cbb56e3

File tree

6 files changed

+38
-15
lines changed

6 files changed

+38
-15
lines changed

ChangeLog

+11
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
Wed Nov 27 08:24:49 2013 Aman Gupta <ruby@tmm1.net>
2+
3+
* compile.c: Use rb_fstring() to de-duplicate string literals in code.
4+
[ruby-core:58599] [Bug #9159] [ruby-core:54405]
5+
* iseq.c (prepare_iseq_build): De-duplicate iseq labels and source
6+
locations.
7+
* re.c (rb_reg_initialize): Use rb_fstring() for regex string.
8+
* string.c (rb_fstring): Handle non-string and already-fstr arguments.
9+
* vm_eval.c (eval_string_with_cref): De-duplicate eval source
10+
filename.
11+
112
Wed Nov 27 07:13:54 2013 Aaron Patterson <aaron@tenderlovemaking.com>
213

314
* ext/psych/lib/psych.rb: psych version 2.0.2

compile.c

+12-11
Original file line numberDiff line numberDiff line change
@@ -171,10 +171,10 @@ r_value(VALUE value)
171171
(((rb_iseq_t*)DATA_PTR(iseq))->location.absolute_path)
172172

173173
#define NEW_ISEQVAL(node, name, type, line_no) \
174-
new_child_iseq(iseq, (node), (name), 0, (type), (line_no))
174+
new_child_iseq(iseq, (node), rb_fstring(name), 0, (type), (line_no))
175175

176176
#define NEW_CHILD_ISEQVAL(node, name, type, line_no) \
177-
new_child_iseq(iseq, (node), (name), iseq->self, (type), (line_no))
177+
new_child_iseq(iseq, (node), rb_fstring(name), iseq->self, (type), (line_no))
178178

179179
/* add instructions */
180180
#define ADD_SEQ(seq1, seq2) \
@@ -2254,15 +2254,16 @@ compile_dstr_fragments(rb_iseq_t *iseq, LINK_ANCHOR *ret, NODE *node, int *cntp)
22542254

22552255
debugp_param("nd_lit", lit);
22562256
if (!NIL_P(lit)) {
2257-
hide_obj(lit);
22582257
cnt++;
2258+
if (RB_TYPE_P(lit, T_STRING))
2259+
lit = node->nd_lit = rb_fstring(node->nd_lit);
22592260
ADD_INSN1(ret, nd_line(node), putobject, lit);
22602261
}
22612262

22622263
while (list) {
22632264
node = list->nd_head;
22642265
if (nd_type(node) == NODE_STR) {
2265-
hide_obj(node->nd_lit);
2266+
node->nd_lit = rb_fstring(node->nd_lit);
22662267
ADD_INSN1(ret, nd_line(node), putobject, node->nd_lit);
22672268
}
22682269
else {
@@ -2515,7 +2516,7 @@ case_when_optimizable_literal(NODE * node)
25152516
break;
25162517
}
25172518
case NODE_STR:
2518-
return node->nd_lit;
2519+
return node->nd_lit = rb_fstring(node->nd_lit);
25192520
}
25202521
return Qundef;
25212522
}
@@ -2542,8 +2543,8 @@ when_vals(rb_iseq_t *iseq, LINK_ANCHOR *cond_seq, NODE *vals, LABEL *l1, int onl
25422543
ADD_INSN(cond_seq, nd_line(val), dup); /* dup target */
25432544

25442545
if (nd_type(val) == NODE_STR) {
2546+
val->nd_lit = rb_fstring(val->nd_lit);
25452547
debugp_param("nd_lit", val->nd_lit);
2546-
OBJ_FREEZE(val->nd_lit);
25472548
ADD_INSN1(cond_seq, nd_line(val), putobject, val->nd_lit);
25482549
}
25492550
else {
@@ -4798,9 +4799,9 @@ iseq_compile_each(rb_iseq_t *iseq, LINK_ANCHOR *ret, NODE * node, int poped)
47984799
break;
47994800
}
48004801
case NODE_STR:{
4802+
node->nd_lit = rb_fstring(node->nd_lit);
48014803
debugp_param("nd_lit", node->nd_lit);
48024804
if (!poped) {
4803-
OBJ_FREEZE(node->nd_lit);
48044805
ADD_INSN1(ret, line, putstring, node->nd_lit);
48054806
}
48064807
break;
@@ -4814,7 +4815,7 @@ iseq_compile_each(rb_iseq_t *iseq, LINK_ANCHOR *ret, NODE * node, int poped)
48144815
break;
48154816
}
48164817
case NODE_XSTR:{
4817-
OBJ_FREEZE(node->nd_lit);
4818+
node->nd_lit = rb_fstring(node->nd_lit);
48184819
ADD_CALL_RECEIVER(ret, line);
48194820
ADD_INSN1(ret, line, putobject, node->nd_lit);
48204821
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)
49084909
}
49094910
case NODE_DEFN:{
49104911
VALUE iseqval = NEW_ISEQVAL(node->nd_defn,
4911-
rb_str_dup(rb_id2str(node->nd_mid)),
4912+
rb_id2str(node->nd_mid),
49124913
ISEQ_TYPE_METHOD, line);
49134914

49144915
debugp_param("defn/iseq", iseqval);
@@ -4928,7 +4929,7 @@ iseq_compile_each(rb_iseq_t *iseq, LINK_ANCHOR *ret, NODE * node, int poped)
49284929
}
49294930
case NODE_DEFS:{
49304931
VALUE iseqval = NEW_ISEQVAL(node->nd_defn,
4931-
rb_str_dup(rb_id2str(node->nd_mid)),
4932+
rb_id2str(node->nd_mid),
49324933
ISEQ_TYPE_METHOD, line);
49334934

49344935
debugp_param("defs/iseq", iseqval);
@@ -5530,7 +5531,7 @@ rb_insns_name_array(void)
55305531
VALUE ary = rb_ary_new();
55315532
int i;
55325533
for (i = 0; i < numberof(insn_name_info); i++) {
5533-
rb_ary_push(ary, rb_obj_freeze(rb_str_new2(insn_name_info[i])));
5534+
rb_ary_push(ary, rb_fstring(rb_str_new2(insn_name_info[i])));
55345535
}
55355536
return rb_obj_freeze(ary);
55365537
}

iseq.c

+4-2
Original file line numberDiff line numberDiff line change
@@ -264,8 +264,10 @@ prepare_iseq_build(rb_iseq_t *iseq,
264264
OBJ_WRITE(iseq->self, &iseq->klass, 0);
265265
set_relation(iseq, parent);
266266

267-
OBJ_FREEZE(name);
268-
OBJ_FREEZE(path);
267+
name = rb_fstring(name);
268+
path = rb_fstring(path);
269+
if (RTEST(absolute_path))
270+
absolute_path = rb_fstring(absolute_path);
269271

270272
iseq_location_setup(iseq, path, absolute_path, name, first_lineno);
271273
if (iseq != iseq->local_iseq) {

re.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -2460,8 +2460,7 @@ rb_reg_initialize(VALUE obj, const char *s, long len, rb_encoding *enc,
24602460
options & ARG_REG_OPTION_MASK, err,
24612461
sourcefile, sourceline);
24622462
if (!re->ptr) return -1;
2463-
OBJ_WRITE(obj, &re->src, rb_enc_str_new(s, len, enc));
2464-
OBJ_FREEZE(re->src);
2463+
OBJ_WRITE(obj, &re->src, rb_fstring(rb_enc_str_new(s, len, enc)));
24652464
RB_GC_GUARD(unescaped);
24662465
return 0;
24672466
}

string.c

+5
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,11 @@ VALUE
136136
rb_fstring(VALUE str)
137137
{
138138
st_data_t fstr;
139+
Check_Type(str, T_STRING);
140+
141+
if (FL_TEST(str, RSTRING_FSTR))
142+
return str;
143+
139144
if (st_lookup(frozen_strings, (st_data_t)str, &fstr)) {
140145
str = (VALUE)fstr;
141146
/* because of lazy sweep, str may be unmaked already and swept

vm_eval.c

+5
Original file line numberDiff line numberDiff line change
@@ -1239,6 +1239,11 @@ eval_string_with_cref(VALUE self, VALUE src, VALUE scope, NODE *const cref_arg,
12391239
fname = rb_usascii_str_new_cstr("(eval)");
12401240
}
12411241

1242+
if (RTEST(fname))
1243+
fname = rb_fstring(fname);
1244+
if (RTEST(absolute_path))
1245+
absolute_path = rb_fstring(absolute_path);
1246+
12421247
/* make eval iseq */
12431248
th->parse_in_eval++;
12441249
th->mild_compile_error++;

0 commit comments

Comments
 (0)