Skip to content

Commit 07ac587

Browse files
author
charliesome
committed
* compile.c (iseq_compile_each): emit opt_str_freeze if the #freeze
method is called on a static string literal with no arguments. * defs/id.def (firstline): add freeze so idFreeze is available * insns.def (opt_str_freeze): add opt_str_freeze instruction which pushes a frozen string literal without allocating a new object if String#freeze is not overriden * string.c (Init_String): define String#freeze * vm.c (vm_init_redefined_flag): define BOP_FREEZE on String class as a basic operation * vm_insnhelper.h: ditto git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@43627 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
1 parent d6f5e30 commit 07ac587

File tree

7 files changed

+49
-0
lines changed

7 files changed

+49
-0
lines changed

ChangeLog

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,23 @@
1+
Sun Nov 10 06:14:39 2013 Charlie Somerville <[email protected]>
2+
3+
* compile.c (iseq_compile_each): emit opt_str_freeze if the #freeze
4+
method is called on a static string literal with no arguments.
5+
6+
* defs/id.def (firstline): add freeze so idFreeze is available
7+
8+
* insns.def (opt_str_freeze): add opt_str_freeze instruction which
9+
pushes a frozen string literal without allocating a new object if
10+
String#freeze is not overriden
11+
12+
* string.c (Init_String): define String#freeze
13+
14+
* vm.c (vm_init_redefined_flag): define BOP_FREEZE on String class as
15+
a basic operation
16+
17+
* vm_insnhelper.h: ditto
18+
19+
[Feature #8992] [ruby-core:57705]
20+
121
Sun Nov 10 01:34:14 2013 Koichi Sasada <[email protected]>
222

323
* gc.c (vm_malloc_increase): sweep immediately on GC due to malloc().

compile.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4314,6 +4314,17 @@ iseq_compile_each(rb_iseq_t *iseq, LINK_ANCHOR *ret, NODE * node, int poped)
43144314
break;
43154315
}
43164316
case NODE_CALL:
4317+
if (node->nd_recv && nd_type(node->nd_recv) == NODE_STR &&
4318+
node->nd_mid == idFreeze && node->nd_args == NULL)
4319+
{
4320+
VALUE str = rb_fstring(node->nd_recv->nd_lit);
4321+
iseq_add_mark_object(iseq, str);
4322+
ADD_INSN1(ret, line, opt_str_freeze, str);
4323+
if (poped) {
4324+
ADD_INSN(ret, line, pop);
4325+
}
4326+
break;
4327+
}
43174328
case NODE_FCALL:
43184329
case NODE_VCALL:{ /* VCALL: variable or call */
43194330
/*

defs/id.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# -*- mode: ruby; coding: us-ascii -*-
22
firstline, predefined = __LINE__+1, %[\
3+
freeze
34
inspect
45
intern
56
object_id

insns.def

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -999,6 +999,20 @@ send
999999
CALL_METHOD(ci);
10001000
}
10011001

1002+
DEFINE_INSN
1003+
opt_str_freeze
1004+
(VALUE str)
1005+
()
1006+
(VALUE val)
1007+
{
1008+
if (BASIC_OP_UNREDEFINED_P(BOP_FREEZE, STRING_REDEFINED_OP_FLAG)) {
1009+
val = str;
1010+
}
1011+
else {
1012+
val = rb_funcall(rb_str_resurrect(str), idFreeze, 0);
1013+
}
1014+
}
1015+
10021016
/**
10031017
@c optimize
10041018
@e Invoke method without block, splat

string.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8745,6 +8745,7 @@ Init_String(void)
87458745
rb_define_method(rb_cString, "byteslice", rb_str_byteslice, -1);
87468746
rb_define_method(rb_cString, "scrub", str_scrub, -1);
87478747
rb_define_method(rb_cString, "scrub!", str_scrub_bang, -1);
8748+
rb_define_method(rb_cString, "freeze", rb_obj_freeze, 0);
87488749

87498750
rb_define_method(rb_cString, "to_i", rb_str_to_i, -1);
87508751
rb_define_method(rb_cString, "to_f", rb_str_to_f, 0);

vm.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1082,6 +1082,7 @@ vm_init_redefined_flag(void)
10821082
OP(EmptyP, EMPTY_P), (C(Array), C(String), C(Hash));
10831083
OP(Succ, SUCC), (C(Fixnum), C(String), C(Time));
10841084
OP(EqTilde, MATCH), (C(Regexp), C(String));
1085+
OP(Freeze, FREEZE), (C(String));
10851086
#undef C
10861087
#undef OP
10871088
}

vm_insnhelper.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ enum {
5656
BOP_NOT,
5757
BOP_NEQ,
5858
BOP_MATCH,
59+
BOP_FREEZE,
5960

6061
BOP_LAST_
6162
};

0 commit comments

Comments
 (0)