1
0
Fork 0
mirror of https://github.com/pry/pry.git synced 2022-11-09 12:35:05 -05:00
pry--pry/spec
Kyrylo Silin a4e9dd2e7d ring: rewrite the class to improve API
Currently, the Ring class is written with help of Hash as backend store.
According to the comments, the implementation should behave like a circular
buffer, however it doesn't. Upon reaching maximum capacity Ring doesn't replace
old elements but keeps writing to new cells, deleting old cells, so that the
hash contains `nil` entries.

The new implementation is based on Array and seems to be closer to the actual
Ring. Older elemens get overwritten with newer ones.

This class also includes Enumerable, however none of our APIs take advantage of
it, so it seems like an overkill. There was also a problem with with this API
because of the above-mentioned nils. For example, if the ring exceeds its
maximum size, then callin `Enumerable#first` on it returns `nil`.

The new implementation deals with this with removal of Enumerable. The `#[]`
syntax is preserved, and now `ring[0]` returns an actual element instead of
`nil`. In case users need the Enumerable functionality, they can call
`Ring#to_a` to build the array, which supports the wanted methods.

As for the speed, the new implementation is:

* slower overall because it's thread-safe
* faster without mutexes for `#<<`
* slower without mutexes for `#[]`

Benchmark for old implementation:

```
Warming up --------------------------------------
             Ring#<<   223.451k i/100ms
             Ring#[]     2.837k i/100ms
Calculating -------------------------------------
             Ring#<<    223.157B (± 3.4%) i/s -    778.097B
             Ring#[]     82.485M (± 9.4%) i/s -    402.602M in   4.957792s
```

Benchmark for this implementation:

```
Warming up --------------------------------------
             Ring#<<   211.587k i/100ms
             Ring#[]     1.974k i/100ms
Calculating -------------------------------------
             Ring#<<    211.385B (± 2.8%) i/s -    698.439B
             Ring#[]     40.292M (±17.0%) i/s -    190.069M in   4.971195s
```

The benchmark:

```rb
require './lib/pry'
require 'benchmark/ips'

Benchmark.ips do |x|
  empty_ring = Pry::Ring.new(100)
  populated_ring = Pry::Ring.new(100)
  150.times { |i| populated_ring << i }

  x.report("Ring#<<") do |times|
    empty_ring << times
  end

  x.report("Ring#[]") do |times|
    populated_ring[0]
    populated_ring[1]
    populated_ring[2]

    populated_ring[-1]
    populated_ring[-2]
    populated_ring[-3]

    populated_ring[1..2]
    populated_ring[-2..-1]
    populated_ring[-2..3]

    populated_ring[0..-1]

    populated_ring[2..-1]
    populated_ring[-1..10]

    populated_ring[-1..0]
    populated_ring[-1..1]
  end
end
```
2018-10-21 05:31:45 +08:00
..
commands core_extensions: make '__binding__' work with redefined #respond_to? 2018-10-17 23:10:21 +08:00
config last_default might not be a Pry::Config::Memoized 2017-06-23 17:42:04 +01:00
fixtures rubocop: fix offences of the Layout/EmptyLineBetweenDefs cop 2018-10-14 14:40:48 +08:00
helpers rubocop: fix offences of the Style/HashSyntax cop 2018-10-13 03:09:29 +08:00
integration rubocop: fix offences of the Layout/ExtraSpacing cop 2018-10-13 00:54:00 +08:00
method specs: convert to shoulds to expects 2015-03-10 22:49:29 +02:00
spec_helpers rubocop: fix offences of the Lint/UnneededRequireStatement cop 2018-10-16 04:43:41 +08:00
cli_spec.rb specs: convert to shoulds to expects 2015-03-10 22:49:29 +02:00
code_object_spec.rb rubocop: fix offences of the Style/HashSyntax cop 2018-10-13 03:09:29 +08:00
code_spec.rb rubocop: fix offences of the Layout/ExtraSpacing cop 2018-10-13 00:54:00 +08:00
color_printer_spec.rb Issue #1647: Fix String#pp output color 2017-10-22 15:42:44 -05:00
command_helpers_spec.rb specs: convert to shoulds to expects 2015-03-10 22:49:29 +02:00
command_integration_spec.rb rubocop: fix offences of the Style/HashSyntax cop 2018-10-13 03:09:29 +08:00
command_set_spec.rb rubocop: fix offences of the Lint/UnusedMethodArgument cop 2018-10-14 14:56:53 +08:00
command_spec.rb rubocop: fix offences of the Lint/UnusedMethodArgument cop 2018-10-14 14:56:53 +08:00
completion_spec.rb rubocop: fix offences of the Style/HashSyntax cop 2018-10-13 03:09:29 +08:00
config_spec.rb rubocop: fix offences of the Lint/AmbiguousBlockAssociation cop 2018-10-16 04:38:17 +08:00
control_d_handler_spec.rb specs: convert to shoulds to expects 2015-03-10 22:49:29 +02:00
documentation_helper_spec.rb Syntax highlight <tt> tags in documentation output. 2015-06-24 16:36:03 +03:00
editor_spec.rb specs: convert to shoulds to expects 2015-03-10 22:49:29 +02:00
exception_whitelist_spec.rb rubocop: fix offences of the Style/HashSyntax cop 2018-10-13 03:09:29 +08:00
helper.rb rubocop: fix offences of the Lint/AmbiguousBlockAssociation cop 2018-10-16 04:38:17 +08:00
history_spec.rb rubocop: fix offences of the Style/HashSyntax cop 2018-10-13 03:09:29 +08:00
hooks_spec.rb rubocop: fix offences of the Style/HashSyntax cop 2018-10-13 03:09:29 +08:00
indent_spec.rb singleline rescue should not change indentation. 2015-07-22 21:30:36 +03:00
method_spec.rb rubocop: fix offences of the Lint/AmbiguousBlockAssociation cop 2018-10-16 04:38:17 +08:00
pager_spec.rb specs: convert to shoulds to expects 2015-03-10 22:49:29 +02:00
prompt_spec.rb rubocop: fix offences of the Style/HashSyntax cop 2018-10-13 03:09:29 +08:00
pry_defaults_spec.rb rubocop: fix offences of the Layout/EmptyLineBetweenDefs cop 2018-10-14 14:40:48 +08:00
pry_output_spec.rb rubocop: fix offences of the Lint/AmbiguousBlockAssociation cop 2018-10-16 04:38:17 +08:00
pry_repl_spec.rb rubocop: fix offences of the Style/HashSyntax cop 2018-10-13 03:09:29 +08:00
pry_spec.rb Rename HistoryArray to Ring 2018-10-20 00:36:22 +08:00
pryrc_spec.rb rubocop: fix offences of the Style/HashSyntax cop 2018-10-13 03:09:29 +08:00
ring_spec.rb ring: rewrite the class to improve API 2018-10-21 05:31:45 +08:00
run_command_spec.rb rubocop: fix offences of the Style/HashSyntax cop 2018-10-13 03:09:29 +08:00
sticky_locals_spec.rb rubocop: fix offences of the Style/HashSyntax cop 2018-10-13 03:09:29 +08:00
syntax_checking_spec.rb Drop support for Rubinius 2018-10-07 00:58:53 +08:00
wrapped_module_spec.rb rubocop: fix offences of the Layout/ExtraSpacing cop 2018-10-13 00:54:00 +08:00