Skip to content

Commit

Permalink
optimize by replacing interpolation with concatenation
Browse files Browse the repository at this point in the history
  • Loading branch information
geemus committed Nov 14, 2010
1 parent 5cb3d4e commit 4351e0b
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.rdoc
Expand Up @@ -14,7 +14,7 @@ This will return a response object, which has body, headers and status attribute

This supports all the major http verbs: [connect, delete, get, head, options, post, put, trace]

You can also create a connection to try and keep open across multiple requests.
You can also create a connection to try and keep open across multiple requests (more performant!).

connection = Excon.new('http://geemus.com')
connection.request(:method => 'GET')
Expand Down
21 changes: 21 additions & 0 deletions benchmarks/concat_vs_interpolate.rb
@@ -0,0 +1,21 @@
require 'rubygems'
require 'tach'

key = 'Content-Length'
value = '100'
Tach.meter(1_000) do
tach('concat') do
key << ': ' << value << "\r\n"
end
tach('interpolate') do
"#{key}: value\r\n"
end
end

# +-------------+----------+
# | tach | total |
# +-------------+----------+
# | concat | 0.000902 |
# +-------------+----------+
# | interpolate | 0.019667 |
# +-------------+----------+
2 changes: 1 addition & 1 deletion lib/excon.rb
Expand Up @@ -28,7 +28,7 @@ def self.new(url, params = {})
%w{connect delete get head options post put trace}.each do |method|
eval <<-DEF
def self.#{method}(url, params = {}, &block)
new(url).request(params.merge!(:method => '#{method.upcase}'), &block)
new(url).request(params.merge!(:method => :#{method}), &block)
end
DEF
end
Expand Down
9 changes: 5 additions & 4 deletions lib/excon/connection.rb
Expand Up @@ -17,12 +17,13 @@ def request(params, &block)
begin
params[:path] ||= @connection[:path]
unless params[:path][0..0] == '/'
params[:path] = "/#{params[:path]}"
params[:path] = '/' << params[:path]
end
request = "#{params[:method].upcase} #{params[:path]}?"
request = params[:method].to_s.upcase << ' ' << params[:path] << '?'
for key, values in (params[:query] || @connection[:query] || {})
for value in [*values]
request << "#{key}#{value && "=#{CGI.escape(value.to_s)}"}&"
value_string = value && ('=' << CGI.escape(value.to_s))
request << key << value_string << '&'
end
end
request.chop!
Expand All @@ -43,7 +44,7 @@ def request(params, &block)
0
end
for key, value in params[:headers]
request << "#{key}: #{value}\r\n"
request << key << ': ' << value << "\r\n"
end
request << "\r\n"
socket.write(request)
Expand Down

0 comments on commit 4351e0b

Please sign in to comment.