Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

switch internal only error to a throw/catch #326

Merged
merged 2 commits into from
Nov 29, 2015
Merged

Conversation

AaronLasseigne
Copy link
Owner

There's no need for a full on error object here. Ruby has throw/catch to handle this and it help distinguish errors that might reach the end user from what is otherwise an internal signal.

There's no need for a full on error object here.
@AaronLasseigne
Copy link
Owner Author

Also, throw/catch is faster than fail/rescue.

@tfausak
Copy link
Collaborator

tfausak commented Nov 29, 2015

I'm not opposed to this, but can we show that it's faster with a benchmark?

@AaronLasseigne
Copy link
Owner Author

Run with Ruby 2.2.3:

require 'benchmark/ips'

Benchmark.ips do |x|
  x.report('Catch/Throw') do
    catch(:benchmarking) do
      throw(:benchmarking)
    end
  end

  x.report('Fail/Rescue') do
    begin
      fail StandardError
    rescue StandardError
      # do nothing
    end
  end

  x.compare!
end
Calculating -------------------------------------
         Catch/Throw    73.753k i/100ms
         Fail/Rescue    33.262k i/100ms
-------------------------------------------------
         Catch/Throw      1.990M (± 8.6%) i/s -      9.883M
         Fail/Rescue    518.015k (± 6.6%) i/s -      2.594M

Comparison:
         Catch/Throw:  1990106.8 i/s
         Fail/Rescue:   518015.2 i/s - 3.84x slower

@tfausak
Copy link
Collaborator

tfausak commented Nov 29, 2015

:shipit:

AaronLasseigne added a commit that referenced this pull request Nov 29, 2015
switch internal only error to a throw/catch
@AaronLasseigne AaronLasseigne merged commit 781d62b into master Nov 29, 2015
@AaronLasseigne AaronLasseigne deleted the fail-to-throw branch November 29, 2015 21:27
@AaronLasseigne
Copy link
Owner Author

Thanks for the review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants