1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00
ruby--ruby/lib/irb
schneems 2b22c93533 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 e0a5c3d2b7/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
```
2021-12-02 15:55:42 +09:00
..
cmd Compatibility with IRB 2021-12-02 15:55:42 +09:00
ext [ruby/irb] Make save-history extension safe for concurrent use 2021-03-06 00:18:32 +09:00
lc [ruby/irb] Add help about extra doc dir option 2021-10-11 21:12:58 +09:00
.document
color.rb [ruby/irb] Added colorable keyword option 2021-04-26 21:14:52 +09:00
color_printer.rb [ruby/irb] Don't call Ruby 2.4+'s String#pretty_print 2021-03-20 20:25:32 +09:00
completion.rb [ruby/irb] Ignore parenthesis during completion 2021-10-13 06:33:48 +09:00
context.rb [ruby/irb] Add --extra-doc-dir option to show doc dialog 2021-10-11 21:12:57 +09:00
easter-egg.rb Revert "Prefer #send over #__send__ when it is clear there is no possible conflict" 2020-11-05 20:54:34 +09:00
extend-command.rb [ruby/irb] Add show_source command 2021-04-02 16:40:06 +09:00
frame.rb
help.rb
init.rb [ruby/irb] Add --extra-doc-dir option to show doc dialog 2021-10-11 21:12:57 +09:00
input-method.rb [ruby/irb] Add --extra-doc-dir option to show doc dialog 2021-10-11 21:12:57 +09:00
inspector.rb [ruby/irb] Make IRB::ColorPrinter.pp compatible with PP.pp 2021-01-07 22:43:40 -08:00
irb.gemspec [ruby/irb] Update dependency, reline >= 0.2.8.pre.11 2021-10-09 23:29:24 +09:00
locale.rb
magic-file.rb
notifier.rb
output-method.rb
ruby-lex.rb Compatibility with IRB 2021-12-02 15:55:42 +09:00
ruby_logo.aa
src_encoding.rb
version.rb [ruby/irb] Version 1.3.8.pre.11 2021-10-09 23:29:29 +09:00
workspace.rb [ruby/irb] Move IRB::TOPLEVEL_BINDING from exe/irb to lib/irb/workspace.rb 2021-09-10 06:37:07 +09:00
ws-for-case-2.rb
xmp.rb