Skip to content

Commit 0d96723

Browse files
committed
Collapse putobject, putobject, newarray
This collapses: ``` == disasm: #<ISeq:bar@bench.rb:3 (3,0)-(5,3)> (catch: FALSE) 0000 putobject "a" ( 4)[LiCa] 0002 putobject "b" 0004 putobject "c" 0006 putobject "d" 0008 putobject "e" 0010 putobject "f" 0012 putobject "g" 0014 putobject "h" 0016 putobject "i" 0018 putobject "j" 0020 putobject "k" 0022 newarray 11 0024 leave ( 5)[Re] ``` In to this: ``` == disasm: #<ISeq:bar@bench.rb:3 (3,0)-(5,3)> (catch: FALSE) 0000 duparray ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"]( 4)[LiCa] 0002 leave ( 5)[Re] ```
1 parent 5d3aa0a commit 0d96723

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

compile.c

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3847,7 +3847,7 @@ enum compile_array_type_t {
38473847
};
38483848

38493849
static inline int
3850-
static_literal_node_p(const NODE *node)
3850+
static_literal_node_p(const NODE *node, const rb_iseq_t *iseq)
38513851
{
38523852
node = node->nd_head;
38533853
switch (nd_type(node)) {
@@ -3856,6 +3856,12 @@ static_literal_node_p(const NODE *node)
38563856
case NODE_TRUE:
38573857
case NODE_FALSE:
38583858
return TRUE;
3859+
case NODE_STR:
3860+
if (ISEQ_COMPILE_DATA(iseq)->option->frozen_string_literal) {
3861+
return TRUE;
3862+
} else {
3863+
return FALSE;
3864+
}
38593865
default:
38603866
return FALSE;
38613867
}
@@ -3872,6 +3878,8 @@ static_literal_value(const NODE *node)
38723878
return Qtrue;
38733879
case NODE_FALSE:
38743880
return Qfalse;
3881+
case NODE_STR:
3882+
return rb_fstring(node->nd_lit);
38753883
default:
38763884
return node->nd_lit;
38773885
}
@@ -3921,7 +3929,7 @@ compile_array(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *const node_ro
39213929
}
39223930
break;
39233931
}
3924-
if (opt_p && !static_literal_node_p(node)) {
3932+
if (opt_p && !static_literal_node_p(node, iseq)) {
39253933
opt_p = 0;
39263934
}
39273935

@@ -3947,8 +3955,8 @@ compile_array(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *const node_ro
39473955
node = node->nd_next;
39483956
}
39493957
while (node && node->nd_next &&
3950-
static_literal_node_p(node) &&
3951-
static_literal_node_p(node->nd_next)) {
3958+
static_literal_node_p(node, iseq) &&
3959+
static_literal_node_p(node->nd_next, iseq)) {
39523960
VALUE elem[2];
39533961
elem[0] = static_literal_value(node);
39543962
elem[1] = static_literal_value(node->nd_next);

test/ruby/test_literal.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,12 @@ def test_frozen_string
177177
end
178178
end
179179

180+
def test_frozen_string_in_array_literal
181+
list = eval("# frozen-string-literal: true\n""['foo', 'bar']")
182+
assert_equal 2, list.length
183+
list.each { |str| assert_predicate str, :frozen? }
184+
end
185+
180186
if defined?(RubyVM::InstructionSequence.compile_option) and
181187
RubyVM::InstructionSequence.compile_option.key?(:debug_frozen_string_literal)
182188
def test_debug_frozen_string

0 commit comments

Comments
 (0)