Skip to content
Permalink
Browse files

Missed optimizations

- freezing constant string to ensure it's not mutated
- use constant where available
- optimize `respond_to?` to take less memory. 

Discussed in #737 and #739

`respond_to?` takes two arguments all recent rubies:

- http://ruby-doc.org/core-2.1.3/Object.html#method-i-respond_to-3F
- http://ruby-doc.org/core-1.9.3/Object.html#method-i-respond_to-3F
- http://ruby-doc.org/core-1.8.7/Object.html#method-i-respond_to-3F

Also `method_missing` will return a symbol from the first argument:

- http://ruby-doc.org/core-2.1.3/BasicObject.html#method-i-method_missing
- http://ruby-doc.org/core-1.9.3/BasicObject.html#method-i-method_missing
- http://ruby-doc.org/core-1.8.7/Kernel.html#method-i-method_missing
  • Loading branch information...
schneems committed Oct 3, 2014
1 parent 022b007 commit 5f808aa2099841e5daec6cb772a304797879ce6c
Showing with 12 additions and 9 deletions.
  1. +9 −6 lib/rack/body_proxy.rb
  2. +2 −2 lib/rack/directory.rb
  3. +1 −1 lib/rack/runtime.rb
@@ -4,9 +4,12 @@ def initialize(body, &block)
@body, @block, @closed = body, block, false
end

def respond_to?(*args)
return false if args.first.to_s =~ /^to_ary$/
super or @body.respond_to?(*args)
def respond_to?(method_name, include_all=false)
case method_name
when :to_ary, 'to_ary'
return false
end
super or @body.respond_to?(method_name, include_all)
end

def close
@@ -31,9 +34,9 @@ def each(*args, &block)
@body.each(*args, &block)
end

def method_missing(*args, &block)
super if args.first.to_s =~ /^to_ary$/
@body.__send__(*args, &block)
def method_missing(method_name, *args, &block)
super if :to_ary == method_name
@body.__send__(method_name, *args, &block)
end
end
end
@@ -71,7 +71,7 @@ def check_forbidden

body = "Forbidden\n"
size = Rack::Utils.bytesize(body)
return [403, {"Content-Type" => "text/plain",
return [403, {CONTENT_TYPE => "text/plain",
CONTENT_LENGTH => size.to_s,
"X-Cascade" => "pass"}, [body]]
end
@@ -129,7 +129,7 @@ def list_path
def entity_not_found
body = "Entity not found: #{@path_info}\n"
size = Rack::Utils.bytesize(body)
return [404, {"Content-Type" => "text/plain",
return [404, {CONTENT_TYPE => "text/plain",
CONTENT_LENGTH => size.to_s,
"X-Cascade" => "pass"}, [body]]
end
@@ -12,7 +12,7 @@ def initialize(app, name = nil)
@header_name << "-#{name}" if name
end

FORMAT_STRING = "%0.6f"
FORMAT_STRING = "%0.6f".freeze
def call(env)
start_time = Time.now
status, headers, body = @app.call(env)

0 comments on commit 5f808aa

Please sign in to comment.
You can’t perform that action at this time.