Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
opt_aset_with
  • Loading branch information
kddnewton committed Nov 22, 2022
commit cc24d7f4198beb08cb3c37e244535afee013554b
18 changes: 18 additions & 0 deletions lib/syntax_tree/compiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,24 @@ def visit_array(node)
def visit_assign(node)
case node.target
when ARefField
if !frozen_string_literal && specialized_instruction && (node.target.index.parts.length == 1)
arg = node.target.index.parts.first

if arg.is_a?(StringLiteral) && (arg.parts.length == 1)
string_part = arg.parts.first

if string_part.is_a?(TStringContent)
visit(node.target.collection)
visit(node.value)
iseq.swap
iseq.topn(1)
iseq.opt_aset_with(string_part.value, :[]=, 2)
iseq.pop
return
end
end
end

iseq.putnil
visit(node.target.collection)
visit(node.target.index)
Expand Down
5 changes: 5 additions & 0 deletions lib/syntax_tree/yarv.rb
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,11 @@ def opt_aref_with(object, method_id, argc, flag = VM_CALL_ARGS_SIMPLE)
push([:opt_aref_with, object, call_data(method_id, argc, flag)])
end

def opt_aset_with(object, method_id, argc, flag = VM_CALL_ARGS_SIMPLE)
stack.change_by(-2 + 1)
push([:opt_aset_with, object, call_data(method_id, argc, flag)])
end

def opt_getconstant_path(names)
if RUBY_VERSION >= "3.2"
stack.change_by(+1)
Expand Down
1 change: 1 addition & 0 deletions test/compiler_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ class CompilerTest < Minitest::Test
"foo[bar] <<= 1",
"foo[bar] ^= 1",
"foo['true']",
"foo['true'] = 1",
# Constants (single)
"Foo",
"Foo = 1",
Expand Down