Skip to content

Commit

Permalink
Simplify the code with Forwardable, leave object_id and __send__ methods
Browse files Browse the repository at this point in the history
  • Loading branch information
exAspArk committed Sep 14, 2017
1 parent 9d62c71 commit 3701923
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 30 deletions.
35 changes: 11 additions & 24 deletions lib/batch_loader.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
# frozen_string_literal: true

require "set"
require "forwardable"

require "batch_loader/version"
require "batch_loader/executor_proxy"
require "batch_loader/middleware"
require "batch_loader/graphql"

class BatchLoader
extend Forwardable

NoBatchError = Class.new(StandardError)

def self.for(item)
Expand Down Expand Up @@ -74,15 +78,11 @@ class << self
end

def replace_with!(value)
BatchLoader.send(:without_warnings) do
ignore_method_names = %i[singleton_method_added].freeze
singleton_class.class_eval do
(value.methods - ignore_method_names).each do |method_name|
define_method(method_name) do |*args, &block|
value.public_send(method_name, *args, &block)
end
end
end
@loaded_value = value

singleton_class.class_eval do
ignore_method_names = %i[object_id __send__ singleton_method_added].freeze
def_delegators :@loaded_value, *(value.methods - ignore_method_names)
end
end

Expand All @@ -98,19 +98,6 @@ def executor_proxy
end
end

class << self
private

def without_warnings(&block)
warning_level = $VERBOSE
$VERBOSE = nil
block.call
$VERBOSE = warning_level
end
end

without_warnings do
leave_method_names = %i[batch batch_loader? respond_to?].freeze
(instance_methods - leave_method_names).each { |method_name| undef_method(method_name) }
end
leave_method_names = %i[object_id __send__ batch batch_loader? respond_to?].freeze
(instance_methods - leave_method_names).each { |method_name| undef_method(method_name) }
end
12 changes: 6 additions & 6 deletions spec/benchmarks/loading.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@
# with BatchLoader without cache 1.000 i/100ms

# Calculating -------------------------------------
# without BatchLoader 965.088k15.1%) i/s - 3.399M in 3.864552s
# with BatchLoader with cache 991.089k (± 8.0%) i/s - 3.272M
# with BatchLoader without cache 75.239k (±18.1%) i/s - 345.350k in 4.883875s
# without BatchLoader 939.708k19.2%) i/s - 3.241M in 3.907448s
# with BatchLoader with cache 990.611k (± 8.3%) i/s - 3.283M
# with BatchLoader without cache 76.292k (±18.0%) i/s - 350.402k in 4.886185s

# Comparison:
# with BatchLoader with cache: 991089.2 i/s
# without BatchLoader: 965088.1 i/s - same-ish: difference falls within error
# with BatchLoader without cache: 75238.7 i/s - 13.17x slower
# with BatchLoader with cache: 990611.0 i/s
# without BatchLoader: 939708.2 i/s - same-ish: difference falls within error
# with BatchLoader without cache: 76292.0 i/s - 12.98x slower

0 comments on commit 3701923

Please sign in to comment.