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

support callable prepare objects in Schema::Argument (fixes #1786) #1787

Merged
merged 1 commit into from
Sep 10, 2018
Merged

support callable prepare objects in Schema::Argument (fixes #1786) #1787

merged 1 commit into from
Sep 10, 2018

Conversation

modosc
Copy link
Contributor

@modosc modosc commented Aug 16, 2018

No description provided.

@rmosolgo
Copy link
Owner

Thanks for the fix here! I'm going to change it on master to use is_a?/respond_to?, for performance:

# bench.rb
require "benchmark/ips"

def with_proc(callable)
  case callable
  when String, Symbol
    callable
  when ->(x) { x.respond_to?(:call) }
    callable
  else
    nil
  end
end

def with_respond_to(callable)
  if callable.is_a?(String) || callable.is_a?(Symbol)
    callable
  elsif callable.respond_to?(:call)
    callable
  else
    nil
  end
end

callable_input = ->(x) { x }

Benchmark.ips do |x|
  x.report("proc condition") { with_proc(callable_input) }
  x.report("respond_to?/is_a?") { with_respond_to(callable_input) }
  x.compare!
end
$ ruby bench.rb
Warming up --------------------------------------
      proc condition    85.632k i/100ms
   respond_to?/is_a?   210.167k i/100ms
Calculating -------------------------------------
      proc condition      1.196M (± 3.5%) i/s -      5.994M in   5.020018s
   respond_to?/is_a?      5.023M (± 3.7%) i/s -     25.220M in   5.027962s

Comparison:
   respond_to?/is_a?:  5023030.1 i/s
      proc condition:  1195574.0 i/s - 4.20x  slower

@rmosolgo rmosolgo merged commit 50d3b33 into rmosolgo:master Sep 10, 2018
@modosc modosc deleted the allow-callable-prepares branch December 27, 2018 18:45
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