summaryrefslogtreecommitdiff
path: root/ext/ripper
diff options
context:
space:
mode:
authorschneems <richard.schneeman+foo@gmail.com>2021-11-07 19:33:04 -0600
committerYusuke Endoh <mame@ruby-lang.org>2021-12-02 15:55:42 +0900
commit2b22c93533a3d94e5fc907682d862f89b62e5bf7 (patch)
treed39d2c97ca5b568d39f140dda89985eee28fee79 /ext/ripper
parent3685b5af95fc31b99b34a5a4f75bdc7c0ba622f4 (diff)
Compatibility with IRB
Instead of accessing the struct as an array, access it via methods. There are other places inside of this file already using this API (for example https://github.com/ruby/ruby/blob/e0a5c3d2b71dfad038d7562fdd33f02ffd79232d/lib/irb/ruby-lex.rb#L829-L830). This commit moves all struct array-ish calls to use their method calls instead. It is also ~1.23 faster accessing values via a method instead of as an array according to this microbenchmark: ```ruby Elem = Struct.new(:pos, :event, :tok, :state, :message) do def initialize(pos, event, tok, state, message = nil) super(pos, event, tok, State.new(state), message) end # ... def to_a a = super a.pop unless a.empty? a end end class ElemClass attr_accessor :pos, :event, :tok, :state, :message def initialize(pos, event, tok, state, message = nil) @pos = pos @event = event @tok = tok @state = State.new(state) @message = message end def to_a if @message [@pos, @event, @tok, @state, @message] else [@pos, @event, @tok, @state] end end end # stub state class creation for now class State; def initialize(val); end; end ``` ```ruby Benchmark.ips do |x| x.report("struct") { struct[1] } x.report("class ") { from_class.event } x.compare! end; nil ``` ``` Warming up -------------------------------------- struct 1.624M i/100ms class 1.958M i/100ms Calculating ------------------------------------- struct 17.139M (± 2.6%) i/s - 86.077M in 5.025801s class 21.104M (± 3.4%) i/s - 105.709M in 5.015193s Comparison: class : 21103826.3 i/s struct: 17139201.5 i/s - 1.23x (± 0.00) slower ```
Notes
Notes: Merged: https://github.com/ruby/ruby/pull/5093
Diffstat (limited to 'ext/ripper')
0 files changed, 0 insertions, 0 deletions