Skip to content

Avoid dup and pop in conditional assignments - #6414

Merged
jhawthorn merged 2 commits into
ruby:masterfrom
HParker:avoid-dup-and-pop-in-conditional-assignments
Sep 22, 2022
Merged

Avoid dup and pop in conditional assignments#6414
jhawthorn merged 2 commits into
ruby:masterfrom
HParker:avoid-dup-and-pop-in-conditional-assignments

Conversation

@HParker

@HParker HParker commented Sep 21, 2022

Copy link
Copy Markdown
Contributor

Remove some extra dup and pop generated in ||= and &&=

ruby --dump=insns -e 'x ||= 1; nil'

before:

== disasm: #<ISeq:<main>@-e:1 (1,0)-(1,12)> (catch: FALSE)
local table (size: 1, argc: 0 [opts: 0, rest: -1, post: 0, block: -1, kw: -1@-1, kwrest: -1])
[ 1] x@0
0000 getlocal_WC_0                          x@0                       (   1)[Li]
0002 dup
0003 branchif                               10
0005 pop
0006 putobject_INT2FIX_1_
0007 dup
0008 setlocal_WC_0                          x@0
0010 pop
0011 putnil
0012 leave

after:

== disasm: #<ISeq:<main>@-e:1 (1,0)-(1,12)> (catch: false)
local table (size: 1, argc: 0 [opts: 0, rest: -1, post: 0, block: -1, kw: -1@-1, kwrest: -1])
[ 1] x@0
0000 getlocal_WC_0                          x@0                       (   1)[Li]
0002 branchif                               7
0004 putobject_INT2FIX_1_
0005 setlocal_WC_0                          x@0
0007 putnil
0008 leave

ruby --dump=insns -e 'x.y ||= 1; nil'

before:

== disasm: #<ISeq:<main>@-e:1 (1,0)-(1,14)> (catch: FALSE)
0000 putself                                                          (   1)[Li]
0001 opt_send_without_block                 <calldata!mid:x, argc:0, FCALL|VCALL|ARGS_SIMPLE>
0003 dup
0004 opt_send_without_block                 <calldata!mid:y, argc:0, ARGS_SIMPLE>
0006 dup
0007 branchif                               18
0009 pop
0010 putobject_INT2FIX_1_
0011 swap
0012 topn                                   1
0014 opt_send_without_block                 <calldata!mid:y=, argc:1, ARGS_SIMPLE>
0016 jump                                   19
0018 swap
0019 pop
0020 pop
0021 putnil
0022 leave

after:

== disasm: #<ISeq:<main>@-e:1 (1,0)-(1,14)> (catch: false)
0000 putself                                                          (   1)[Li]
0001 opt_send_without_block                 <calldata!mid:x, argc:0, FCALL|VCALL|ARGS_SIMPLE>
0003 dup
0004 opt_send_without_block                 <calldata!mid:y, argc:0, ARGS_SIMPLE>
0006 branchif                               11
0008 putobject_INT2FIX_1_
0009 opt_send_without_block                 <calldata!mid:y=, argc:1, ARGS_SIMPLE>
0011 pop
0012 putnil
0013 leave
compare-ruby: ruby 3.2.0dev (2022-04-04T13:27:14Z master ea9c09a92c) [x86_64-darwin21]
built-ruby: ruby 3.2.0dev (2022-09-21T21:12:45Z avoid-dup-and-pop-.. da183a284c) [x86_64-darwin21]
# Iteration per second (i/s)

|                  |compare-ruby|built-ruby|
|:-----------------|-----------:|---------:|
|vm_lvar_cond_set  |     63.841M|  109.491M|
|                  |           -|     1.72x|

Co-authored-by: John Hawthorn <jhawthorn@github.com>
@HParker
HParker force-pushed the avoid-dup-and-pop-in-conditional-assignments branch from c65fb4f to ab8a77e Compare September 21, 2022 22:07
Co-authored-by: John Hawthorn <jhawthorn@github.com>
@HParker
HParker force-pushed the avoid-dup-and-pop-in-conditional-assignments branch from ab8a77e to 3031d62 Compare September 21, 2022 22:12
@jhawthorn
jhawthorn merged commit fbaac83 into ruby:master Sep 22, 2022
@HParker
HParker deleted the avoid-dup-and-pop-in-conditional-assignments branch November 29, 2022 06:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants