Skip to content

Commit

Permalink
remove hacks when conditionally forwarding blocks in methods
Browse files Browse the repository at this point in the history
Yes, named blocks (Procs) in methods are slow in Ruby. This is because
of unfortunate performance penalty of instantiating and
garbage-collecting these Proc objects, which happens even if we never
use or even pass a block to the method. Certain methods in Faraday were
optimized to avoid named blocks.

This removes such optimizations. The rationale is that the performance
penalty of named blocks is negligible to the overhead of HTTP requests
over the network.
  • Loading branch information
mislav committed Jan 6, 2012
1 parent 445152b commit 7f0cd87
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 22 deletions.
15 changes: 5 additions & 10 deletions lib/faraday/builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,7 @@ def locked?
@handlers.frozen?
end

def use(klass, *args)
block = block_given? ? Proc.new : nil
def use(klass, *args, &block)
if klass.is_a? Symbol
use_symbol(Faraday::Middleware, klass, *args, &block)
else
Expand All @@ -97,18 +96,15 @@ def use(klass, *args)
end
end

def request(key, *args)
block = block_given? ? Proc.new : nil
def request(key, *args, &block)
use_symbol(Faraday::Request, key, *args, &block)
end

def response(key, *args)
block = block_given? ? Proc.new : nil
def response(key, *args, &block)
use_symbol(Faraday::Response, key, *args, &block)
end

def adapter(key, *args)
block = block_given? ? Proc.new : nil
def adapter(key, *args, &block)
use_symbol(Faraday::Adapter, key, *args, &block)
end

Expand Down Expand Up @@ -146,8 +142,7 @@ def raise_if_locked
raise StackLocked, "can't modify middleware stack after making a request" if locked?
end

def use_symbol(mod, key, *args)
block = block_given? ? Proc.new : nil
def use_symbol(mod, key, *args, &block)
use(mod.lookup_middleware(key), *args, &block)
end

Expand Down
18 changes: 6 additions & 12 deletions lib/faraday/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,33 +71,27 @@ def app
end
end

def get(url = nil, headers = nil)
block = block_given? ? Proc.new : nil
def get(url = nil, headers = nil, &block)
run_request(:get, url, nil, headers, &block)
end

def post(url = nil, body = nil, headers = nil)
block = block_given? ? Proc.new : nil
def post(url = nil, body = nil, headers = nil, &block)
run_request(:post, url, body, headers, &block)
end

def put(url = nil, body = nil, headers = nil)
block = block_given? ? Proc.new : nil
def put(url = nil, body = nil, headers = nil, &block)
run_request(:put, url, body, headers, &block)
end

def patch(url = nil, body = nil, headers = nil)
block = block_given? ? Proc.new : nil
def patch(url = nil, body = nil, headers = nil, &block)
run_request(:patch, url, body, headers, &block)
end

def head(url = nil, headers = nil)
block = block_given? ? Proc.new : nil
def head(url = nil, headers = nil, &block)
run_request(:head, url, nil, headers, &block)
end

def delete(url = nil, headers = nil)
block = block_given? ? Proc.new : nil
def delete(url = nil, headers = nil, &block)
run_request(:delete, url, nil, headers, &block)
end

Expand Down

0 comments on commit 7f0cd87

Please sign in to comment.